-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: improve coverage for load hooks
Refs: #43363 (comment) PR-URL: #43374 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Akhil Marsonya <akhil.marsonya27@gmail.com>
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { mustCall } from '../common/index.mjs'; | ||
import { fileURL, path } from '../common/fixtures.mjs'; | ||
import { match, ok, notStrictEqual, strictEqual } from 'assert'; | ||
import { spawn } from 'child_process'; | ||
import { execPath } from 'process'; | ||
|
||
{ | ||
const child = spawn(execPath, [ | ||
'--experimental-loader', | ||
fileURL('es-module-loaders', 'thenable-load-hook.mjs').href, | ||
path('es-modules', 'test-esm-ok.mjs'), | ||
]); | ||
|
||
let stderr = ''; | ||
child.stderr.setEncoding('utf8'); | ||
child.stderr.on('data', (data) => { | ||
stderr += data; | ||
}); | ||
child.on('close', mustCall((code, _signal) => { | ||
strictEqual(code, 0); | ||
ok(!stderr.includes('must not call')); | ||
})); | ||
} | ||
|
||
{ | ||
const child = spawn(execPath, [ | ||
'--experimental-loader', | ||
fileURL('es-module-loaders', 'thenable-load-hook-rejected.mjs').href, | ||
path('es-modules', 'test-esm-ok.mjs'), | ||
]); | ||
|
||
let stderr = ''; | ||
child.stderr.setEncoding('utf8'); | ||
child.stderr.on('data', (data) => { | ||
stderr += data; | ||
}); | ||
child.on('close', mustCall((code, _signal) => { | ||
notStrictEqual(code, 0); | ||
|
||
match(stderr, /\sError: must crash the process\r?\n/); | ||
|
||
ok(!stderr.includes('must not call')); | ||
})); | ||
} | ||
|
||
{ | ||
const child = spawn(execPath, [ | ||
'--experimental-loader', | ||
fileURL('es-module-loaders', 'thenable-load-hook-rejected-no-arguments.mjs').href, | ||
path('es-modules', 'test-esm-ok.mjs'), | ||
]); | ||
|
||
let stderr = ''; | ||
child.stderr.setEncoding('utf8'); | ||
child.stderr.on('data', (data) => { | ||
stderr += data; | ||
}); | ||
child.on('close', mustCall((code, _signal) => { | ||
notStrictEqual(code, 0); | ||
|
||
match(stderr, /\sundefined\r?\n/); | ||
|
||
ok(!stderr.includes('must not call')); | ||
})); | ||
} |
10 changes: 10 additions & 0 deletions
10
test/fixtures/es-module-loaders/thenable-load-hook-rejected-no-arguments.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function load () { | ||
let thenAlreadyAccessed = false; | ||
return { | ||
get then() { | ||
if (thenAlreadyAccessed) throw new Error('must not call'); | ||
thenAlreadyAccessed = true; | ||
return (_, reject) => reject(); | ||
} | ||
}; | ||
} |
10 changes: 10 additions & 0 deletions
10
test/fixtures/es-module-loaders/thenable-load-hook-rejected.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function load () { | ||
let thenAlreadyAccessed = false; | ||
return { | ||
get then() { | ||
if (thenAlreadyAccessed) throw new Error('must not call'); | ||
thenAlreadyAccessed = true; | ||
return (_, reject) => reject(new Error('must crash the process')); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function load(url, context, next) { | ||
let thenAlreadyAccessed = false; | ||
return { | ||
get then() { | ||
if (thenAlreadyAccessed) throw new Error('must not call'); | ||
thenAlreadyAccessed = true; | ||
return (resolve) => resolve(next(url, context)); | ||
} | ||
}; | ||
} |