Reputation: 1995
I'm writing my first package and I just converted it to be TypeScript compatible, but this somehow affected my GitHub workflow. When I run my tests using Jest locally, they work just fine. When my tests are run on GitHub, it succeeds for 10.x
, but not 12.x
or 14.x
, giving me the following error:
(node:2397) ExperimentalWarning: The ESM module loader is experimental.
ReferenceError: module is not defined
at file:///home/runner/work/enhancedMathJS/enhancedMathJS/jest.config.js:1:1
at ModuleJob.run (internal/modules/esm/module_job.js:146:37)
at async Loader.import (internal/modules/esm/loader.js:182:24)
at async readConfigFileAndSetRootDir (/home/runner/work/enhancedMathJS/enhancedMathJS/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:126:32)
at async readConfig (/home/runner/work/enhancedMathJS/enhancedMathJS/node_modules/jest-config/build/index.js:217:18)
at async readConfigs (/home/runner/work/enhancedMathJS/enhancedMathJS/node_modules/jest-config/build/index.js:406:26)
at async runCLI (/home/runner/work/enhancedMathJS/enhancedMathJS/node_modules/@jest/core/build/cli/index.js:230:59)
at async Object.run (/home/runner/work/enhancedMathJS/enhancedMathJS/node_modules/jest/node_modules/jest-cli/build/cli/index.js:163:37)
npm ERR! Test failed. See above for more details.
[email protected] test /home/runner/work/enhancedMathJS/enhancedMathJS
jest
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['<rootDir>/**/__tests__/**/*.spec.ts'],
testPathIgnorePatterns: ['/node_modules/'],
coverageDirectory: './test-reports',
coveragePathIgnorePatterns: ['node_modules', 'src/database', 'src/test', 'src/types'],
reporters: ['default', 'jest-junit'],
globals: { 'ts-jest': { diagnostics: false } },
};
name: Node.js CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
I don't understand what the problem is, since everything works locally, but not for every version of Node.js on GitHub.
If you want to check out the files and errors for yourself, you can find my repository here.
Upvotes: 46
Views: 36910
Reputation: 1748
I got the very similar error ReferenceError: module is not defined in ES module scope
when running Jest in ESM context.
This did the trick for me:
In jest.config.js
, change
module.exports = {
/* config here */
}
to
export default {
/* config here */
}
Most tutorials as well as Jest's config scaffolding make the config a CJS module which will lead to the above error if you use it in ESM context.
Upvotes: 49
Reputation: 541
I use esm in my project. And config { type: "module" }
in file package.json. So just change module.exports =
to export default
.
Upvotes: 54
Reputation: 1995
I got it working by installing ts-node
and updating my jest.config.js
file to a jest.config.ts
file:
npm i --save-dev ts-node
export default {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['<rootDir>/**/__tests__/**/*.spec.ts'],
testPathIgnorePatterns: ['/node_modules/'],
coverageDirectory: './coverage',
coveragePathIgnorePatterns: ['node_modules', 'src/database', 'src/test', 'src/types'],
reporters: ['default', 'jest-junit'],
globals: { 'ts-jest': { diagnostics: false } },
transform: {},
};
Upvotes: 59