Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable jest/expect-expect */
import * as path from "path";
import * as fs from "fs";
import * as os from "os";
Expand All @@ -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", () => {
Expand All @@ -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(() => {
Expand All @@ -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(() => {
Expand All @@ -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(() => {
Expand Down
Loading