-
Notifications
You must be signed in to change notification settings - Fork 147
How to stub dependencies with esm? #712
Comments
Hi @jzaefferer! A reduced or small repro repo would help. The |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@jzaefferer just gave it a try, not sure if you tried to do something like this: // index.js
import { stub } from "sinon"
import * as util from "./util.js"
stub(util, "getPricing").returns(Promise.resolve("$2"))
util.getPricing().then(val => console.log(val)) // util.js
export function getPricing() {
return Promise.resolve("$1")
} node -r esm index.js # output: $2 ps: make sure you're using the |
@jzaefferer Feel free to ping back with a repro and we will dig in. In the meantime @dnalborczyk would you please open a PR to add a scenario test for Sinon so we ensure we don't regress this stub/spy case. |
@jdalton yeah, sure, I'll have a look ... |
Hello @jdalton! Thanks for looking into this in the first place. I've finally managed to put together a reduced test case: https://github.com/jzaefferer/esm-mock-repro I suspect there's a silly mistake in there in the way I'm requiring/importing modules, though it did work fine pre-esm (with babel). |
Hi @jzaefferer! Thank you for the repro ❤️. You've got a mix of things going on here. You don't need your common.jsimport chai, { expect } from 'chai'
import sinon from "sinon"
import * as util from "../src/util"
global.chai = chai
global.expect = expect
global.sinon = sinon
sinon.stub(util, 'getPricing').returns(Promise.resolve({
plans: ['a', 'b', 'c']
})) then const x = await util.getPricing()
expect(x).deep.equal({ plans: ['a', 'b', 'c'] }) passes. The issue is that var util = require('../src/util') Is not the namespace object of import * as util from "../src/util" so if you stub the namespace object, the namespace object is stubbed. If you stub the exports then that's something else. |
@jzaefferer did you look at any of the examples above? import sinon from "sinon";
import { expect } from "chai";
import * as util from "../src/util";
describe("lib", () => {
it("tests stuff", async () => {
sinon.stub(util, "getPricing").returns(Promise.resolve(["a", "b", "c"]));
const x = await util.getPricing();
expect(x).deep.equal(["a", "b", "c"]);
util.getPricing.restore();
const y = await util.getPricing();
expect(y).deep.equal(["ABC", "FOO", "BAR"]);
});
}); usually you would use either the cli options or the bridge (code), not both. you can also install |
Thanks, that makes sense. For some reason I missed that @dnalborczyk I did! But it didn't (yet) help spot the issue with my common.js file. Also you're right that the lack of any reference to |
👋 @jzaefferer! I've been thinking about this and your case will be common. With a couple lines of code I can make the namespace object be returned from Update: I added support for it. 😌 |
Nice! I'm glad this contributed a little bit to the project :-) |
This is primarily a support request, but might turn into an enhancement for docs!
I'm trying to migrate from babel to esm, and I'm currently stuck trying to make some mocks work again.
What I'm starting with is some common setup code for mocha-based tests, like this:
As far as I can tell, that stub silently fails, since namespace objects are immutable.
Which I figured setting
cjs.mutableNamespace
totrue
would address, but I can't figure out how to do that. I did add this to my package.json:But then I get a new error that makes no sense to me:
I suspect that I'm removing all
cjs
defaults by only setting the one option. But without examples for setting a single option and with no docs for the default values, I haven't been able to address that. I looked atesm/src/package.js
Lines 64 to 97 in f15dfc7
mode
option makes this confusing, too - ismode: auto
the default or not?If a reduced test case has any value here, let me know and I'll put something together.
The text was updated successfully, but these errors were encountered: