How to use ESM tests with jest?

Here are the steps I took to run Jest with a test using ESM. The source files under test were also written using ESM.

  1. Set my node version to 14.16.0

  2. Install Jest:

    npm i jest -D
    
  3. Add "type": "module" to package.json

  4. Update test script in package.json:

    "scripts": {
      "test": "node --experimental-vm-modules ./node_modules/.bin/jest"
    }
    
  5. Create a jest.config.js file with the following content:

    export default { transform: {} }
    
  6. Have at least one test that could be found using the default testMatch (mine was inside __tests__ dir)

  7. Run tests:

    npm test
    

There is a more complete example in the magic-comments-loader repository on GitHub.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)