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

新手上路,请多包涵

ES6、Windows 10 x64、Node.js 8.6.0、摩卡 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;
        }
    }
}

摩卡测试:

 /* 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));
    });
  });
});

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

在此处输入图像描述

原文由 Andrey Bushman 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 701
2 个回答

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

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

  • 您必须为使用 ES 模块的源文件使用 .mjs 文件扩展名,或者您的 package.json 中必须有 "type": "module"
  • import 来自 CommonJS 模块时,你不能使用命名导入
  • 本地 import 语句必须明确包含 .js 文件扩展名

等等。

更新的答案

我之前曾推荐使用 esm 包作为 Mocha 内置模块支持的替代方案,但它不再被维护,无法处理更新的语法结构,如 ?. ,和似乎可能根本无法与较新版本的摩卡一起使用。

但是, @babel/register 似乎适用于此:

 mocha -r @babel/register -r regenerator-runtime/runtime

我将其与此预设一起使用(在 .babelrc 中):

 {
    "presets": [
        "@babel/preset-env"
    ]
}

此设置需要以下软件包:

  • @babel/核心
  • @babel/注册
  • @babel/预设环境
  • 再生运行时

您还可以在 .mocharc.js 文件中而不是在命令行中指定这些:

 module.exports = {
    require: [
        '@babel/register',
        'regenerator-runtime/runtime',
    ],
};

到目前为止,我个人的经验是,尝试利用 Mocha 新的、固有的 ESM 支持仍然是一个相当大的负担,但使用这种方法是非常无缝的。

上一个答案

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

 mocha -r esm

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

原文由 JLRishe 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题