question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I stub uuid

See original GitHub issue

How can I stub function uuid v4 if imported like import * as uuid from 'uuid/v4'?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
djungowskicommented, Feb 7, 2022

You can do this with Jasmine:

import * as uuid from 'uuid';
...
spyOnProperty(uuid, 'v4').and.returnValue(() => 'f3b3a923-61b3-4d6f-9a7e-632061825358');

This does not work anymore, since it will produce the error

Error: <spyOnProperty> : v4 is not declared configurable

The same applies for spyOn. The way to go is to write some kind of service for your application that uses v4 that you can then mock

0reactions
roobycommented, Apr 21, 2021

You can do this with Jasmine:

import * as uuid from 'uuid';
...
spyOnProperty(uuid, 'v4').and.returnValue(() => 'f3b3a923-61b3-4d6f-9a7e-632061825358');
Read more comments on GitHub >

github_iconTop Results From Across the Web

Stubbing uuid with sinon - Stack Overflow
My unit tests were working perfectly with the below stub. However in the latest version of UUID, this seemingly has broken. Any suggestions...
Read more >
An example of how I was using a sinon stub to mock creating ...
An example of how I was using a sinon stub to mock creating uuids - create_stub_example.js. ... import uuid from 'node-uuid';. import sinon...
Read more >
How To Mock uuid In Jest - I Like Kill Nerds
We have a method called returnUuid which calls the function and returns the generated guid; Inside of our test file, we mock the...
Read more >
mock-uuid - npm
Repeatable and predictable mock UUID generator for testing purposes. ... Start using mock-uuid in your project by running `npm i mock-uuid`.
Read more >
JavaScript uuid v4 Examples
JavaScript v4 - 13 examples found. These are the top rated real world JavaScript examples of uuid.v4 extracted from open source projects.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found