我有以下测试套件...
import Mocha from 'mocha';
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);
(()=>{
let mocha = new Mocha();
mocha.addFile(path.resolve(__dirname,'./tests/sampleTest.js'));
mocha.run(failures => {
console.log("Running Mocha");
process.on('exit', () => {
console.log("Ending Mocha");
process.exit(failures ? 1 : 0);
});
});
})();
还有以下测试文件...
const assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal([1,2,3].indexOf(4), -1);
});
});
});
这很好用,但是,我想将其转换为模块JS(.mjs),以便可以在测试中导入其他模块。我通过更改扩展名和编写代码来尝试此操作...
import assert from 'assert';
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal([1,2,3].indexOf(4), -1);
});
});
});
当我运行它时,我得到...
Must use import to load ES Module: **/src/test/js/webdriver/tests/sampleTest.mjs
我也尝试过像这样导入它...
import Mocha from 'mocha';
import path from 'path';
import './tests/sampleTest.mjs';
const __dirname = path.dirname(new URL(import.meta.url).pathname);
(()=>{
let mocha = new Mocha();
// mocha.addFile(path.resolve(__dirname,'./tests/sampleTest.mjs'));
mocha.run(failures => {
console.log("Running Mocha");
process.on('exit', () => {
console.log("Ending Mocha");
process.exit(failures ? 1 : 0);
});
});
})();
但是我得到...
ReferenceError:描述未定义
我也尝试过
import mocha from "mocha"
...
mocha.describe(...)
但这也不起作用。
如何将另一个模块加载到Mocha中?
答案 0 :(得分:0)
目前唯一的解决方法是https://www.npmjs.com/package/mjs-mocha
我希望此链接适合此处,因为npm可以永久保存软件包。随时参考软件包文档和源代码