question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using td.replace() with external modules

See original GitHub issue

Based on the node example I’ve tried to use td.replace() with a external module. The module I’m testing does something like this:

import elasticsearch from 'elasticsearch'
var client = new elasticsearch.Client({ ... })
client.search({ ... });

My test, currently using sinon, does something like this:

import sinon from 'sinon'
import elasticsearch from 'elasticsearch'
searchSpy = {
  search: sinon.stub().returns(Promise.resolve({
    hits: {
      hits: [ ... ]
    }
  }))
}
sinon.stub(elasticsearch, 'Client').returns(searchSpy)

Stubbing only elasticsearch.Client is something I want to get rid of (to solve an unrelated problem). td.replace() seems like the right tool for that. So I tried something like this:

import elasticsearch from 'elasticsearch'
import td from 'testdouble'
searchSpy = {
  search: sinon.stub().returns(Promise.resolve({
    hits: {
      hits: [ ... ]
    }
  }))
}
var es = td.replace('elasticsearch')
td.when(es.Client()).return(searchSpy)

That fails with TypeError: es.Client is not a function. td.when(es.Client) doesn’t work either.

Since there is no documentation for td.replace() and the example only imports internal module (with relative paths), I can’t tell if I’m doing it wrong (likely) or if this is just not supported (yet, also likely).

If the snippets above aren’t enough, I can also put together a sample that actually works.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
searlscommented, Mar 25, 2017

Hey @jzaefferer and friends, I’ve changed my mind about this. Enough teams are using private modules at this point as a code organization strategy, that no one is benefited by the library preventing using td.replace in this way, especially because it requires hardly any code changes on our part to support this. (Really it just requires resolving from the perspective of the current working directory).

See #220

4reactions
searlscommented, Nov 19, 2016

Yeah, we are open to it, but we still have the concerns we did at the outset–that giving folks a module replacement method would encourage replacing third party dependencies directly without wrapping them. At the same time that’s not always necessary (e.g. a separate module that you do control)

Anyway maybe someone has the time to look into adding this to quibble over the holiday.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Replacing modules with testdouble.js - Abundancia
I have a ./utils/fs.js file that is imported by saveImage.js . Here's how I create a stub for it with testdouble, if I...
Read more >
mocha test mock/spy imported library using testdouble.js
I tried to mock/replacing downloadjs to verify that it called with valid data. neither td.replace(download) nor td.replace('downloadjs') works.
Read more >
testdouble - npm
Start using testdouble in your project by running `npm i testdouble`. ... Why doesn't td.replace() work with external CommonJS modules?
Read more >
How to mock files with TestDouble, TypeScript and Jest - Xolvio
replace() before we import the module that will use it. We had to do the same thing with td.replace() and require() calls but...
Read more >
How to mock a dependency in a Node.js, and why you should ...
And each of them have some pros and cons. For example — mockery has something about isolation and can replace one module by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found