Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

td.replace breaks subsequent require calls #103

Closed
scottaj opened this issue Apr 28, 2016 · 3 comments
Closed

td.replace breaks subsequent require calls #103

scottaj opened this issue Apr 28, 2016 · 3 comments

Comments

@scottaj
Copy link

scottaj commented Apr 28, 2016

This code example demonstrates the problem. Since CantMakeHeadsOrTailsOfItError is require'd after doing a td.replace, it fails the instanceof check in the test.

It seems unexpected that replacing a different module would have any effect on other modules require'd after it.

To be clear either removing the td.replace, or moving the require of the error to before the td.replace fixes the issue, so it is easy to work around.

...
const MyLib = require('../index');
const MyDependency = td.replace('../dependency');
const CantMakeHeadsOrTailsOfItError = require('../cant-make-heads-or-tails-of-it-error');

describe('My Lib', function() {
  ...
  describe('#tail', function() {
    it('throws an error if the provided object is not an array', function () {
      try {
        myLib.tail(7);
        throw new Error('Did not get expected exception.');
      } catch (error) {
        expect(error).to.be.an.instanceOf(CantMakeHeadsOrTailsOfItError);
      }
    });
  });
});

Here is a runnable project with the full code example and failing test from above: https://github.com/scottaj/td-instanceof-example

@searls
Copy link
Member

searls commented May 10, 2016

There are three issues in the example above, and two of them will confound the third (which is a real issue)

  1. If you're using the td.replace API, it needs to be inside a beforeEach or some kind of before hook that gets reset after each test, paired with td.reset() in an afterEach hook so that the state of the universe can be properly restored.
  2. Any dependencies you replace need to be td.replace()'d before you actually call require on the subject under test; otherwise, the library has no way to know what dependencies to replace when the subject is loaded (and will instead load real ones). (e.g. MyDependency = td.replace('../dependency') should be before MyLib = require('../index'))
  3. Instantiable function dependencies replace()'d with testdouble.js will fail instanceof checks.

Can you resolve the first two issues and retitle/redescribe the problem in those contexts? Otherwise I'll close and open a new one for it.

@scottaj
Copy link
Author

scottaj commented May 10, 2016

Sure, I'll update it.

@searls
Copy link
Member

searls commented Jun 3, 2016

Closing. Follow along in #107

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants