できない.dev

Jest を 29 から 30 に上げると動かなくなること

Jest を 29 から 30 に上げると、toBeCalledWith などの別名のマッチャーと --testPathPattern が無くなり、window.location の差し替えが落ち、--ci を付けると 29 で撮ったスナップショットで止まる。
29 と 30 で打ち比べた 8 つの変更点と直し方をまとめる。

公開: 更新:

jest@29 と jest-environment-jsdom@29 を入れたプロジェクトと、同じファイルのまま両方を 30 に上げたプロジェクトを並べ、同じコマンドを打ち比べた。
各節の出力は「v29 では」「v30 に上げると」「v30 で直すと」の 3 段で並ぶ。

公式の移行ガイドによると、30 は Node.js 14・16・19・21 をサポートしなくなり、Jest の型定義を使うなら TypeScript 5.4 以上を求める。
CHANGELOG は、Node.js 23 も対象から外したと書いている。
npm の engines は ^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0 である。

jest-environment-jsdom は Jest 本体とは別のパッケージなので、jest と同じ 30 に揃えて入れる。
今回は jest-environment-jsdom 30.5.2 とともに jsdom 26.1.0 が入った。

動かなくなる原因は 4 つに分かれる。

  • 古い名前の API が消えた。
    公式の移行ガイドによると、別名のマッチャーは Jest 26 から非推奨だったが、29 は警告を出さずに通していた。
    jest.genMockFromModule と --testPathPattern も、新しい名前だけが残った。
  • jsdom が 20 から 26 に上がり、window.location を差し替えるテストが落ちる。
  • 29 で撮ったスナップショットは、中身が同じでも --ci を付けると先頭の行で止まる。
    付けなければ 30 が書き換える。
  • 既定の挙動が変わり、.mjs のファイルがテストとして拾われ、値を付けない --maxWorkers が止まる。

29 は警告を出さないので、上げる前にリポジトリの中を検索しておくと、30 で止まる場所が先に分かる。
別名のマッチャーは toBeCalled・lastCalledWith・nthCalledWith・toReturn・lastReturnedWith・nthReturnedWith・toThrowError( で見つかる。
ほかに genMockFromModule・testPathPattern・window.location も検索する。

jest --init の削除、toEqual などが列挙できないプロパティを比べなくなったこと、CalledWith 系の型の推論は、このページでは打っていない。
jest.mock のパスの大文字と小文字、--filter の戻り値の形、glob 10 への更新を含め、公式の移行ガイドに一覧がある。

toBeCalledWith などの別名のマッチャーが無くなった

Jest のマッチャーには、同じ働きに 2 つの名前が付いたものがあった。
toBeCalledWith は toHaveBeenCalledWith の別名で、toThrowError は toThrow の別名である。
30 で別名がまとめて消え、呼んだ行で TypeError になる。

v29 では

$ cat notify.test.js
const notify = require("./notify");
 
test("sends the message", () => {
  const send = jest.fn();
  notify(send, "hello");
  expect(send).toBeCalledWith("hello");
  expect(send).toBeCalledTimes(1);
});
 
test("rejects an empty message", () => {
  expect(() => notify(jest.fn(), "")).toThrowError("message is required");
});
$ npx jest
PASS ./notify.test.js
  ✓ sends the message (2 ms)
  ✓ rejects an empty message (2 ms)
 
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.185 s
Ran all test suites.
$ echo $?
0

v30 に上げると

$ npx jest
FAIL ./notify.test.js
  ✕ sends the message (3 ms)
  ✕ rejects an empty message
 
  ● sends the message
 
    TypeError: expect(...).toBeCalledWith is not a function
 
      4 |   const send = jest.fn();
      5 |   notify(send, "hello");
    > 6 |   expect(send).toBeCalledWith("hello");
        |                ^
      7 |   expect(send).toBeCalledTimes(1);
      8 | });
      9 |
 
      at Object.toBeCalledWith (notify.test.js:6:16)
 
  ● rejects an empty message
 
    TypeError: expect(...).toThrowError is not a function
 
       9 |
      10 | test("rejects an empty message", () => {
    > 11 |   expect(() => notify(jest.fn(), "")).toThrowError("message is required");
         |                                       ^
      12 | });
      13 |
 
      at Object.toThrowError (notify.test.js:11:39)
 
Test Suites: 1 failed, 1 total
Tests:       2 failed, 2 total
Snapshots:   0 total
Time:        0.219 s
Ran all test suites.
$ echo $?
1

v30 で直すと

$ sed -i -e s/toBeCalledWith/toHaveBeenCalledWith/ -e s/toBeCalledTimes/toHaveBeenCalledTimes/ -e s/toThrowError/toThrow/ notify.test.js
$ cat notify.test.js
const notify = require("./notify");
 
test("sends the message", () => {
  const send = jest.fn();
  notify(send, "hello");
  expect(send).toHaveBeenCalledWith("hello");
  expect(send).toHaveBeenCalledTimes(1);
});
 
test("rejects an empty message", () => {
  expect(() => notify(jest.fn(), "")).toThrow("message is required");
});
$ npx jest
PASS ./notify.test.js
  ✓ sends the message (2 ms)
  ✓ rejects an empty message (2 ms)
 
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.206 s, estimated 1 s
Ran all test suites.
$ echo $?
0

30 では 2 本とも落ちた。
1 本目は 6 行目の toBeCalledWith で止まり、7 行目の toBeCalledTimes までは進んでいない。
元の名前に書き換えると通る。
働きは同じで、変わるのは名前だけである。

消えた別名と置き換える名前は次の表のとおりで、公式の移行ガイドの表と同じである。
ESLint で eslint-plugin-jest を使っているなら、no-alias-methods のルールの自動修正でまとめて書き換えられる。

消えた名前置き換える名前
toBeCalledtoHaveBeenCalled
toBeCalledTimestoHaveBeenCalledTimes
toBeCalledWithtoHaveBeenCalledWith
lastCalledWithtoHaveBeenLastCalledWith
nthCalledWithtoHaveBeenNthCalledWith
toReturntoHaveReturned
toReturnTimestoHaveReturnedTimes
toReturnWithtoHaveReturnedWith
lastReturnedWithtoHaveLastReturnedWith
nthReturnedWithtoHaveNthReturnedWith
toThrowErrortoThrow

上の sed の s/toThrowError/toThrow/ は、toThrowErrorMatchingSnapshot の中にも当たる。
toThrowErrorMatchingSnapshot は 30 にも残っているマッチャーで、このページの「Error の cause と ArrayBuffer の中身がスナップショットに出るようになった」の節でも 30 で使っている。
ほかのファイルにかけるときは、s/toThrowError(/toThrow(/ のように括弧まで含めて置き換える。

--testPathPattern が --testPathPatterns に変わった

--testPathPattern は、テストファイルのパスを正規表現で絞り込むオプションである。
30 で名前の末尾に s が付き、--testPathPatterns になった。
古い名前を渡すと、Jest は設定の検査の段階で止まり、テストを 1 本も走らせない。

v29 では

$ npx jest --testPathPattern=unit
PASS src/unit/price.test.js
  ✓ adds tax (2 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.254 s
Ran all test suites matching /unit/i.
$ echo $?
0

v30 に上げると

$ npx jest --testPathPattern=unit
testPathPattern:
 
  Option "testPathPattern" was replaced by "--testPathPatterns". "--testPathPatterns" is only available as a command-line option.
  
  Please update your configuration.
 
  CLI Options Documentation:
  https://jestjs.io/docs/cli
$ echo $?
1

v30 で直すと

$ npx jest --testPathPatterns=unit
PASS src/unit/price.test.js
  ✓ adds tax (1 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.215 s
Ran all test suites matching unit.
$ echo $?
0

30 の案内のとおり、--testPathPatterns に直すと通る。
案内の後半は、--testPathPatterns がコマンドラインだけのオプションで、設定ファイルには書けないことを知らせている。
package.json の scripts や CI の設定に --testPathPattern を書いているなら、そこも直す。

公式の移行ガイドによると、--testPathPatterns にはパターンを空白で区切って複数渡せる。
オプションを繰り返して渡してもよい。
最後の行の表示も変わり、29 は /unit/i と正規表現の形で出していたが、30 は unit とそのまま出した。

jsdom 26 で window.location を差し替えられなくなった

jest-environment-jsdom 30 は、29 の jsdom 20 から上がった jsdom 26 を使う。
jsdom は仕様に合わせる変更を重ねており、26 の window.location は定義し直せないプロパティになっている。
テストの URL を決めるために Object.defineProperty で window.location を差し替えていたテストは、30 でその行が TypeError になる。

v29 では

$ cat query.test.js
/** @jest-environment jsdom */
const getUserId = require("./query");
 
test("reads id from the URL", () => {
  Object.defineProperty(window, "location", {
    value: new URL("https://example.com/users?id=1"),
    writable: true,
  });
  expect(getUserId()).toBe("1");
});
$ npx jest
PASS ./query.test.js
  ✓ reads id from the URL (2 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.374 s
Ran all test suites.
$ echo $?
0

v30 に上げると

$ npx jest
FAIL ./query.test.js
  ✕ reads id from the URL (2 ms)
 
  ● reads id from the URL
 
    TypeError: Cannot redefine property: location
        at Object.defineProperty (<anonymous>)
 
      3 |
      4 | test("reads id from the URL", () => {
    > 5 |   Object.defineProperty(window, "location", {
        |          ^
      6 |     value: new URL("https://example.com/users?id=1"),
      7 |     writable: true,
      8 |   });
 
      at Object.defineProperty (query.test.js:5:10)
 
Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.456 s
Ran all test suites.
$ echo $?
1

v30 で直すと

$ cat query.test.js
/** @jest-environment jsdom */
const getUserId = require("./query");
 
test("reads id from the URL", () => {
  window.history.replaceState({}, "", "/users?id=1");
  expect(getUserId()).toBe("1");
});
$ npx jest
PASS ./query.test.js
  ✓ reads id from the URL (4 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.344 s, estimated 1 s
Ran all test suites.
$ echo $?
0

URL のパスとクエリを変えるだけなら、window.history.replaceState で今のページの URL を書き換える。
直したテストは、window.location.search から id=1 を読めた。
ホスト名まで決めたいときは、公式の設定のドキュメントにある testEnvironmentOptions の url で、テストを始める URL を変える。

公式のブログは、jsdom が仕様に沿うよう変わったことで window.location のモックが壊れることを、既知の問題に挙げている。
対処としては、jsdom を元に独自のテスト環境を組むための @jest/environment-jsdom-abstract が Jest に入ったことと、jsdom に当てるパッチを案内している。
どちらもこのページでは打っていない。

スナップショットの先頭のリンクが変わった

スナップショットのファイルの 1 行目には、書式の版とガイドへのリンクが書かれている。
29 はリンクに https://goo.gl/fbAQLP を書いていた。
Google が goo.gl の短縮 URL を廃止したため、30 は https://jestjs.io/docs/snapshot-testing を書く。
30 は 1 行目も検査し、古いリンクのファイルを --ci で読むと、テストを走らせずに止まる。

v29 では

$ npx jest
PASS ./user.test.js
  ✓ user (4 ms)
 
 › 1 snapshot written.
Snapshot Summary
 › 1 snapshot written from 1 test suite.
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   1 written, 1 total
Time:        0.183 s
Ran all test suites.
$ echo $?
0
$ cat __snapshots__/user.test.js.snap
// Jest Snapshot v1, https://goo.gl/fbAQLP
 
exports[`user 1`] = `
{
  "id": 1,
  "name": "sato",
}
`;

v30 に上げると

$ npx jest --ci
FAIL ./user.test.js
  ● Test suite failed to run
 
    Outdated guide link: The snapshot guide link at the top of this snapshot is outdated. Please update all snapshots during this upgrade of Jest.
 
    Expected: https://jestjs.io/docs/snapshot-testing
    Received: https://goo.gl/fbAQLP
 
      at validateSnapshotHeader (node_modules/@jest/snapshot-utils/build/index.js:104:12)
 
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.123 s
Ran all test suites.
$ echo $?
1

v30 で直すと

$ npx jest -u
PASS ./user.test.js
  ✓ user (2 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   1 passed, 1 total
Time:        0.184 s
Ran all test suites.
$ echo $?
0
$ head -n 1 __snapshots__/user.test.js.snap
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
$ npx jest --ci
PASS ./user.test.js
  ✓ user (4 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   1 passed, 1 total
Time:        0.198 s, estimated 1 s
Ran all test suites.
$ echo $?
0

--ci を付けると、Jest はスナップショットのファイルを書き換えない。
どのファイルも 1 行目に同じリンクを持つので、29 で撮ったファイルをコミットしたままだと、--ci を付けて走らせる CI は、30 に上げた最初の実行で全部のファイルが止まる。
手元で npx jest -u を打つと 1 行目が書き換わり、--ci でも通るようになった。
書き換わったファイルはコミットしておく。

--ci を付けずに打つと、30 は古いリンクを黙って書き換える。
次の節の「v30 に上げると」の出力で、落ちたテストがあっても 1 行目が新しいリンクになっている。

Error の cause と ArrayBuffer の中身がスナップショットに出るようになった

30 は、スナップショットに書く値の表し方をいくつか変えた。
toThrowErrorMatchingSnapshot は、投げられた Error の cause もメッセージの下に書く。
ArrayBuffer は、29 では中身の無い ArrayBuffer [] と書かれていたが、30 は中のバイトを並べる。

v29 では

$ cat config.test.js
const loadError = () =>
  new Error("failed to load config", { cause: new Error("ENOENT: config.json") });
 
test("error object", () => {
  expect(loadError()).toMatchSnapshot();
});
 
test("thrown error", () => {
  expect(() => {
    throw loadError();
  }).toThrowErrorMatchingSnapshot();
});
 
test("buffer", () => {
  expect(new Uint8Array([1, 2]).buffer).toMatchSnapshot();
});
$ npx jest
PASS ./config.test.js
  ✓ error object (2 ms)
  ✓ thrown error
  ✓ buffer
 
 › 3 snapshots written.
Snapshot Summary
 › 3 snapshots written from 1 test suite.
 
Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   3 written, 3 total
Time:        0.183 s
Ran all test suites.
$ echo $?
0
$ cat __snapshots__/config.test.js.snap
// Jest Snapshot v1, https://goo.gl/fbAQLP
 
exports[`buffer 1`] = `ArrayBuffer []`;
 
exports[`error object 1`] = `[Error: failed to load config]`;
 
exports[`thrown error 1`] = `"failed to load config"`;

v30 に上げると

$ npx jest
FAIL ./config.test.js
  ✓ error object (2 ms)
  ✕ thrown error (2 ms)
  ✕ buffer (1 ms)
 
  ● thrown error
 
    expect(received).toThrowErrorMatchingSnapshot()
 
    Snapshot name: `thrown error 1`
 
    - Snapshot  - 0
    + Received  + 1
 
      failed to load config
    + Cause: ENOENT: config.json
 
       9 |   expect(() => {
      10 |     throw loadError();
    > 11 |   }).toThrowErrorMatchingSnapshot();
         |      ^
      12 | });
      13 |
      14 | test("buffer", () => {
 
      at Object.toThrowErrorMatchingSnapshot (config.test.js:11:6)
 
  ● buffer
 
    expect(received).toMatchSnapshot()
 
    Snapshot name: `buffer 1`
 
    - Snapshot  - 1
    + Received  + 4
 
    - ArrayBuffer []
    + ArrayBuffer [
    +   1,
    +   2,
    + ]
 
      13 |
      14 | test("buffer", () => {
    > 15 |   expect(new Uint8Array([1, 2]).buffer).toMatchSnapshot();
         |                                         ^
      16 | });
      17 |
 
      at Object.toMatchSnapshot (config.test.js:15:41)
 
 › 2 snapshots failed.
Snapshot Summary
 › 2 snapshots failed from 1 test suite. Inspect your code changes or run `npm run npx -- -u` to update them.
 
Test Suites: 1 failed, 1 total
Tests:       2 failed, 1 passed, 3 total
Snapshots:   2 failed, 1 passed, 3 total
Time:        0.245 s
Ran all test suites.
$ echo $?
1
$ head -n 1 __snapshots__/config.test.js.snap
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

v30 で直すと

$ npx jest -u
PASS ./config.test.js
  ✓ error object (2 ms)
  ✓ thrown error (1 ms)
  ✓ buffer
 
 › 2 snapshots updated.
Snapshot Summary
 › 2 snapshots updated from 1 test suite.
 
Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   2 updated, 1 passed, 3 total
Time:        0.136 s, estimated 1 s
Ran all test suites.
$ echo $?
0
$ cat __snapshots__/config.test.js.snap
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
 
exports[`buffer 1`] = `
ArrayBuffer [
  1,
  2,
]
`;
 
exports[`error object 1`] = `[Error: failed to load config]`;
 
exports[`thrown error 1`] = `
"failed to load config
Cause: ENOENT: config.json"
`;
$ npx jest --ci
PASS ./config.test.js
  ✓ error object (2 ms)
  ✓ thrown error
  ✓ buffer (1 ms)
 
Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   3 passed, 3 total
Time:        0.124 s, estimated 1 s
Ran all test suites.
$ echo $?
0

30 では 3 本のうち 2 本が落ちた。
expect(error).toMatchSnapshot() で撮った Error は 29 と同じ [Error: failed to load config] のままで、通っている。
公式の移行ガイドは Error を書くときに cause を含めるようになったと書いているが、今回の実測で cause が増えたのは toThrowErrorMatchingSnapshot だけだった。

差分を見て意図どおりなら、npx jest -u で撮り直す。
npx から打つと案内に npm run npx -- -u と出るが、打つのは npx jest -u でよい。

公式の移行ガイドによると、React の要素の子に置いた空の文字列もスナップショットに書かれなくなった(このページでは打っていない)。

既定の testMatch が .mjs のテストも拾うようになった

testMatch は、どのファイルをテストとして読むかを決める設定である。
30 で既定の値が広がり、.mjs・.cjs・.mts・.cts で終わるファイルもテストとして拾うようになった。
Node.js の組み込みのテストランナー node:test 用に *.test.mjs を置いているプロジェクトでは、Jest がそのファイルまで読みに行く。

v29 では

$ cat scripts/build.test.mjs
import test from "node:test";
import assert from "node:assert/strict";
 
test("build", () => {
  assert.equal(1 + 2, 3);
});
$ npx jest
PASS src/sum.test.js
  ✓ sum (2 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.194 s
Ran all test suites.
$ echo $?
0

v30 に上げると

$ npx jest
FAIL scripts/build.test.mjs
  ● Test suite failed to run
 
    Must use import to load ES Module: /work/v30/test-match-mjs/scripts/build.test.mjs
 
    The file contains ESM syntax (import/export) that could not be executed as CommonJS. Either:
      - Configure a transform (e.g. babel-jest) that compiles this file to CommonJS (see https://jestjs.io/docs/code-transformation)
      - If the file is in "node_modules", allow it to be transformed by adjusting "transformIgnorePatterns" (see https://jestjs.io/docs/configuration#transformignorepatterns-arraystring)
      - Use Node v24.9+ where Jest supports require(esm) natively (see https://jestjs.io/docs/ecmascript-modules#require-of-esm)
 
      at createRequireEsmError (node_modules/jest-runtime/build/index.js:452:17)
 
PASS src/sum.test.js
 
Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.31 s
Ran all test suites.
$ echo $?
1

v30 で直すと

$ cat jest.config.js
module.exports = {
  testMatch: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
};
$ npx jest
PASS src/sum.test.js
  ✓ sum (2 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.182 s
Ran all test suites.
$ echo $?
0

30 は scripts/build.test.mjs を CommonJS として読もうとして止まった。
案内の最後の項目は Node.js 24.9 以上なら読めると書いているが、今回は Node.js 24.22.2 で打ってもこのエラーで止まった。

設定の testMatch に 29 の既定の値を書くと、29 と同じ範囲に戻る。
直したあとの出力では、src/sum.test.js だけが走った。
特定のディレクトリだけを外すなら、testPathIgnorePatterns にそのディレクトリを足す書き方もある。

jest.genMockFromModule が無くなった

jest.genMockFromModule は、モジュールの中身を見て、関数を全部モックに置き換えたものを返す。
同じ働きの jest.createMockFromModule が後から入り、古い名前は 30 で消えた。

v29 では

$ cat api.test.js
const api = jest.genMockFromModule("./api");
 
test("fetchUser is mocked", () => {
  api.fetchUser.mockReturnValue({ id: 1, name: "mock" });
  expect(api.fetchUser(1).name).toBe("mock");
});
$ npx jest
PASS ./api.test.js
  ✓ fetchUser is mocked (2 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.175 s
Ran all test suites.
$ echo $?
0

v30 に上げると

$ npx jest
FAIL ./api.test.js
  ● Test suite failed to run
 
    TypeError: jest.genMockFromModule is not a function
 
    > 1 | const api = jest.genMockFromModule("./api");
        |                  ^
      2 |
      3 | test("fetchUser is mocked", () => {
      4 |   api.fetchUser.mockReturnValue({ id: 1, name: "mock" });
 
      at Object.genMockFromModule (api.test.js:1:18)
 
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.191 s
Ran all test suites.
$ echo $?
1

v30 で直すと

$ sed -i s/genMockFromModule/createMockFromModule/ api.test.js
$ cat api.test.js
const api = jest.createMockFromModule("./api");
 
test("fetchUser is mocked", () => {
  api.fetchUser.mockReturnValue({ id: 1, name: "mock" });
  expect(api.fetchUser(1).name).toBe("mock");
});
$ npx jest
PASS ./api.test.js
  ✓ fetchUser is mocked (1 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.187 s
Ran all test suites.
$ echo $?
0

この例はファイルの先頭で呼んでいるので、読み込む段階で止まり、そのファイルのテストは 1 本も走らなかった。
テストの中で呼んでいる場合は打っていない。
名前を createMockFromModule に変えると、29 と同じモックが返って通った。

値の要るオプションを値なしで渡すと止まるようになった

30 は、値を取るオプションに値が付いているかを検査する。
29 は --maxWorkers だけを書いても、エラーを出さずにテストを走らせていた。
30 はヘルプを全部出したあと、最後の行に値の足りないオプションの名前を出して止まる。

v29 では

$ npx jest --maxWorkers
PASS ./a.test.js
  ✓ a (1 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.162 s
Ran all test suites.
$ echo $?
0

v30 に上げると

$ npx jest --maxWorkers > jest.log 2>&1
$ echo $?
1
$ tail -n 3 jest.log
Documentation: https://jestjs.io/docs/cli
 
Not enough arguments following: maxWorkers

v30 で直すと

$ npx jest --maxWorkers=50%
PASS ./a.test.js
  ✓ a (2 ms)
 
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.189 s
Ran all test suites.
$ echo $?
0

ヘルプが長いので、30 の出力はファイルに受けて最後の 3 行だけを見せた。
--maxWorkers=50% のように値を付けると通る。
公式の移行ガイドは、同じ扱いになるオプションの例に --selectProjects も挙げている。
package.json の scripts に、値を付けないまま書いたオプションが残っていないかを見ておく。

検証環境Node.js 24.22.2 / npm 11.19.1 / Python 3.11.2 / 2026-11-07 検証(ほか 7 件)
検証日
実行環境
node:24 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 24.22.2
  • npm 11.19.1
  • Python 3.11.2
  • Git 2.39.5
  • Jest(旧) 29.7.0
  • Jest(新) 30.5.2
  • jest-environment-jsdom(旧) 29.7.0
  • jest-environment-jsdom(新) 30.5.2
  • jsdom(旧) 20.0.3
  • jsdom(新) 26.1.0

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。