-
-
Notifications
You must be signed in to change notification settings - Fork 35
docs, test: clarify how to hook a module sub-path using .cjs extension #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| 'use strict' | ||
|
|
||
| // This tests that sub-module files using the `.cjs` extension are *not* | ||
| // hookable via a normalized module path. Instead one must use the .cjs | ||
| // extension on the hook arg. | ||
| // | ||
| // E.g., a Hook arg of `cjs-sub-module/foo` will **not** hook | ||
| // `./node_modules/cjs-sub-module/foo.cjs`. This is different compared to `.js` | ||
| // file extension usage. The difference is that Node.js's `require()` treats | ||
| // `.js` and `.cjs` differently. | ||
| // See https://nodejs.org/api/modules.html#file-modules | ||
|
Comment on lines
+7
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand the reasoning, but it would make instrumenting modules very complicated. The end user API of |
||
|
|
||
| const test = require('tape') | ||
|
|
||
| const { Hook } = require('../') | ||
|
|
||
| test('require("cjs-sub-module/foo") does NOT hook cjs-sub-module/foo.cjs', function (t) { | ||
| const hook = new Hook(['cjs-sub-module/foo'], function (exports) { | ||
| t.fail('should not get here') | ||
| return exports | ||
| }) | ||
|
|
||
| t.equal(require('./node_modules/cjs-sub-module'), 'cjs-sub-module/index.js') | ||
| t.equal(require('./node_modules/cjs-sub-module/foo.cjs'), 'cjs-sub-module/foo.cjs') | ||
|
|
||
| try { | ||
| require('./node_modules/cjs-sub-module/foo') | ||
| t.fail('the previous require should throw') | ||
| } catch (err) { | ||
| t.ok(/Cannot find module/.test(err.message), 'got expected exception') | ||
| } | ||
|
|
||
| hook.unhook() | ||
| t.end() | ||
| }) | ||
|
|
||
| test('require("cjs-sub-module/bar") does NOT hook cjs-sub-module/bar/index.cjs', function (t) { | ||
| const hook = new Hook(['cjs-sub-module/bar'], function (exports) { | ||
| t.fail('should not get here') | ||
| return exports | ||
| }) | ||
|
|
||
| t.equal(require('./node_modules/cjs-sub-module'), 'cjs-sub-module/index.js') | ||
| t.equal(require('./node_modules/cjs-sub-module/bar/index.cjs'), 'cjs-sub-module/bar/index.cjs') | ||
|
|
||
| try { | ||
| require('./node_modules/cjs-sub-module/bar') | ||
| t.fail('the previous require should throw') | ||
| } catch (err) { | ||
| t.ok(/Cannot find module/.test(err.message), 'got expected exception') | ||
| } | ||
|
|
||
| hook.unhook() | ||
| t.end() | ||
| }) | ||
|
|
||
| test('require("cjs-sub-module/foo.cjs") DOES hook cjs-sub-module/foo.cjs', function (t) { | ||
| const hookedNames = [] | ||
| const hook = new Hook(['cjs-sub-module/foo.cjs'], function (exports, name) { | ||
| hookedNames.push(name) | ||
| return exports | ||
| }) | ||
|
|
||
| t.equal(require('./node_modules/cjs-sub-module'), 'cjs-sub-module/index.js') | ||
| t.equal(require('./node_modules/cjs-sub-module/foo.cjs'), 'cjs-sub-module/foo.cjs') | ||
| t.deepEqual(hookedNames, ['cjs-sub-module/foo.cjs']) | ||
|
|
||
| hook.unhook() | ||
| t.end() | ||
| }) | ||
|
|
||
| test('require("cjs-sub-module/bar/index.cjs") DOES hook cjs-sub-module/bar/index.cjs', function (t) { | ||
| const hookedNames = [] | ||
| const hook = new Hook(['cjs-sub-module/bar/index.cjs'], function (exports, name) { | ||
| hookedNames.push(name) | ||
| return exports | ||
| }) | ||
|
|
||
| t.equal(require('./node_modules/cjs-sub-module'), 'cjs-sub-module/index.js') | ||
| t.equal(require('./node_modules/cjs-sub-module/bar/index.cjs'), 'cjs-sub-module/bar/index.cjs') | ||
| t.deepEqual(hookedNames, ['cjs-sub-module/bar/index.cjs']) | ||
|
|
||
| hook.unhook() | ||
| t.end() | ||
| }) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.