diff --git a/packages/integration-tests/fixtures/debug-ids-already-injected/debug-ids-already-injected.test.ts b/packages/integration-tests/fixtures/debug-ids-already-injected/debug-ids-already-injected.test.ts index 8d6feca8..c88b05be 100644 --- a/packages/integration-tests/fixtures/debug-ids-already-injected/debug-ids-already-injected.test.ts +++ b/packages/integration-tests/fixtures/debug-ids-already-injected/debug-ids-already-injected.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable jest/expect-expect */ import * as path from "path"; import * as fs from "fs"; import * as os from "os"; @@ -11,23 +12,35 @@ function createTempDir() { const SPEC_DEBUG_ID_REGEX = /\/\/# debugId=([a-fA-F0-9-]+)/g; function countDebugIdComments(source: string): number { - const matches = source.match(SPEC_DEBUG_ID_REGEX); - if (matches) { - return matches.length; + return source.match(SPEC_DEBUG_ID_REGEX)?.length || 0; +} + +function getDebugIdFromComment(source: string): string | undefined { + const match = SPEC_DEBUG_ID_REGEX.exec(source); + if (match && match[1]) { + return match[1]; } - return 0; + return undefined; } -function getSingleJavaScriptSourceFileFromDirectory( - dir: string, - fileExtension = ".js" -): string | undefined { +function getSingleJavaScriptSourceFileFromDirectory(dir: string, fileExtension = ".js"): string { const files = fs.readdirSync(dir); const jsFiles = files.filter((file) => file.endsWith(fileExtension)); if (jsFiles.length === 1) { return fs.readFileSync(path.join(dir, jsFiles[0] as string), "utf-8"); } - return undefined; + return ""; +} + +function expected(tempDir: string) { + const source = getSingleJavaScriptSourceFileFromDirectory(tempDir); + expect(source).toBeDefined(); + const debugIds = countDebugIdComments(source); + expect(debugIds).toBe(1); + const debugId = getDebugIdFromComment(source); + expect(debugId).toBeDefined(); + // Check the JavaScript contains a matching id + expect(source).toContain(`="${debugId || "fail"}"`); } describeNode18Plus("vite 6 bundle", () => { @@ -44,10 +57,7 @@ describeNode18Plus("vite 6 bundle", () => { }); test("check vite 6 bundle", () => { - const source = getSingleJavaScriptSourceFileFromDirectory(tempDir); - expect(source).toBeDefined(); - const debugIds = countDebugIdComments(source as string); - expect(debugIds).toBe(1); + expected(tempDir); }); afterEach(() => { @@ -69,10 +79,7 @@ describeNode18Plus("webpack 5 bundle", () => { }); test("check webpack 5 bundle", () => { - const source = getSingleJavaScriptSourceFileFromDirectory(tempDir); - expect(source).toBeDefined(); - const debugIds = countDebugIdComments(source as string); - expect(debugIds).toBe(1); + expected(tempDir); }); afterEach(() => { @@ -94,10 +101,7 @@ describeNode18Plus("rollup bundle", () => { }); test("check rollup bundle", () => { - const source = getSingleJavaScriptSourceFileFromDirectory(tempDir); - expect(source).toBeDefined(); - const debugIds = countDebugIdComments(source as string); - expect(debugIds).toBe(1); + expected(tempDir); }); afterEach(() => {