diff options
author | Marvin Hagemeister <hello@marvinh.dev> | 2023-03-27 21:54:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-27 21:54:22 +0200 |
commit | 8c051dbd1a075ad3c228f78b29b13f0e455972a7 (patch) | |
tree | 5f17ffd87ca6e0febab5fa64803570eca4dfb4f5 /cli/tests | |
parent | 101abf35411d26342e6855dc3432cc7f9695dbc8 (diff) |
fix(ext/node): add missing _preloadModules hook (#18447)
This internal node hook is used by libraries such as `ts-node` when used
as a require hook `node -r ts-node/register`. That combination is often
used with test frameworks like `mocha` or `jasmine`.
We had a reference to `Module._preloadModules` in our code, but the
implementation was missing. While fixing this I also noticed that the
`fakeParent` module that we create internally always threw because of
the `pathDirname` check on the module id in the constructor of `Mdoule`.
So this code path was probably broken for a while.
```txt
✖ ERROR: Error: Empty filepath.
at pathDirname (ext:deno_node/01_require.js:245:11)
at new Module (ext:deno_node/01_require.js:446:15)
at Function.Module._resolveFilename (ext:deno_node/01_require.js:754:28)
at Function.resolve (ext:deno_node/01_require.js:1015:19)
```
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/unit_node/module_test.ts | 14 | ||||
-rw-r--r-- | cli/tests/unit_node/testdata/add_global_property.js | 1 |
2 files changed, 15 insertions, 0 deletions
diff --git a/cli/tests/unit_node/module_test.ts b/cli/tests/unit_node/module_test.ts new file mode 100644 index 000000000..d071ed2d1 --- /dev/null +++ b/cli/tests/unit_node/module_test.ts @@ -0,0 +1,14 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +import { Module } from "node:module"; +import { assertStrictEquals } from "../../../test_util/std/testing/asserts.ts"; + +Deno.test("[node/module _preloadModules] has internal require hook", () => { + // Check if it's there + // deno-lint-ignore no-explicit-any + (Module as any)._preloadModules([ + "./cli/tests/unit_node/testdata/add_global_property.js", + ]); + // deno-lint-ignore no-explicit-any + assertStrictEquals((globalThis as any).foo, "Hello"); +}); diff --git a/cli/tests/unit_node/testdata/add_global_property.js b/cli/tests/unit_node/testdata/add_global_property.js new file mode 100644 index 000000000..814d17d0d --- /dev/null +++ b/cli/tests/unit_node/testdata/add_global_property.js @@ -0,0 +1 @@ +globalThis.foo = "Hello"; |