使用Jasmine或任何其他替代方案在Node上运行测试.mjs / ESM [英] Running tests .mjs / ESM on Node using Jasmine or any other alternative

查看:93
本文介绍了使用Jasmine或任何其他替代方案在Node上运行测试.mjs / ESM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过-experimental-modules CLI开关(即 node- -experimental-modules )。

My Node-based project is implemented using native ES module support on Node thanks to the --experimental-modules CLI switch (i.e. node --experimental-modules).

很显然,当我使用Jasmine node --experimental-modules运行规范时。 / node_modules / jasmine / bin / jasmine 我收到以下错误:

Obviously, when I run a spec using Jasmine node --experimental-modules ./node_modules/jasmine/bin/jasmine I get the following error:


错误[ERR_REQUIRE_ESM]:必须使用导入以加载ES模块

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

是否可以在Node中通过ES模块使用Jasmine?

Is it ever possible to use Jasmine using ES modules in Node?

如果没有,是否有不使用框架的替代方法(例如,使用 npm 脚本运行测试)?

If not, is there any alternative to don't use a framework (e.g. running tests with npm scripts)?

推荐答案

这比我想象的要容易。

It was easier than I thought.

这只是关于调用一个文件,您可能会调用 run.mjs ,如下所示:

It's just about calling a file which you might call run.mjs as follows:

node --experimental-modules ./run.mjs

整个文件如下所示:

jasmine.mjs

import Jasmine from "jasmine"
import JasmineConsoleReporter from "jasmine-console-reporter"

const jasmine = new Jasmine()
jasmine.loadConfigFile( "./support/jasmine.json" )

jasmine.env.clearReporters()
jasmine.addReporter( new JasmineConsoleReporter( {
    colors: true,
    cleanStack: true,
    verbosity: 4,
    listStyle: 'indent',
    activity: false
} ) )

export default jasmine

您将在单独的文件中添加以下规范:

And you would add specs as follows in separate files:

import jasmine from './my-project/spec/jasmine.mjs'

jasmine.env.describe('Foo', () => {
    jasmine.env.it('Bar', () => {
        // Expects, assertions...
    })
})

最后,您将运行茉莉花导入配置的茉莉花实例和规格:

Finally, you would run jasmine importing both configured jasmine instance and specs:

import jasmine from './my-project/spec/jasmine.mjs'
import someSpec1 from './my-project/spec/someSpec1.mjs'
import someSpecN from './my-project/spec/someSpecN.mjs'

someSpec1()
someSpecN()

jasmine.execute()

这篇关于使用Jasmine或任何其他替代方案在Node上运行测试.mjs / ESM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆