Running tests .mjs / ESM on Node using Jasmine or any other alternative

梦想的初衷 提交于 2019-12-04 08:08:20

It was easier than I thought.

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

node --experimental-modules ./run.mjs

The whole file would look like this:

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()

Simplifying the solution of @Matias_Fidemraizer, keeping only the important bits in one file:

import glob from 'glob';
import Jasmine from 'jasmine';

const jasmine = new Jasmine();
jasmine.loadConfigFile('tests/jasmine.json');

// Load your mjs specs
glob('**/*-test.mjs', function (er, files) {
    Promise.all(
        files
            // Use relative paths
            .map(f => f.replace('tests/specs/', './'))
            .map(f => import(f)
                .catch(e => {
                    console.error('** Error loading ' + f + ': ');
                    console.error(e);
                    process.exit(1);
                }))
    )
        .then(() => jasmine.execute());
});

And execute it with

node --experimental-modules jasmine-run.mjs

You will have some problems in the logs, receiving a message like

[...] .split('\n') [...]

That message mean that you have an exception in the mjs code.

You can follow there: https://github.com/jasmine/jasmine-npm/issues/150

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!