Closed5

jest(+ts-jest) で Pure ESM package を扱うときのメモ

odanodan

jest(+ts-jest) で Pure ESM package を使うとエラーになるので、その時の対応をメモ

テストコード

import ky from "ky";

describe("app", (): void => {
  describe("add", (): void => {
    it("should be correct", (): void => {
      expect.assertions(1);
      expect(32 + 10).toBe(42);
      console.log({ ky });
    });
  });
});

jest.config.js

jest.config.js
module.exports = {
  preset: "ts-jest",
  collectCoverage: true,
  coverageReporters: ["json", "lcov", "text", "html"],
  transform: {
    "^.+\\.tsx?$": "esbuild-jest",
  },
};

この状態で jest コマンドを実行すると次のエラーが発生する

 FAIL  src/app.spec.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/odan/source/github.com/odan-sandbox/node-cjs-interop-ts-jest-sandbox/node_modules/ky/distribution/index.js:2
    import { Ky } from './core/Ky.js';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module



      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (src/app.spec.ts:19:25)
odanodan

方針
ts-jest に .ts ファイルと同様に .js ファイルもトランスパイルしてもらう

jest.config.js

node_moduels 以下のファイルはデフォルトではトランスパイルされないので transformIgnorePatterns で Pure ESM package を指定する必要がある

jest.config.js
module.exports = {
  preset: "ts-jest/presets/js-with-ts",
  collectCoverage: true,
  coverageReporters: ["json", "lcov", "text", "html"],
  transformIgnorePatterns: ["/node_modules/(?!(ky))"],
  transform: {
    "^.+\\.tsx?$": "esbuild-jest",
  },
  globals: {
    "ts-jest": {
      useESM: true,
    },
  },
};

tsconfig.json"allowJs": true を有効にする必要がある
理由は ts-jest/presets/js-with-ts で求められているため

You'll need to set allowJs to true in your tsconfig.json file.
https://kulshekhar.github.io/ts-jest/docs/getting-started/presets

tsconfig.build.json
{
  "extends": "@tsconfig/node16/tsconfig.json",
  "compilerOptions": {
    "outDir": "dist",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "allowJs": true
  },
  "include": ["./src/**/*.ts"],
  "exclude": ["./src/**/*.spec.ts"]
}
このスクラップは2022/02/21にクローズされました