@@ -2,7 +2,10 @@ import { spawnPromisified } from '../common/index.mjs';
2
2
import * as fixtures from '../common/fixtures.mjs' ;
3
3
import assert from 'node:assert' ;
4
4
import { execPath } from 'node:process' ;
5
- import { describe , it } from 'node:test' ;
5
+ import { describe , it , test } from 'node:test' ;
6
+
7
+ import { mkdir , rm , writeFile } from 'node:fs/promises' ;
8
+ import * as tmpdir from '../common/tmpdir.js' ;
6
9
7
10
import secret from '../fixtures/experimental.json' assert { type : 'json ' } ;
8
11
@@ -21,4 +24,61 @@ describe('ESM: importing JSON', () => {
21
24
assert . strictEqual ( code , 0 ) ;
22
25
assert . strictEqual ( signal , null ) ;
23
26
} ) ;
27
+
28
+ test ( 'should load different modules when the URL is different' , async ( t ) => {
29
+ const root = tmpdir . fileURL ( `./test-esm-json-${ Math . random ( ) } /` ) ;
30
+ try {
31
+ await mkdir ( root , { recursive : true } ) ;
32
+
33
+ await t . test ( 'json' , async ( ) => {
34
+ let i = 0 ;
35
+ const url = new URL ( './foo.json' , root ) ;
36
+ await writeFile ( url , JSON . stringify ( { id : i ++ } ) ) ;
37
+ const absoluteURL = await import ( `${ url } ` , {
38
+ assert : { type : 'json' } ,
39
+ } ) ;
40
+ await writeFile ( url , JSON . stringify ( { id : i ++ } ) ) ;
41
+ const queryString = await import ( `${ url } ?a=2` , {
42
+ assert : { type : 'json' } ,
43
+ } ) ;
44
+ await writeFile ( url , JSON . stringify ( { id : i ++ } ) ) ;
45
+ const hash = await import ( `${ url } #a=2` , {
46
+ assert : { type : 'json' } ,
47
+ } ) ;
48
+ await writeFile ( url , JSON . stringify ( { id : i ++ } ) ) ;
49
+ const queryStringAndHash = await import ( `${ url } ?a=2#a=2` , {
50
+ assert : { type : 'json' } ,
51
+ } ) ;
52
+
53
+ assert . notDeepStrictEqual ( absoluteURL , queryString ) ;
54
+ assert . notDeepStrictEqual ( absoluteURL , hash ) ;
55
+ assert . notDeepStrictEqual ( queryString , hash ) ;
56
+ assert . notDeepStrictEqual ( absoluteURL , queryStringAndHash ) ;
57
+ assert . notDeepStrictEqual ( queryString , queryStringAndHash ) ;
58
+ assert . notDeepStrictEqual ( hash , queryStringAndHash ) ;
59
+ } ) ;
60
+
61
+ await t . test ( 'js' , async ( ) => {
62
+ let i = 0 ;
63
+ const url = new URL ( './foo.mjs' , root ) ;
64
+ await writeFile ( url , `export default ${ JSON . stringify ( { id : i ++ } ) } \n` ) ;
65
+ const absoluteURL = await import ( `${ url } ` ) ;
66
+ await writeFile ( url , `export default ${ JSON . stringify ( { id : i ++ } ) } \n` ) ;
67
+ const queryString = await import ( `${ url } ?a=1` ) ;
68
+ await writeFile ( url , `export default ${ JSON . stringify ( { id : i ++ } ) } \n` ) ;
69
+ const hash = await import ( `${ url } #a=1` ) ;
70
+ await writeFile ( url , `export default ${ JSON . stringify ( { id : i ++ } ) } \n` ) ;
71
+ const queryStringAndHash = await import ( `${ url } ?a=1#a=1` ) ;
72
+
73
+ assert . notDeepStrictEqual ( absoluteURL , queryString ) ;
74
+ assert . notDeepStrictEqual ( absoluteURL , hash ) ;
75
+ assert . notDeepStrictEqual ( queryString , hash ) ;
76
+ assert . notDeepStrictEqual ( absoluteURL , queryStringAndHash ) ;
77
+ assert . notDeepStrictEqual ( queryString , queryStringAndHash ) ;
78
+ assert . notDeepStrictEqual ( hash , queryStringAndHash ) ;
79
+ } ) ;
80
+ } finally {
81
+ await rm ( root , { force : true , recursive : true } ) ;
82
+ }
83
+ } ) ;
24
84
} ) ;
0 commit comments