可以在Mocha测试中使用ES6模块吗?

And*_*man 6 javascript mocha.js node.js ecmascript-6

ES6,Windows 10 x64,Node.js 8.6.0,Mocha 3.5.3

可以在Mocha测试中使用ES6模块吗?我exportimport关键字有问题。

/* eventEmitter.js
 */

/* Event emitter. */
export default class EventEmitter{

    constructor(){

        const subscriptions = new Map();

        Object.defineProperty(this, 'subscriptions', {
            enumerable: false,
            configurable: false,
            get: function(){
                return subscriptions;
            }
        });
    }

    /* Add the event listener.
     * @eventName - the event name. 
     * @listener - the listener.
     */
    addListener(eventName, listener){
        if(!eventName || !listener) return false;
        else{
            if(this.subscriptions.has(eventName)){
                const arr = this.subscriptions.get(eventName);
                arr.push(listener);
            }
            else{
                const arr = [listener];
                this.subscriptions.set(eventName, arr);
            }
            return true;
        }
    }

    /* Delete the event listener.
     * @eventName - the event name. 
     * @listener - the listener.
     */
    deleteListener(eventName, listener){
        if(!eventName || !listener) return false;
        else{
            if(this.subscriptions.has(eventName)){
                const arr = this.subscriptions.get(eventName);
                let index = arr.indexOf(listener);

                if(index >= 0){
                    arr.splice(index, 1);
                    return true;
                }
                else{
                    return false;
                }
            }
            else{
                return false;
            }
        }
    }

    /* Emit the event.
     * @eventName - the event name. 
     * @info - the event argument.
     */
    emit(eventName, info){
        if(!eventName || !this.subscriptions.has(eventName)) {
            return false;
        }
        else{
            for(let fn of this.subscriptions.get(eventName)){
                if(fn) fn(info);
            }
            return true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

摩卡咖啡测试:

/* test.js 
 * Mocha tests.
 */
import EventEmitter from '../../src/js/eventEmitter.js';

const assert = require('assert');

describe('EventEmitter', function() {
  describe('#constructor()', function() {
    it('should work.', function() {
        const em = new EventEmitter();
        assert.equal(true, Boolean(em));
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

mocha直接通过PowerShell控制台启动。结果:

在此处输入图片说明

JLR*_*she 25

Mocha从 7.1.0 版本开始支持 ESM(发布:2020 年 2 月 26 日)。

这需要 Node 12.11.0 或更高版本,并且受当前在 Node 中使用模块的限制/限制:

  • 对于使用 ES 模块的源文件,您必须使用 .mjs 文件扩展名,或者您"type": "module"的 package.json 中必须有
  • import从 CommonJS 模块导入时不能使用命名导入

等等。

另一种选择是使用esm包,它不受上述限制:

mocha -r esm
Run Code Online (Sandbox Code Playgroud)

我个人的经验是,尝试利用 Mocha 新的、固有的 ESM 支持仍然是一个相当大的负担,但使用 esm 包是非常无缝的。

  • 如果您想使用“mocha --watch”,那么您还必须像“mocha --watch --parallel”一样并行运行测试,以便 watch 不会调用“loadFiles”。 (2认同)

fit*_*rec 5

就我而言,运行:

基本命令:

npx mocha --require esm test_path/

Run Code Online (Sandbox Code Playgroud)

包.json

"scripts": {
    // ...
    "test": "npx mocha --require esm --reporter spec test_path/"
}
Run Code Online (Sandbox Code Playgroud)

润宁

"scripts": {
    // ...
    "test": "npx mocha --require esm --reporter spec test_path/"
}
Run Code Online (Sandbox Code Playgroud)

  • 我得到“找不到模块‘esm’” (4认同)

小智 -1

Babel 和 Browserfy 是可能的 https://drublic.de/blog/es6-modules-using-browserify-mocha/

  • `npm i -D esm` 然后使用 `mocha --require esm` 运行测试 (27认同)
  • 鉴于节点支持开箱即用,这似乎是一个无法回答的问题,因此问题是如何让 mocha 在节点中启用此功能。 (7认同)
  • @user239558 这并不那么容易。在 Mocha 决定之前,不可能使用内置支持。您可以传递标志以在 Node 中启用模块支持,如下所示:`node --experimental-modules $(npm bin)/_mocha`,但使用 `import * from './my-module' 运行代码时它仍然会崩溃`。原因是 Mocha 使用普通的 CommonJS require 导入测试文件,而 Node 要求它使用 import 来启用文件作为 ES 模块运行的模式。这意味着 Mocha 必须做出改变才能使其发挥作用。在此之前,ESM 或转译就是您的答案。 (3认同)