Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stub(obj, method).callFake() not work when method is defined as Function #1877

Closed
ShanaMaid opened this issue Aug 9, 2018 · 3 comments
Closed
Labels

Comments

@ShanaMaid
Copy link

ShanaMaid commented Aug 9, 2018

Describe the bug
I have a module

modules.js

export async function test () {
    return 1;
}

test.js

import * as Module from 'modules.js';
sinon.createSandbox().stub(Module, 'test').callFakce(() =>  2);

Module.test() // 1 .    it's incorrect!

but if do this

modules.js

export const test = async () => {
    return 1;
}

test.js

import * as Module from 'modules.js';
sinon.createSandbox().stub(Module, 'test').callFakce(() =>  2);

Module.test() // 2 .    it's correct!
@stale
Copy link

stale bot commented Oct 8, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 8, 2018
@fatso83
Copy link
Contributor

fatso83 commented Oct 9, 2018

This has nothing to do with Sinon. This has to do with module loaders and the details of which are outside of Sinon's hands. If you are using ES Modules (which you are when using import) then they are not supposed to be modifiable! Hence Sinon cannot change them. If you transpile using Babel to ES5 they will be trivially stubbable, or if you use ESM with CJS compat settings that should also work.

This does work fine in my tests when using the esm loader and the CJS compatibility settings. Check that out.
See https://github.com/fatso83/sinon-issues/compare/sinon-1877-esm-cjs-compat?expand=1
See https://github.com/fatso83/sinon-issues/compare/sinon-1877-esm-no-cjs-compat?expand=1

@fatso83 fatso83 closed this as completed Oct 9, 2018
@ShanaMaid
Copy link
Author

@fatso83 thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants