由于某种原因proxquire
找不到我的模块index.js
。我一定是在做一些非常愚蠢或明显的事情,而且看不到。
错误:
invoicer
good request
1) "before all" hook
0 passing (9ms)
1 failing
1) invoicer good request "before all" hook:
Error: Cannot find module '../index.js'
at Function.Module._resolveFilename (module.js:338:15)
at Proxyquire._disableModuleCache (/Users/mario/projects/_invoice/print-invoice/node_modules/proxyquire/lib/proxyquire.js:218:19)
at Proxyquire._disableCache (/Users/mario/projects/_invoice/print-invoice/node_modules/proxyquire/lib/proxyquire.js:203:15)
at Proxyquire._withoutCache (/Users/mario/projects/_invoice/print-invoice/node_modules/proxyquire/lib/proxyquire.js:173:27)
at Proxyquire.load (/Users/mario/projects/_invoice/print-invoice/node_modules/proxyquire/lib/proxyquire.js:135:15)
at getTestedModule (/Users/mario/projects/_invoice/print-invoice/test/test.js:89:12)
at Context.<anonymous> (/Users/mario/projects/_invoice/print-invoice/test/test.js:69:28)
at Hook.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:218:15)
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:259:10)
at Object._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)
at processImmediate [as _immediateCallback] (timers.js:345:15)
奇怪的是,我为另一个应用程序的测试设置了相同的设置,并且在那里找到模块没有问题。我什至维护了相同的版本proxyquire
,将其更改为最新版本仍然没有修复它。index.js
存在于指定路径的目录中。
这是我的测试代码:
var chai = require('chai');
var sinonChai = require("sinon-chai");
var sinon = require('sinon');
chai.use(sinonChai);
var nock = require('nock');
var mockfs = require('mock-fs');
var proxyquire = require('proxyquire');
describe('invoicer', function () {
var expect = chai.expect;
var ResponseOptions = {
"username": "Peter Pan",
"user_address_line_1": "Never Never Land",
"user_address_line_2": "Tree-house 99",
"user_post_code": "E4 9BY",
"delivery_address_line_1": "Hooks Boat",
"delivery_address_line_2": "Dock 69",
"delivery_post_code": "SE2 4C",
"order_number": "234234234",
"order_date": "20/12/2090",
"dispatch_date": "20/12/2090",
"items": [
{
"product_name": "Fairy Dust",
"brand": "Airy fairy",
"quantity": 5,
"total": 2000
}
],
"grand_total": 2000,
"user_email": "peter@flyaway.com"
}
beforeEach(function () {
var api = nock("http://wherever.com")
.get("/to/fake/order/55432")
.reply(200, ResponseOptions);
});
describe("good request", function () {
var createTransport, sendMail, url, testedModule, contextDoneSpy, request;
before(function (done) {
createTransport = sinon.spy();
sendMail = sinon.spy();
url = "http://wherever.com/to/fake/order/55432";
constextDoneSpy = sinon.spy();
mockfs({
"../email.html": "<span><%= username %></span>"
});
request = sinon.spy();
testedModule = getTestedModule(createTransport, sendMail, request);
testedModule.Invoicer(url, { done: function () {
contextDoneSpy.apply(null, arguments);
done();
}});
});
afterEach(function () {
mockfs.restore();
});
it("context done was called", function () {
expect(contextDoneSpy).to.have.been.called;
});
});
});
function getTestedModule (createTransport, sendMail, request) {
return proxyquire('../index.js', {
"nodemailer": function () {
return {
createTransport: createTransport,
sendMail: sendMail
}
},
'request': request
});
}