Closed3

ReferenceError: jest is not defined

Hide(ひで)Hide(ひで)

https://www.prisma.io/docs/orm/prisma-client/testing/unit-testing#singleton の通り設定してみたがテストを実行すると

hide@hidenoMacBook-Pro issuer-worker % yarn test
yarn run v1.22.19
$ NODE_OPTIONS=--experimental-vm-modules jest
 FAIL  __tests__/server/models/skill-credential.ts
  ● Test suite failed to run

    ReferenceError: jest is not defined

      4 | import { prisma } from "@/server/db/client";
      5 |
    > 6 | jest.mock("@/server/db/client", () => ({
        | ^
      7 |   __esModule: true,
      8 |   default: mockDeep<PrismaClient>(),
      9 | }));

      at singleton.ts:6:1

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.806 s
Ran all test suites.
(node:66893) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Hide(ひで)Hide(ひで)

https://japanese-document.github.io/tips/2023/javascript-jest-is-not-defined.html

を参考に。

singleton.ts
import { jest } from "@jest/globals";
import { PrismaClient } from "@prisma/client";
import { DeepMockProxy, mockDeep, mockReset } from "jest-mock-extended";

import { prisma } from "@/server/db/client";

jest.mock("@/server/db/client", () => ({
  __esModule: true,
  default: mockDeep<PrismaClient>(),
}));

beforeEach(() => {
  mockReset(prismaMock);
});

export const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>;

すると以下のエラーに変わった。


    TypeError: Cannot read properties of undefined (reading '_isMockObject')

      11 |
      12 | beforeEach(() => {
    > 13 |   mockReset(prismaMock);
         |   ^
      14 | });
      15 |
      16 | export const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>;

      at mockReset (../../node_modules/jest-mock-extended/lib/Mock.js:39:23)
      at Object.<anonymous> (singleton.ts:13:3)
このスクラップは2023/12/19にクローズされました