2

I have a jest spyOn with the uuid which was working in the version 3.4.0 after upgrading it to 8.3.2 the test breaks with the error Cannot spyOn on a primitive value; undefined given

import uuid from 'uuid';
jest.spyOn(uuid, 'v4');
vignesh
  • 51
  • 4

2 Answers2

2

The best way to mock uuid lib version 8.x is using Mocking Partials:

jest.mock( 'uuid', () => ({
  v4: jest.fn(() => '1234567890' )
}));
0

You need to change the way you import uuid

import { v4 as uuidv4 ) from uuid;

https://github.com/uuidjs/uuid#readme

drub4n
  • 148
  • 8
  • spyOn cannot be used directly for a method – vignesh Apr 22 '21 at 06:14
  • My bad, I usually use jest.mock instead of jest.spyOn to work with uuid in tests, you can find relevant information on uuid mocks here https://stackoverflow.com/questions/51383177/how-to-mock-uuid-with-jest – drub4n Apr 22 '21 at 06:40