gpt4 book ai didi

javascript - 支持 Mocha ES6 测试吗?

转载 作者:数据小太阳 更新时间:2023-10-29 04:38:30 24 4
gpt4 key购买 nike

我正在尝试使用 expect使用 mocha 进行测试,用 ES6 编写,即使是一个简单的测试用例也出现 TypeError:

import expect from "expect"; 

describe('Example', () => {
it('should just work', (done) => {
expect(5).to.eql(5);
done();
});
});

我正在使用 Babel 来转换和运行测试:

./node_modules/.bin/mocha --compilers js:babel/register example.js

结果是:

  Example
1) should just work


0 passing (76ms)
1 failing

1) Example should just work:
TypeError: Cannot read property 'eql' of undefined
at Context.<anonymous> (example.js:5:17)

这是不受支持,还是我遗漏了一些重要的东西?

版本:

  • babel 5.5.6
  • 期待 1.6.0
  • mocha 2.2.5

最佳答案

起初这让人头疼,但您使用的是导入错误的 expect!

将您的导入更改为:

import expect from "expect.js"; 

一切正常。 Hereexpect 模块。您“期望”使用的模块称为 expect.js

希望这对您有所帮助,对于不好的双关语我深表歉意:)

编辑:您还必须确保 npm install expect.js!

关于javascript - 支持 Mocha ES6 测试吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31014826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com