1
1
import { access , readFile , writeFile } from "fs/promises" ;
2
2
import { execSync , type SpawnSyncReturns } from "child_process" ;
3
3
import assert from "node:assert" ;
4
+ import path from "node:path" ;
4
5
import { after , describe , test } from "node:test" ;
6
+ import { fileURLToPath } from "node:url" ;
5
7
6
- const attw = `node ${ new URL ( "../../dist/index.js" , import . meta. url ) . pathname } ` ;
8
+ const directoryPath = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
9
+ function resolveFileRelativePath ( relPath : string ) {
10
+ return path . resolve ( directoryPath , relPath ) ;
11
+ }
12
+
13
+ const attw = `node ${ resolveFileRelativePath ( "../../dist/index.js" ) } ` ;
7
14
const updateSnapshots = process . env . UPDATE_SNAPSHOTS || process . env . U ;
8
15
const testFilter = ( process . env . TEST_FILTER || process . env . T ) ?. toLowerCase ( ) ;
9
16
@@ -34,11 +41,11 @@ const tests = [
34
41
35
42
[
36
43
37
- `--definitely-typed ${ new URL ( "../../../core/test/fixtures/@[email protected] " , import . meta . url ) . pathname } ` ,
44
+ `--definitely-typed ${ resolveFileRelativePath ( "../../../core/test/fixtures/@[email protected] " ) } ` ,
38
45
] ,
39
46
[
40
47
41
- `--definitely-typed ${ new URL ( "../../../core/test/fixtures/@[email protected] " , import . meta . url ) . pathname } ` ,
48
+ `--definitely-typed ${ resolveFileRelativePath ( "../../../core/test/fixtures/@[email protected] " ) } ` ,
42
49
] ,
43
50
44
51
[ "[email protected] " , "--entrypoints-legacy --ignore-rules=cjs-only-exports-default" ] ,
@@ -69,7 +76,7 @@ describe("snapshots", async () => {
69
76
}
70
77
71
78
test ( fixture , async ( ) => {
72
- const tarballPath = new URL ( `../../../core/test/fixtures/${ tarball } ` , import . meta . url ) . pathname ;
79
+ const tarballPath = resolveFileRelativePath ( `../../../core/test/fixtures/${ tarball } ` ) ;
73
80
let stdout ;
74
81
let stderr = "" ;
75
82
let exitCode = 0 ;
@@ -85,7 +92,7 @@ describe("snapshots", async () => {
85
92
}
86
93
const snapshotURL = new URL ( `../snapshots/${ fixture } .md` , import . meta. url ) ;
87
94
// prettier-ignore
88
- const expectedSnapshot = [
95
+ const actualSnapshot = [
89
96
`# ${ fixture } ` ,
90
97
"" ,
91
98
"```" ,
@@ -99,19 +106,16 @@ describe("snapshots", async () => {
99
106
] . join ( "\n" ) ;
100
107
101
108
if (
102
- await access ( snapshotURL )
109
+ ! updateSnapshots &&
110
+ ( await access ( snapshotURL )
103
111
. then ( ( ) => true )
104
- . catch ( ( ) => false )
112
+ . catch ( ( ) => false ) )
105
113
) {
106
114
const snapshot = await readFile ( snapshotURL , "utf8" ) ;
107
- if ( updateSnapshots ) {
108
- await writeFile ( snapshotURL , expectedSnapshot ) ;
109
- snapshotsWritten . push ( snapshotURL ) ;
110
- } else {
111
- assert . strictEqual ( snapshot , expectedSnapshot ) ;
112
- }
115
+ const expectedSnapshot = snapshot . replace ( / \r \n / g, "\n" ) ;
116
+ assert . strictEqual ( actualSnapshot , expectedSnapshot ) ;
113
117
} else {
114
- await writeFile ( snapshotURL , expectedSnapshot ) ;
118
+ await writeFile ( snapshotURL , actualSnapshot ) ;
115
119
snapshotsWritten . push ( snapshotURL ) ;
116
120
}
117
121
} ) ;
0 commit comments