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

编程入门 行业动态 更新时间:2024-10-26 08:28:58
本文介绍了可以在Mocha测试中使用ES6模块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

是否可以在Mocha测试中使用ES6模块?我遇到了export和import关键字的问题.

Is it possible to use ES6 modules in Mocha tests? I have the problems with export and import keywords.

/* 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.结果:

I launch the mocha directly through the PowerShell console. The result:

推荐答案

Babel和Browserfy都有可能 drublic.de/blog/es6-modules-using-browserify -mocha/

It's possible with Babel and Browserfy drublic.de/blog/es6-modules-using-browserify-mocha/

更多推荐

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

本文发布于:2023-07-24 13:23:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1203698.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   测试中   Mocha

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!