-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: ensure successful import returns the same result
- Loading branch information
Showing
5 changed files
with
150 additions
and
12 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
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
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,25 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
const assert = require('node:assert'); | ||
const fs = require('node:fs/promises'); | ||
const { pathToFileURL } = require('node:url'); | ||
|
||
tmpdir.refresh(); | ||
const tmpDir = pathToFileURL(tmpdir.path); | ||
|
||
const target = new URL(`./${Math.random()}.mjs`, tmpDir); | ||
|
||
(async () => { | ||
|
||
await assert.rejects(import(target), { code: 'ERR_MODULE_NOT_FOUND' }); | ||
|
||
await fs.writeFile(target, 'export default "actual target"\n'); | ||
|
||
const moduleRecord = await import(target); | ||
|
||
await fs.rm(target); | ||
|
||
assert.strictEqual(await import(target), moduleRecord); | ||
})().then(common.mustCall()); |
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,42 @@ | ||
import { spawnPromisified } from '../common/index.mjs'; | ||
import tmpdir from '../common/tmpdir.js'; | ||
|
||
import assert from 'node:assert'; | ||
import fs from 'node:fs/promises'; | ||
import { execPath } from 'node:process'; | ||
import { pathToFileURL } from 'node:url'; | ||
|
||
tmpdir.refresh(); | ||
const tmpDir = pathToFileURL(tmpdir.path); | ||
|
||
const target = new URL(`./${Math.random()}.mjs`, tmpDir); | ||
|
||
await assert.rejects(import(target), { code: 'ERR_MODULE_NOT_FOUND' }); | ||
|
||
await fs.writeFile(target, 'export default "actual target"\n'); | ||
|
||
const moduleRecord = await import(target); | ||
|
||
await fs.rm(target); | ||
|
||
assert.strictEqual(await import(target), moduleRecord); | ||
|
||
// Add the file back, it should be deleted by the subprocess. | ||
await fs.writeFile(target, 'export default "actual target"\n'); | ||
|
||
assert.deepStrictEqual( | ||
await spawnPromisified(execPath, [ | ||
'--input-type=module', | ||
'--eval', | ||
[`import * as d from${JSON.stringify(target)};`, | ||
'import{rm}from"node:fs/promises";', | ||
`await rm(new URL(${JSON.stringify(target)}));`, | ||
'import{strictEqual}from"node:assert";', | ||
`strictEqual(JSON.stringify(await import(${JSON.stringify(target)})),JSON.stringify(d));`].join(''), | ||
]), | ||
{ | ||
code: 0, | ||
signal: null, | ||
stderr: '', | ||
stdout: '', | ||
}); |
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