Proxyquire failing to replace simple module
Closed this issue · 1 comments
shaynemeyer commented
`
// HelloWorld.js
function HelloWorld() {
return 'Hello World';
}
module.exports = HelloWorld;
`
`
// TestProxyquire..spec.js
/* eslint-env node, mocha */
'use strict';
/* eslint-env node, mocha */
const proxyquire = require('proxyquire').noPreserveCache();
const {expect} = require('chai');
describe('Proxyquire', () => {
beforeEach(() => {
proxyquire('./HelloWorld', {
HelloWorld: function() {
return 'Fake Hello World';
},
'@noCallThru': true
});
});
it('should ', () => {
const HelloWorld = require('./HelloWorld');
const results = HelloWorld();
expect(results).to.be.equal('Fake Hello World');
});
});
`
This test fails.
AssertionError: expected 'Hello World' to equal 'Fake Hello World' Expected :Fake Hello World Actual :Hello World
bendrucker commented
Hi, this is not how proxyquire works. You use the return value of proxyquire
, effectively replacing require
. It is not meant to set up mocks for future require calls. Also, the stubs
keys (second arg) are module paths, not identifiers from your code.