I'm trying to test a file which needs to import an es6 module like this:
https://repl.it/HG9t/0
It seems I'm missing some configurations to make it work properly.
If you can achieve this with another unit testing framework easily, I'm interested too.
Thank you in advance for your help.
Jest can be used to mock ES6 classes that are imported into files you want to test. ES6 classes are constructor functions with some syntactic sugar. Therefore, any mock for an ES6 class must be a function or an actual ES6 class (which is, again, another function). So you can mock them using mock functions.
The ES6 import / export statements are not supported by default in Node version 12. Jest wants CommonJS modules, but you're using . js files.
Jest ships with experimental support for ECMAScript Modules (ESM). The implementation may have bugs and lack features. For the latest status check out the issue and the label on the issue tracker. Also note that the APIs Jest uses to implement ESM support are still considered experimental by Node (as of version 18.8.
yarn add --dev babel-jest @babel/core @babel/preset-env
or
npm install --save-dev babel-jest @babel/core @babel/preset-env
babel.config.js
in your main folder and paste it there:// babel.config.js module.exports = { presets: [ [ '@babel/preset-env', { targets: { node: 'current', }, }, ], ], };
package.json
and jest.config.js
are set to default.As node does not support modules you have to compile your files using Babel. Have a look at the docs on how to configure Jest and Babel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With