I'm updating NodeJS and dependencies versions and got a problem trying to update uuid from v3.1.0 to v9.0.0 on my tests.
This was working before the update, but not anymore.
sinon = require('sinon'),
uuid = require('uuid'),
before(async () => { uuidV4Stub = sinon.stub(uuid, 'v4').returns('RANDOM_ID');
});I read the uuid npm page and found:
As of uuid@7 this library now provides ECMAScript modules builds, which allow packagers like Webpack and Rollup to do "tree-shaking" to remove dead code. Instead, use the import syntax:
import { v4 as uuidv4 } from 'uuid'; uuidv4(); ... or for CommonJS:
const { v4: uuidv4 } = require('uuid'); uuidv4();
Default Export Removed
Is it still possible to stub it somehow?
1 Answer
Heyo,
I've been there when I tried to alter uuid library
take this example and let's see how to it goes with you:
const sinon = require('sinon')
const {v4: uuidv4} = require('uuid')
let uuidV4stub = null;
before(async () => {
uuidV4stub = sinon .stub(uuidv4, 'name').value(() => 'ANY_RANDOM_ID');
});
after(() => {
uuidV4stub.restore();
});so here is the deal, grab that v4 thingy with some fancy de-structuring and call it uuidv4 just to keep things slick. Old school stubbing isn't gonna cut it cause its a constant now, so switch it up and use sinon.stub().value() to swap it out and don't space out and forget to reset that stub after your tests or things might go sideways