Skip to content
Advertisement

Stubbing uuid with sinon

So I’m updating dependencies on my project and I’ve run into a snag…

My unit tests were working perfectly with the below stub. However in the latest version of UUID, this seemingly has broken. Any suggestions on how to fix it?

These are simplistic extracts from the code to illustrate the method I’m using to stub the functionality of uuid and how I am using uuid in my code.

import * as uuid from 'uuid'

sinon.stub(uuid, 'v4').returns('some-v4-uuid')
import * as uuid from 'uuid'

const payload = {
  id: uuid.v4()
}

The dependency versions

  • “uuid”: “7.0.1”
  • “sinon”: “9.0.0”

Here is the code

Here is the test

Answer

Given the uuid@7 dist uses Object.defineProperty to export the versions, I don’t think stubbing is possible. This is annoying but you might have to put an abstraction layer on top of uuid and stub that function.

Advertisement