LoginSignup
0
0

Jest.useFakeTimersとPrisma.findUniqueを組み合わせるとテストが止まる問題の解決

Posted at

概要

Jestを使ったテストでJest.useFakeTimersでタイマーモックを設定した上でPrisma.findUniqueを使うと、何も返って来ずテストが進まないという現象が発生した。
何故解決するのか完全に理解はしていないが、その解決方法の備忘メモ。

環境

  • typescript: 4.2.x
  • jest: 29.1.x
  • prisma: 3.15.x

結論

jest.useFakeTimersのConfigでnextTickをモック対象外に指定することで解決。

解決前の設定
jest.useFakeTimers({
  now: new Date('2023-01-01 00:00:00'),
});
解決後の設定
jest.useFakeTimers({
+  doNotFake: ['nextTick'],
  now: new Date('2023-01-01 00:00:00'),
});

備考

Prisma.findUniqueではなくPrisma.findFirstであれば、nextTickをモック対象外に指定せずともテストが問題なく走った。

参考URL

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0