Skip to content

Commit cf1f994

Browse files
authored
Merge pull request #516 from testdouble/support-register
support Node v20.6.0 ability to auto-register loader
2 parents 16d28bb + f82b42b commit cf1f994

22 files changed

Lines changed: 18636 additions & 1327 deletions

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ replaces all functions it encounters with fake test double functions which can,
9797
in turn, be configured by your test to either stub responses or assert
9898
invocations.
9999

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

104103
#### Module replacement with Node.js

docs/7-replacing-dependencies.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,15 @@ late to have their intended effect).
206206

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

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

215+
If you're using Node.js v20.6.0 or higher, the loader is registered automatically. But if you're using a lower
216+
version of Node.js, you must run Node with `--loader=testdouble`, or `td.replaceEsm` won't work.
217+
215218
Also, every time you call `td.replaceEsm` on a module, you generate a new "generation" of that module, so
216219
that the next time Node.js loads that module, it loads the new generation of that replaced module.
217220
If you do `td.reset`, then the original module is loaded instead.

examples/babel/package-lock.json

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/jest-broken/package-lock.json

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/jest/package-lock.json

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)