-1

I'm encountering an error when trying to run a test with Jest in a project that uses Pulumi.

Here's the error stack:

Cannot find module 'spdx-license-ids' from '../../node_modules/spdx-expression-parse/scan.js'

  at Resolver._throwModNotFoundError (../../node_modules/jest-> > >resolve/build/resolver.js:427:11)  
  at Object.<anonymous> (../../node_modules/spdx-expression-parse/scan.js:4:11)  
  at Object.<anonymous> (../../node_modules/spdx-expression-parse/index.js:3:12)  
  at Object.<anonymous> (../../node_modules/validate-npm-package-license/index.js:1:177)  
  at Object.<anonymous> (../../node_modules/normalize-package-data/lib/fixer.js:3:23)  
  at Object.<anonymous> (../../node_modules/normalize-package-data/lib/normalize.js:3:13)  
  at Object.<anonymous> (../../node_modules/@pulumi/runtime/closure/codePaths.ts:21:1)  
  at Object.<anonymous> (../../node_modules/@pulumi/runtime/index.ts:22:1)  
  at Object.<anonymous> (../../node_modules/@pulumi/index.ts:31:1)  
  at Object.<anonymous> (src/lib/infrastructure.spec.ts:1:1)  

  1 | import * as pulumi from "@pulumi/pulumi";
    | ^

jest.config.ts

export default {
  displayName: 'infrastructure',
  preset: '../../jest.preset.js',
  testEnvironment: 'node',
  transform: {
    '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
  },
  moduleFileExtensions: ['ts', 'js', 'html', 'json'],
  coverageDirectory: '../../coverage/packages/infrastructure',
};

Details:

  • Node.js Version: 20.9.0
  • NPM Version: 10.1.0
  • Jest Version: 29.7.0
  • Pulumi Version: 3.146.0
  • NX version: 20.1.4

What I've Tried:

  • I've deleted node_modules and package-lock.json, then ran npm install.
  • I've tried to explicitly install spdx-license-ids with npm install spdx-license-ids --save.

Question: What could be the cause of this error? How can I fix it?

1 Answer 1

0

The problem is solved by adding json to the moduleFileExtensions array in jest.config.ts.

moduleFileExtensions: ['ts', 'js', 'html', 'json']

The problem is caused because spdx-license-ids exports a json file, which jest didn't recognize as a valid module.

spdx-license-ids is used by spdx-expression-parse.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.