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.

v7 methods cannot be stubbed

See original GitHub issue

Our unit tests that use uuid stopped working. It seems that sandbox.stub(uuid, 'v4') no longer works.

const uuid = require('uuid');
const sinon = require('sinon');

const sandbox = sinon.createSandbox();
sandbox.stub(uuid, 'v4').returns('uuid');

console.log(uuid.v4());
// prints some uuidv4 instead of 'uuid'

It seems to be related to the way defineProperty is being used. Not sure if there are other implications of replacing the defineProperty by simply setting the property, but switching this:

"use strict";

Object.defineProperty(exports, "v4", {
  enumerable: true,
  get: function () {
    return _v3.default;
  }
});

var _v3 = _interopRequireDefault(require("./v4.js"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

to the following works.

"use strict";

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _v3 = _interopRequireDefault(require("./v4.js"));

exports.v4 = _v3.default;

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
ctavancommented, Feb 24, 2020

As @broofa already explained we have migrated this library to use ESModules and per the spec ESModule exports cannot be dynamically modified, so in fact everything behaves as expected. Obviously this new behavior differs from the way things behaved with CommonJS exports with the downside of not being as easily mockable as before.

I found these discussions provided some helpful context:

As suggested above, if you really want to continue to mock the uuid module you are left with building a wrapper or using something like https://github.com/speedskater/babel-plugin-rewire#readme.

If all you want to achieve is a static output you could also try to mock the global crypto.randomBytes() method from the node crypto library or crypto.getRandomValues() for the browser.

Or you could provide your own random number generator as described in https://github.com/uuidjs/uuid#version-4-random and done in this test.

@CareLuLu-Gabriel with respect to your specific question: Looking forward I don’t think that modules should continue to be mockable in the way they were in CommonJS times so I’m reluctant on going one step back here. I assume that ESModules will become more and more common in Node.js-land pretty soon so we’ll see these mockability issues arise for many other modules as well. I think we’ll need other solutions than just sticking with CommonJS behavior.

2reactions
azizurcommented, Apr 11, 2020

I have found this works really nice.

At the top of test file:

// your regular imports here

jest.mock('uuid', () => ({ v4: () => '00000000-0000-0000-0000-000000000000' }));

// describe('test suite here', () => {

in the actual code:

import { v4 as uuidv4 } from 'uuid';
Read more comments on GitHub >

github_iconTop Results From Across the Web

java - MockitoException - is a *void method* and it *cannot* be ...
Voids are usually stubbed with Throwables: doThrow(exception).when(mock).someVoidMethod(); *** If you're unsure why you're getting above error ...
Read more >
"void method cannot be stubbed" error when irrelevant ...
MockitoException: 'callProt' is a void method and it cannot be stubbed with a return value! Voids are usually stubbed with Throwables:
Read more >
PI11816: CLASS CAST EXCEPTION, EJB STUB CANNOT BE ...
A ClassCastException occurs within a generated stub method:
Read more >
Re: [mockito] Problems with programming a mock with when()
Now you noticed that the message says that void method cannot be stubbed with a return value, that is because the stubbing syntax...
Read more >
stubby4j
HTTP/1.1, HTTP/2 and WebSockets stub server for stubbing distributed web services in Docker and non-containerized environments for contract testing.
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