`proxyquire` - error: cannot find module
Asked Answered
B

0

6

For some reason proxquire cannot find my module index.js. I must be doing something really stupid or obvious and can't see it.

Error:

  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)

The strange thing is that I've got the same set-up for the tests on another app and it has no trouble finding the module there. I've even maintained the same version of proxyquire, changing it to the latest version still didn't fix it. index.js exists in the directory for the specified path.

This is my test code:

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": "[email protected]"
    }

    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
    });
}
Beare answered 24/4, 2015 at 15:37 Comment(9)
Getting any error than? Can you paste it in here?Stickseed
@Stickseed just added the errorBeare
Did you try full path? proxyquire(' /Users/mario/projects/_invoice/print-invoice/index.js,... Or wherever the file isStickseed
@Stickseed just tried that. Returns the same error. Cannot find module.Beare
Couldn't it be a file permission issue?Stickseed
How so? I'm not aware of this. Could you elaborate please?Beare
Just a thought but maybe node doesn't have permission to read the file which sometimes throw err can't find. Does "node index.js" work ok?Stickseed
@Stickseed I don't think so. If I require the file without proxyquire, it doesn't return and error and console.log's a message I have at the top of the module. I think it must have something to do with the getTestedModule function.Beare
How did you manage to fix it? @BeareMillardmillboard

© 2022 - 2024 — McMap. All rights reserved.