Skip to content

Commit

Permalink
chore: add test for #140
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Nov 13, 2022
1 parent f35ec8e commit f5a0ef7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
7 changes: 7 additions & 0 deletions _tests/issues/140/A.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { func } = require('./B')

const execFunc = () => {
func();
}

module.exports = { execFunc }
5 changes: 5 additions & 0 deletions _tests/issues/140/B.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const func = () => {
console.log("This is not the stubbed function");
}

module.exports = { func };
19 changes: 19 additions & 0 deletions _tests/issues/140/test.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import rewiremock from '../../../node';


describe('rewiremock issue 140', () => {
it('stub function with rewiremock', () => {

const mockFunc = () => {
console.log('This is the stubbed function')
};

const subject = rewiremock.proxy(
() => require('./A'),
() => {
rewiremock(() => require('./B'))
.with({ func: mockFunc })
});
subject.execFunc();
})
})
3 changes: 1 addition & 2 deletions _tests/typedImport.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ describe('typed import ', () => {
it('should throw: ', () => {
rewiremock.inScope(() => {
rewiremock(() => import('./lib/typed/b.js')).with(() => 42);
rewiremock.enable();
expect(() => require('./lib/typed/a.js')).to.throw();
expect(() => rewiremock.enable()).to.throw();
rewiremock.disable();
});
});
Expand Down
4 changes: 3 additions & 1 deletion src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const NodeModule = {
probeSyncModules() {
const load = Module._load;
Module._load = probeAsyncModules.load(this);
probeAsyncModules.execute(() => Promise.reject('could not use dynamic imports with sync API'));
probeAsyncModules.execute(() => {
throw new Error('could not use dynamic imports with sync API')
});
Module._load = load;
},

Expand Down

0 comments on commit f5a0ef7

Please sign in to comment.