Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions src/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function runTests(testFiles: string[], options: IRunTestsOptions =
const indexContents = testFiles.map((f) => `require(${JSON.stringify(f)});`).join("\n");

const { onBuildEndPlugin, onBuildEnd } = captureBuildEnd();

const format = esbuildConfig?.format ?? "iife";
const combinedConfig = {
...esbuildConfig,
stdin: {
Expand All @@ -51,11 +51,11 @@ export async function runTests(testFiles: string[], options: IRunTestsOptions =
write: false,
sourcemap: true,
bundle: true,
format: "iife",
format,
logLevel: "info",
plugins: [
...(esbuildConfig?.plugins ?? []),
createOutputCapturePlugin(workingDir, buildFilesMap, options),
createOutputCapturePlugin(workingDir, buildFilesMap, options, format),
onBuildEndPlugin,
],
} satisfies esbuild.BuildOptions;
Expand Down Expand Up @@ -121,6 +121,7 @@ function createOutputCapturePlugin(
workingDir: string,
buildFilesMap: Map<string, esbuild.OutputFile>,
options: IRunTestsOptions,
format: esbuild.Format,
): esbuild.Plugin {
return {
name: "capture-output",
Expand All @@ -132,6 +133,7 @@ function createOutputCapturePlugin(
buildFilesMap.set("/" + path.relative(workingDir, outFile.path).replace("/\\/g", "/"), outFile);
}
const testsHTML = createTestsHTML(
format,
"mocha tests",
options.ui ?? "bdd",
options.colors ?? true,
Expand Down Expand Up @@ -195,6 +197,7 @@ async function waitForTestResults(page: playwright.Page): Promise<number> {
}

function createTestsHTML(
format: esbuild.Format,
title: string,
ui: string,
color: boolean,
Expand Down Expand Up @@ -253,7 +256,7 @@ function createTestsHTML(
.on('end', () => {(mochaStatus.finished = true)});
});
</script>
<script src="tests.js"></script>
${format === "esm" ? `<script type="module" src="tests.js"></script>` : `<script src="tests.js"></script>`}
</body>
</html>
`;
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/with-esm-esbuild-config/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import("esbuild").BuildOptions} */
export default {
format: "esm",
};
7 changes: 7 additions & 0 deletions test/fixtures/with-esm-esbuild-config/with-import-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe("suite", () => {
it("passes when esm is supported", () => {
if (typeof import.meta.url !== "string") {
throw new Error("import.meta.url is not a string");
}
});
});
11 changes: 11 additions & 0 deletions test/mocha-web.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,15 @@ describe("mocha-web", function () {
expect(output, output).to.include("body background-color: rgb(0, 255, 0)");
expect(status).to.equal(0);
});

it("supports esm syntax when esbuild config specifies format=esm", () => {
const { output, status } = runMochaPlay({
args: ["./with-import-meta.ts"],
fixture: "with-esm-esbuild-config",
});

expect(output).to.include("Found 1 test files");
expect(output).to.include("1 passing");
expect(status).to.equal(0);
});
});
Loading