Closed4

ESMなTypeScriptプロジェクトでJestを使う

ピン留めされたアイテム
Shingo YamazakiShingo Yamazaki
  • package.json で "type": "module" を指定している
  • TypeScript を使用している

というプロジェクトでjestを使うための設定。

結論としては jest.config.js を以下のように書けば動いた。

export default {
  preset: "ts-jest/presets/default-esm",
  globals: {
    "ts-jest": {
      useESM: true,
    },
  },
};
Shingo YamazakiShingo Yamazaki

ts-jestの
ESM Support | ts-jest
を読むと、冒頭にJestの
ECMAScript Modules · Jest
をまず読めと書いてるので読む。

その後 ts-jest の方に戻ってきて、書いてあるとおりに jest.config.js を以下のようにしたところ

module.exports = {
  preset: "ts-jest/presets/default-esm",
  globals: {
    "ts-jest": {
      useESM: true,
    },
  },
};

このエラー。

$ yarn test
ReferenceError: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/yamazaki/repos/github.com/zaki-yama/foo/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///Users/yamazaki/repos/github.com/zaki-yama/foo/jest.config.js:1:1
    at ModuleJob.run (internal/modules/esm/module_job.js:169:25)
    at async Loader.import (internal/modules/esm/loader.js:177:24)
    at async requireOrImportModule (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/jest-util/build/requireOrImportModule.js:65:32)
    at async readConfigFileAndSetRootDir (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:109:22)
    at async readConfig (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/jest-config/build/index.js:227:18)
    at async readConfigs (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/jest-config/build/index.js:412:26)
    at async runCLI (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/@jest/core/build/cli/index.js:220:59)
    at async Object.run (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/jest-cli/build/cli/index.js:163:37)
e

原因は jest.config.js 自体も ESM として扱われるからで、次のようにするのが正しそう。

-module.exports = {
+export default {
   preset: "ts-jest/presets/default-esm",
   globals: {
     "ts-jest": {
  ...
Shingo YamazakiShingo Yamazaki

余談だが、
Jestの設定 · Jest
を読むと jest.config.ts というように拡張子 .ts でも認識してくれるらしいが、↑の修正後のファイルを拡張子変えただけだと以下のエラー。

$ jest --rootDir src
Error: Jest: Failed to parse the TypeScript config file /Users/yamazaki/repos/github.com/zaki-yama/foo/jest.config.ts
  Error: Must use import to load ES Module: /Users/yamazaki/repos/github.com/zaki-yama/foo/jest.config.ts
require() of ES modules is not supported.
require() of /Users/yamazaki/repos/github.com/zaki-yama/foo/jest.config.ts from /Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/jest-config/build/readConfigFileAndSetRootDir.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users/yamazaki/repos/github.com/zaki-yama/foo/package.json.

    at readConfigFileAndSetRootDir (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:118:13)
    at readConfig (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/jest-config/build/index.js:227:18)
    at readConfigs (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/jest-config/build/index.js:412:26)
    at runCLI (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/@jest/core/build/cli/index.js:220:59)
    at Object.run (/Users/yamazaki/repos/github.com/zaki-yama/foo/node_modules/jest-cli/build/cli/index.js:163:37)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

これは原因と解決策わからず。

このスクラップは2021/07/26にクローズされました