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
).
Obviously, when I run a spec using Jasmine node --experimental-modules ./node_modules/jasmine/bin/jasmine
I get the following error:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module
Is it ever possible to use Jasmine using ES modules in Node?
If not, is there any alternative to don't use a framework (e.g. running tests with npm
scripts)?
Simplifying the solution of @Matias_Fidemraizer, keeping only the important bits in one file:
And execute it with
You will have some problems in the logs, receiving a message like
That message mean that you have an exception in the mjs code.
You can follow there: https://github.com/jasmine/jasmine-npm/issues/150
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
:And you would add specs as follows in separate files:
Finally, you would run
jasmine
importing both configuredjasmine
instance and specs: