testdouble / quibble

Makes it easy to replace require'd dependencies.
93 stars 26 forks source link

Add documentation for replacing builtinModules and esm function's parameters #40

Closed TimDaub closed 3 years ago

TimDaub commented 3 years ago

Here @giltayar wrote that:

quibble.esm accepts two mock parameters: the "named exports" and the "default export". Since you import the default export, then the third parameter should be the one you mock. The internal modules are unique in that they are both the same (the default export is an object that has properties with the same names as the named exports), so the more correct way to do this would be:

 const fsExports = {
    readFileSync: function (path) {
      console.log("using quibbled readFileSyns... yay!");
      return "Looks like 'fs' was replaced correctly.";
    },
  }
  await quibble.esm("fs", fsExports, fsExports);

What I think was said implicitly, but is important to mention explicitly is that when you decide to use quibble either with the first parameter (that is "named exports") or second parameter (that is "default exports"), then for an internal it will take either "a named exports" or a "default exports" form!

Hence, it's important for internal modules to always use quibble as described above.

Anyways, if you want to add this to the README.md, I'm happy to write a few lines.

searls commented 3 years ago

Hey @giltayar would you mind clarifying this point for us? Thank you! 🙏

giltayar commented 3 years ago

@TimDaub you're right in that this text is confusing and unclear. I'll try and rewrite it this week, but if you want to take a stab at it, would love to see it

giltayar commented 3 years ago

@searls the PR @TimDaub wrote looks good!