跳到主要内容
版本:0.17.0+

jest howto

[toc]

top js test frameworks

top 1: jest

top 2: mocha

others are: jsamine, tape, etc.

In project of rehype-shortcodes, I found them using tape and seems cabinet(小巧的) and output-friendly.

Nevertheless, I used to use jest and ts-jest which is mature and strong. So I didn't pretend to dive into tape deeper.

jest ESM support

  1. Add "type": "module" in package.json
  2. Add transform: {} in jest.config.js
  3. use one of the following approach to run jest in esm mode:
    1. node --experimental-vm-modules ./node_modules/.bin/jest JS_FILE
    2. NODE_OPTIONS=--experimental-vm-modules npx jest JS_FILE
    3. Add "scripts": {"test": "NODE_OPTIONS=--experimental-vm-modules npx jest"} in package.json, then run test JS_FILE in command line
    4. Add --experimental-vm-modules in webstorm jest configuration, and then click run button: picture 35

ref:

official answer of jest

stackoverflow solution

jest + typescript + ESM

// jest.config.js
module.exports = {
preset: 'ts-jest/presets/default-esm', // or other ESM presets
globals: {
'ts-jest': {
useESM: true,
},
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
};

ref:

recommend: