0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Jestでuuidを固定してテストしたい

Posted at

はじめに

Jestでuuidを含むテストコードを書く際に、uuidが毎回違う値が生成されてしまうため、固定する必要があったのでその方法を整理しました。

結論

以下コードにより、uuidを固定してテストができます。
以下の例だと、uuidを123と固定してテストを実施できます。

import * as uuid from "uuid"; 
jest.mock("uuid"); 
describe("someTest", () => {
   const uuidSpy = jest.spyOn(uuid, "v4");
   uuidSpy.mockReturnValue("123");
}

参考

https://dev.classmethod.jp/articles/mocking-uuid-generation-in-jest/
https://stackoverflow.com/questions/51383177/how-to-mock-uuid-with-jest

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?