Search code examples
node.jsjestjsgithub-actions

Jest module not defined


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

File jest.config.js

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 } },
};

Workflow

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.


Solution

  • 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

    Jest config

    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: {},
    };