Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
documentation
  • Loading branch information
giltayar committed Sep 12, 2023
1 parent 35af6cc commit f82b42b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -97,8 +97,7 @@ replaces all functions it encounters with fake test double functions which can,
in turn, be configured by your test to either stub responses or assert
invocations.

Additionally, if you're using Node 13 or newer, you can specify testdouble as a
module loader and replace native ES modules with `td.replaceEsm()`. More details
Additionally, if you're using Node 16 or newer, you can replace native ES modules with `td.replaceEsm()`. More details
[here](docs/7-replacing-dependencies.md#how-module-replacement-works-for-es-modules-using-import).

#### Module replacement with Node.js
Expand Down
5 changes: 4 additions & 1 deletion docs/7-replacing-dependencies.md
Expand Up @@ -206,12 +206,15 @@ late to have their intended effect).

### How module replacement works for ES modules (using `import`)

Under the hood, testdouble.js uses a loader (which you run using `node loader=testdouble`). A loader
Under the hood, testdouble.js uses a loader. A loader
is a module that can hook into the ES module loading mechanism in an official way. The testdouble
loader ensures that if you call `td.replaceEsm` on an ES Module, the next time that module is loaded
by Node.js, the source Node.js will get from the loader is not the regular source, but rather source
code that contains some replacement code that can be used for the mock doubles.

If you're using Node.js v20.6.0 or higher, the loader is registered automatically. But if you're using a lower
version of Node.js, you must run Node with `--loader=testdouble`, or `td.replaceEsm` won't work.

Also, every time you call `td.replaceEsm` on a module, you generate a new "generation" of that module, so
that the next time Node.js loads that module, it loads the new generation of that replaced module.
If you do `td.reset`, then the original module is loaded instead.
Expand Down

0 comments on commit f82b42b

Please sign in to comment.