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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46487307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:42:09  来源:igfitidea点击:

Is it possible to use ES6 modules in Mocha tests?

javascriptnode.jsecmascript-6mocha

提问by Andrey Bushman

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

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

Is it possible to use ES6 modules in Mocha tests? I have the problems with exportand importkeywords.

是否可以在 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;
        }
    }
}

Mocha test:

摩卡测试:

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

I launch the mochadirectly through the PowerShell console. The result:

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

enter image description here

在此处输入图片说明

采纳答案by Osama Bari

回答by JLRishe

Mocha has support for ESMfrom version 7.1.0 onward (release: Feb. 26, 2020).

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

This requires Node 12.11.0 or higher, and is subject to the current restrictions/limitations of using modules in Node:

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

  • Either you must use .mjs file extensions for source files that use ES modules, or you must have "type": "module"in your package.json
  • You can't use named imports with CommonJS modules
  • 对于使用 ES 模块的源文件,您必须使用 .mjs 文件扩展名,或者您"type": "module"的 package.json 中必须有
  • 你不能在 CommonJS 模块中使用命名导入

And so on.

等等。

Another option is to use the esmpackage, which is not subject to the above limitations:

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

mocha -r esm

My personal experience as of yet is that trying to take advantage of Mocha's new, inherent ESM support is still a considerable burden, but using the esm package is quite seamless.

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