I'm using the proxyquire library, which mocks packages on import.
I'm creating my own proxyquire function, which stubs a variety of packages I use regularly and want to stub regularly (meteor packages, which have a special import syntax):
// myProxyquire.js
import proxyquire from 'proxyquire';
const importedStubs = {
'meteor/meteor': { Meteor: { defer: () => {} } },
};
const myProxyquire = filePath => proxyquire(filePath, importedStubs);
export default myProxyquire;
Now I want to write a test of a file which uses one of these packages:
// src/myFunc.js
import { Meteor } from 'meteor/meteor'; // This import should be stubbed
export const myFunc = () => {
Meteor.defer(() => console.log('hi')); // This call should be stubbed
return 'test';
};
And finally I test it like this:
// src/myFunc.test.js
import myProxyquire from '../myProxyquire';
const { myFunc } = myProxyquire('./myFunc'); // error: ENOENT: no such file
describe('myFunc', () => {
it("should return 'test'", () => {
expect(myFunc()).to.equal('test');
});
});
Note that my last 2 files are nested inside a subfolder src
. So when I try to run this test, I get an error saying that the module ./myFunc
couldn't be found, as it is being looked for in the "root" directory, where the myProxyquire.js
file is, not the src
directory as expected.
Aucun commentaire:
Enregistrer un commentaire