Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我是否错误地导出了对象?JS don'新增;我不明白为什么考试不及格_Javascript_Npm_Jestjs_Tdd - Fatal编程技术网

Javascript 我是否错误地导出了对象?JS don'新增;我不明白为什么考试不及格

Javascript 我是否错误地导出了对象?JS don'新增;我不明白为什么考试不及格,javascript,npm,jestjs,tdd,Javascript,Npm,Jestjs,Tdd,我刚开始学习Javascript,我的老师从对象和测试驱动开发开始 这是我的代码和伴随的测试失败了,我不太清楚为什么。当我只有第一个对象和第一个测试时,它工作了,但当我添加第二个对象时,它失败了 主要内容: 测试代码: const { TestScheduler } = require("jest") const person1 = require('./family') const person2 = require('./family') describe('perso

我刚开始学习Javascript,我的老师从对象和测试驱动开发开始

这是我的代码和伴随的测试失败了,我不太清楚为什么。当我只有第一个对象和第一个测试时,它工作了,但当我添加第二个对象时,它失败了

主要内容:

测试代码:

const { TestScheduler } = require("jest")
const person1 = require('./family')
const person2 = require('./family')

describe('person objects', () => {
    test('I am in the family', () => {
        expect(person1.name).toEqual("Louis")
    })
    test('My sister is in the family', () => {
        expect(person2.name).toEqual("Amber")
    })
})
测试结果:

Debugger attached.
 FAIL  ./family.test.js
  person objects
    ✕ I am in the family (4 ms)
    ✓ My sister is in the family

  ● person objects › I am in the family

    expect(received).toEqual(expected) // deep equality

    Expected: "Louis"
    Received: "Amber"

       5 | describe('person objects', () => {
       6 |     test('I am in the family', () => {
    >  7 |         expect(person1.name).toEqual("Louis")
         |                              ^
       8 |     })
       9 |     test('My sister is in the family', () => {
      10 |         expect(person2.name).toEqual("Amber")

      at Object.<anonymous> (family.test.js:7:30)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        1.178 s
已附加调试器。
失败。/family.test.js
人反对
✕ 我是一家人(4毫秒)
✓ 我妹妹在家里
● 个人物品›我在家里
expect(received).toEqual(expected)//深度相等
预期:“路易斯”
收到:“琥珀色”
5 |描述('person objects',()=>{
6 |测试('我在家',()=>{
>7 | expect(人名)。托夸尔(“路易斯”)
|                              ^
8 |     })
9 |测试('我妹妹在家',()=>{
10 | expect(人名)。托夸尔(“琥珀”)
at对象。(family.test.js:7:30)
测试套件:1个失败,共1个
测试:1次失败,1次通过,共2次
快照:共0个
时间:1.178秒
如评论中所述,您只需:

module.exports={
人1,
人2
};
或者,您可以这样导出两个变量:

exports.person1=person1;
exports.person2=person2;
我认为,通常文件中应该只有1个
module.exports=…
语句

module.exports = person1
module.exports = person2
module.exports是一个对象,因此您在上面所做的是将对象值指定给person1,然后将对象值覆盖到person2,这样Louis就消失了

如果要导出多个变量,请执行以下操作:

module.exports = {
  person1: person1,
  person2: person2
}
const { person1, person2 } = require('./family')
或者,如果对象属性名称与变量名称相同,则可以按照Patrick Evans在回答中所述缩短它:

module.exports = {
  person1,
  person2
}
您还可以使用以下方式导出:

module.exports.person1 = person1
module.exports.person2 = person2
或稍微缩短为:

exports.person1 = person1
exports.person2 = person2
要执行命名导入,请执行以下操作:

module.exports = {
  person1: person1,
  person2: person2
}
const { person1, person2 } = require('./family')

否则您不需要更改测试代码。

您正在重新分配
module.exports
,因此第二个分配将覆盖第一个分配,以便将两者导出到一个对象中,并导出该对象,即
module.exports={person1,person2};