Error

Test server › should return OK on /health

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

    Require stack:
      /Users/duc.nguyen/Workspace/sample-project/node_modules/spdx-expression-parse/scan.js
      /Users/duc.nguyen/Workspace/sample-project/node_modules/spdx-expression-parse/index.js
      /Users/duc.nguyen/Workspace/sample-project/node_modules/validate-npm-package-license/index.js
      /Users/duc.nguyen/Workspace/sample-project/node_modules/normalize-package-data/lib/fixer.js
      /Users/duc.nguyen/Workspace/sample-project/node_modules/normalize-package-data/lib/normalize.js
      /Users/duc.nguyen/Workspace/sample-project/node_modules/read-pkg-up/node_modules/read-pkg/index.js
      /Users/duc.nguyen/Workspace/sample-project/node_modules/read-pkg-up/index.js
      /Users/duc.nguyen/Workspace/sample-project/node_modules/koa2-swagger-ui/dist/index.js
      src/server.ts
      src/server.test.ts

      at Resolver.resolveModule (../../node_modules/jest-resolve/build/resolver.js:311:11)
      at Object.<anonymous> (../../node_modules/spdx-expression-parse/scan.js:4:11)

How to fix

Current jest.config.json

module.exports = {
	//...rest
	moduleFileExtensions: ["ts", "js", "html"],
};

We need to add json:

module.exports = {
	//...rest
	moduleFileExtensions: ["ts", "js", "html", "json"],
};

Reason

The package that failed is spdx-license-ids, which uses JSON files internally. Jest only reads files with extensions specified in moduleFileExtensions. Because json was missing, Jest could not read the JSON files inside spdx-license-ids and threw an error.

By the way, the default value of moduleFileExtensions in Jest is:

["js", "json", "jsx", "ts", "tsx", "node"]

So this error wouldn’t have occurred if I hadn’t customized the extensions in the first place 😇

Source: https://qiita.com/nekoshita_yuki/items/b20b76cad37a6c9748b8