1+ /* eslint-disable jest/expect-expect */
12import * as path from "path" ;
23import * as fs from "fs" ;
34import * as os from "os" ;
@@ -11,23 +12,35 @@ function createTempDir() {
1112const SPEC_DEBUG_ID_REGEX = / \/ \/ # d e b u g I d = ( [ a - f A - F 0 - 9 - ] + ) / g;
1213
1314function countDebugIdComments ( source : string ) : number {
14- const matches = source . match ( SPEC_DEBUG_ID_REGEX ) ;
15- if ( matches ) {
16- return matches . length ;
15+ return source . match ( SPEC_DEBUG_ID_REGEX ) ?. length || 0 ;
16+ }
17+
18+ function getDebugIdFromComment ( source : string ) : string | undefined {
19+ const match = SPEC_DEBUG_ID_REGEX . exec ( source ) ;
20+ if ( match && match [ 1 ] ) {
21+ return match [ 1 ] ;
1722 }
18- return 0 ;
23+ return undefined ;
1924}
2025
21- function getSingleJavaScriptSourceFileFromDirectory (
22- dir : string ,
23- fileExtension = ".js"
24- ) : string | undefined {
26+ function getSingleJavaScriptSourceFileFromDirectory ( dir : string , fileExtension = ".js" ) : string {
2527 const files = fs . readdirSync ( dir ) ;
2628 const jsFiles = files . filter ( ( file ) => file . endsWith ( fileExtension ) ) ;
2729 if ( jsFiles . length === 1 ) {
2830 return fs . readFileSync ( path . join ( dir , jsFiles [ 0 ] as string ) , "utf-8" ) ;
2931 }
30- return undefined ;
32+ return "" ;
33+ }
34+
35+ function expected ( tempDir : string ) {
36+ const source = getSingleJavaScriptSourceFileFromDirectory ( tempDir ) ;
37+ expect ( source ) . toBeDefined ( ) ;
38+ const debugIds = countDebugIdComments ( source ) ;
39+ expect ( debugIds ) . toBe ( 1 ) ;
40+ const debugId = getDebugIdFromComment ( source ) ;
41+ expect ( debugId ) . toBeDefined ( ) ;
42+ // Check the JavaScript contains a matching id
43+ expect ( source ) . toContain ( `="${ debugId || "fail" } "` ) ;
3144}
3245
3346describeNode18Plus ( "vite 6 bundle" , ( ) => {
@@ -44,10 +57,7 @@ describeNode18Plus("vite 6 bundle", () => {
4457 } ) ;
4558
4659 test ( "check vite 6 bundle" , ( ) => {
47- const source = getSingleJavaScriptSourceFileFromDirectory ( tempDir ) ;
48- expect ( source ) . toBeDefined ( ) ;
49- const debugIds = countDebugIdComments ( source as string ) ;
50- expect ( debugIds ) . toBe ( 1 ) ;
60+ expected ( tempDir ) ;
5161 } ) ;
5262
5363 afterEach ( ( ) => {
@@ -69,10 +79,7 @@ describeNode18Plus("webpack 5 bundle", () => {
6979 } ) ;
7080
7181 test ( "check webpack 5 bundle" , ( ) => {
72- const source = getSingleJavaScriptSourceFileFromDirectory ( tempDir ) ;
73- expect ( source ) . toBeDefined ( ) ;
74- const debugIds = countDebugIdComments ( source as string ) ;
75- expect ( debugIds ) . toBe ( 1 ) ;
82+ expected ( tempDir ) ;
7683 } ) ;
7784
7885 afterEach ( ( ) => {
@@ -94,10 +101,7 @@ describeNode18Plus("rollup bundle", () => {
94101 } ) ;
95102
96103 test ( "check rollup bundle" , ( ) => {
97- const source = getSingleJavaScriptSourceFileFromDirectory ( tempDir ) ;
98- expect ( source ) . toBeDefined ( ) ;
99- const debugIds = countDebugIdComments ( source as string ) ;
100- expect ( debugIds ) . toBe ( 1 ) ;
104+ expected ( tempDir ) ;
101105 } ) ;
102106
103107 afterEach ( ( ) => {
0 commit comments