0

I have the following code which I am trying to test. However proxyquire cannot find the readDirectory.js. Does anyone understand why?

It's returning the following error, Error: Cannot find module '../readDirectory.js' and point to the line were testedModule = proxyquire('../readDirectory.js', {

readDirectory.js:

var dir = require('node-dir');

var _getFiles = {};

_getFiles._get = function (directory, callback) {

    dir.readFiles(directory,
        function(err, content, next) {
            if (err) throw err;
            console.log('content:', content);
        });
};

module.exports = _getFiles;

tests:

var mockDir = require('mock-fs');    
describe("readDirectory", function () {
        var testedModule, callbackSpy, readFileStub;

    before(function () {

        mockDir({
            tmp: {
                images: {
                    thumb_test: "thumbnail pic",
                    small_test: "small pic",
                    medium_test: "medium pic"
                }
            }
        });

        readFileStub = sinon.stub();

        callbackSpy = sinon.spy();

        testedModule = proxyquire('../readDirectory.js', {
            "node-dir": {
                "readFiles": readFileStub
            }
        });
    });

    after(function () {
        mockDir.restore();
    });

    it("call readdir with fake directory", function () {
        testedModule._get(mockDir, function () {console.log("Hello");});

    });
});
hyprstack
  • 4,043
  • 6
  • 46
  • 89
  • Does `require('../readDirectory.js')` throw an error? – Joseph Dykstra May 13 '15 at 13:32
  • I have managed to get it working. Simply changed the order in which I have `testedModule` and `mockDir` set-up. Put `mockDir` at the end, after `testedModule` and it works. I suppose there is some sort of conflict once the fake directory is created. – hyprstack May 13 '15 at 13:36
  • Cool! It sounds like `mock-fs` actually modifies the `fs` object? Maybe they should have created it to be compatible with `proxyquire`. – Joseph Dykstra May 13 '15 at 13:46
  • Yeah, I'm not sure what the cause is, but it does sound like, something close to, if not, what you mentioned. – hyprstack May 13 '15 at 14:01

0 Answers0