Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 728
Mout Pessemier Avatar asked Nov 19 '20 15:11

Mout Pessemier


People also ask

What is transformIgnorePatterns?

json file. transformIgnorePatterns option can be used to specify which files shall be transformed by Babel. Many react-native npm modules unfortunately don't pre-compile their source code before publishing. I have react-navigation package installed in my app so I added this regex value in transformIgnorePatterns.

How does Jest recognize a test file?

The pattern Jest uses to detect test files. By default it looks for . js and . jsx files inside of __tests__ folders, as well as any files with a suffix of .


2 Answers

I use esm in my project. And config { type: "module" } in file package.json. So just change module.exports = to export default.

like image 103
Paprika.W Avatar answered Sep 23 '22 04:09

Paprika.W


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: {},
};
like image 43
Mout Pessemier Avatar answered Sep 20 '22 04:09

Mout Pessemier