Skip to content

Commit 68dbdb7

Browse files
committed
feat(vitest-plugin): support esm only
1 parent f3111d0 commit 68dbdb7

File tree

7 files changed

+152
-118
lines changed

7 files changed

+152
-118
lines changed

packages/vitest-plugin/package.json

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
"vitest",
99
"performance"
1010
],
11-
"main": "dist/index.es5.js",
12-
"types": "dist/index.d.ts",
11+
"module": "./dist/index.mjs",
12+
"types": "./dist/index.d.ts",
13+
"exports": {
14+
".": {
15+
"import": "./dist/index.mjs"
16+
}
17+
},
1318
"type": "module",
1419
"files": [
1520
"dist"
@@ -24,14 +29,14 @@
2429
"dependencies": {
2530
"@codspeed/core": "workspace:^2.2.0"
2631
},
32+
"peerDependencies": {
33+
"vitest": ">=1.0.0-beta.4 || >=1",
34+
"vite": "^4.2.0 || ^5.0.0"
35+
},
2736
"devDependencies": {
2837
"@total-typescript/shoehorn": "^0.1.1",
2938
"execa": "^8.0.1",
30-
"vite": "^4.5.0",
39+
"vite": "^5.0.0",
3140
"vitest": "1.0.0-beta.4"
32-
},
33-
"peerDependencies": {
34-
"vitest": ">=1.0.0-beta.4 || >=1",
35-
"vite": ">=4"
3641
}
3742
}

packages/vitest-plugin/rollup.config.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import pkg from "./package.json" assert { type: "json" };
55
export default defineConfig([
66
{
77
input: "src/index.ts",
8-
// for some reasons, vitest only wants to require the `main` entrypoint
9-
// but fails when its CJS since it cannot require `vitest/*` modules, as
10-
// they are ESM only 🤷
11-
// we can circumvent this by exposing the `main` entrypoint as ESM
12-
output: { file: pkg.main, format: "es" },
8+
output: { file: pkg.module, format: "es" },
139
plugins: jsPlugins(pkg.version),
1410
external: ["@codspeed/core", /^vitest/],
1511
},
@@ -20,13 +16,13 @@ export default defineConfig([
2016
},
2117
{
2218
input: "src/globalSetup.ts",
23-
output: { file: "dist/globalSetup.es5.js", format: "es" },
19+
output: { file: "dist/globalSetup.mjs", format: "es" },
2420
plugins: jsPlugins(pkg.version),
2521
external: ["@codspeed/core", /^vitest/],
2622
},
2723
{
2824
input: "src/runner.ts",
29-
output: { file: "dist/runner.es5.js", format: "es" },
25+
output: { file: "dist/runner.mjs", format: "es" },
3026
plugins: jsPlugins(pkg.version),
3127
external: ["@codspeed/core", /^vitest/],
3228
},

packages/vitest-plugin/src/__tests__/globalSetup.test.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import { describe, expect, it, vi } from "vitest";
2-
import globalSetup from "../globalSetup";
32

43
console.log = vi.fn();
54

6-
vi.stubGlobal("__VERSION__", "1.0.0");
5+
async function importFreshGlobalSetup() {
6+
return (
7+
(await import(
8+
`../globalSetup?${Date.now()}`
9+
)) as typeof import("../globalSetup")
10+
).default;
11+
}
712

8-
describe("globalSetup", () => {
9-
it("should log the correct message on setup and teardown, and fail when teardown is called twice", async () => {
13+
describe("globalSetup", async () => {
14+
it("with version, should log the correct message on setup and teardown, and fail when teardown is called twice", async () => {
15+
vi.stubGlobal("__VERSION__", "1.0.0");
16+
const globalSetup = await importFreshGlobalSetup();
1017
const teardown = globalSetup();
1118

1219
expect(console.log).toHaveBeenCalledWith(
@@ -21,4 +28,22 @@ describe("globalSetup", () => {
2128

2229
expect(() => teardown()).toThrowError("teardown called twice");
2330
});
31+
32+
it("without version, should log the correct message on setup and teardown, and fail when teardown is called twice", async () => {
33+
vi.unstubAllGlobals();
34+
const globalSetup = await importFreshGlobalSetup();
35+
const teardown = globalSetup();
36+
37+
expect(console.log).toHaveBeenCalledWith(
38+
"[CodSpeed] @codspeed/vitest-plugin - setup"
39+
);
40+
41+
teardown();
42+
43+
expect(console.log).toHaveBeenCalledWith(
44+
"[CodSpeed] @codspeed/vitest-plugin - teardown"
45+
);
46+
47+
expect(() => teardown()).toThrowError("teardown called twice");
48+
});
2449
});

packages/vitest-plugin/src/__tests__/index.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ describe("codSpeedPlugin", () => {
7777
expect(config({}, fromPartial({}))).toStrictEqual({
7878
test: {
7979
globalSetup: [
80-
expect.stringContaining(
81-
"packages/vitest-plugin/src/globalSetup.es5.js"
82-
),
80+
expect.stringContaining("packages/vitest-plugin/src/globalSetup.ts"),
8381
],
8482
pool: "forks",
8583
poolOptions: {
@@ -95,9 +93,7 @@ describe("codSpeedPlugin", () => {
9593
],
9694
},
9795
},
98-
runner: expect.stringContaining(
99-
"packages/vitest-plugin/src/runner.es5.js"
100-
),
96+
runner: expect.stringContaining("packages/vitest-plugin/src/runner.ts"),
10197
},
10298
});
10399
});

packages/vitest-plugin/src/globalSetup.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
declare const __VERSION__: string;
1+
declare const __VERSION__: string | undefined;
22

33
/**
44
* @deprecated
@@ -12,12 +12,15 @@ function logCodSpeed(message: string) {
1212
let teardownHappened = false;
1313

1414
export default function () {
15-
logCodSpeed(`@codspeed/vitest-plugin v${__VERSION__} - setup`);
15+
// log the version of the plugin if the global variable is defined
16+
const VERSION = typeof __VERSION__ === "string" ? `v${__VERSION__} ` : "";
17+
18+
logCodSpeed(`@codspeed/vitest-plugin ${VERSION}- setup`);
1619

1720
return () => {
1821
if (teardownHappened) throw new Error("teardown called twice");
1922
teardownHappened = true;
2023

21-
logCodSpeed(`@codspeed/vitest-plugin v${__VERSION__} - teardown`);
24+
logCodSpeed(`@codspeed/vitest-plugin ${VERSION}- teardown`);
2225
};
2326
}

packages/vitest-plugin/src/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import { getV8Flags, Measurement } from "@codspeed/core";
2+
import { join } from "path";
23
import { Plugin } from "vite";
34
import { UserConfig } from "vitest/config";
45

6+
// get this file's directory path from import.meta.url
7+
const __dirname = new URL(".", import.meta.url).pathname;
8+
const isFileInTs = import.meta.url.endsWith(".ts");
9+
10+
function getCodSpeedFileFromName(name: string) {
11+
const fileExtension = isFileInTs ? "ts" : "mjs";
12+
13+
return join(__dirname, `${name}.${fileExtension}`);
14+
}
15+
516
export default function codspeedPlugin(): Plugin {
617
return {
718
name: "codspeed:vitest",
@@ -18,8 +29,6 @@ export default function codspeedPlugin(): Plugin {
1829
return true;
1930
},
2031
enforce: "post",
21-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
22-
// @ts-ignore - vite does not support vitest config yet in typings
2332
config(): UserConfig {
2433
return {
2534
test: {
@@ -29,8 +38,8 @@ export default function codspeedPlugin(): Plugin {
2938
execArgv: getV8Flags(),
3039
},
3140
},
32-
runner: `${__dirname}/runner.es5.js`,
33-
globalSetup: [`${__dirname}/globalSetup.es5.js`],
41+
runner: getCodSpeedFileFromName("runner"),
42+
globalSetup: [getCodSpeedFileFromName("globalSetup")],
3443
},
3544
};
3645
},

0 commit comments

Comments
 (0)