mockery icon indicating copy to clipboard operation
mockery copied to clipboard

better handling of relative paths

Open afeld opened this issue 10 years ago • 9 comments

An example:

// src/dependency.js
throw new Error("Shouldn't be required!");


// src/mocked.js
require('./dependency');


// test/mockery_test.js
var mockery = require('mockery');
mockery.enable();
mockery.registerMock('../src/dependency', {});
var mocked = require('../src/mocked');

Running node test/mockery_test.js will throw the Error. It would be great if registerMock() recognized paths as being relative to the test file (where the mock is coming from), rather than relative to the file that will require them. Does that make sense?

afeld avatar Nov 08 '14 00:11 afeld

This may be "ok" in some instances, but mockery was designed to work off a "perfect match" so that it's not trying to play guessing games on what module to mock out.

We call this behavior out in the readme: https://github.com/mfncooper/mockery#registering-mocks

module, the name or path of the module for which a mock is being registered. This must exactly match the argument to require; there is no "clever" matching

davglass avatar Jan 21 '15 19:01 davglass

I understand... just seems like a pretty big limitation, and one that doesn't require too much cleverness to work around.

afeld avatar Jan 21 '15 19:01 afeld

I'm looking into it now, when mockery was designed the node module code was way simpler I think they have added better resolving methods since this was done.

Now that I have the tests updated, I'll see about merging the PR for this (which breaks several of my current tests).

davglass avatar Jan 21 '15 20:01 davglass

I've been wanting this functionality for a while. It's a breaking change so please update the major version :)

gotwarlost avatar Jan 21 '15 20:01 gotwarlost

Any update on this? I am looking forward for this update.

3den avatar Jan 26 '15 22:01 3den

It's on my list, I'm looking into the best solutions now.

davglass avatar Jan 27 '15 14:01 davglass

:+1:

3den avatar Jan 27 '15 17:01 3den

+1

jeffkole avatar Apr 27 '15 23:04 jeffkole

+1 Had an issue today where we are mocking out one of our relative dependencies './parser' but we did not want to mock out a library 'handlebars'. The problem we found is that handlebars has its own './parser' and so that one is also mocked which breaks 'handlebars' :(

mikefarah avatar Jul 20 '16 04:07 mikefarah