Mocha支持ES6测试吗?

7

我试图在ES6中使用expectmocha进行测试,但是即使是一个简单的测试案例也会出现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
  • expect 1.6.0
  • mocha 2.2.5

1
请注意,在 babel 6.x 中,您应该编写 --compilers js:babel-core/register - Ivan Kolmychek
1个回答

8
一开始这可能会让人有些困惑,但你所使用的import不正确!请将你的import改为以下内容:
import expect from "expect.js"; 

一切都正常。 这里expect 模块。你期望使用的模块叫做 expect.js

希望这可以帮到你,对于糟糕的双关语感到抱歉 :)

编辑:你还需要确保执行 npm install expect.js


我简直不敢相信就是这样!谢谢你!我随意地从 import 中排除了 .js,认为“哦,我可以在导入时省略文件扩展名”。 - Collin Allen
1
有趣的是,当我尝试重现这个问题时,我做了和你完全一样的事情。 - Brennan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接