Skip to content

Commit

Permalink
chore: change vitest config to be ESM (for sure) to avoid warnings (#…
Browse files Browse the repository at this point in the history
…6055)

Without this we often see:

```
 The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.
```
  • Loading branch information
petebacondarwin authored Jun 18, 2024
1 parent 9487179 commit 68fac09
Show file tree
Hide file tree
Showing 74 changed files with 54 additions and 51 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ As a reviewer, it's important to be thoughtful about the proposed changes and co
Every PR should include tests for the functionality that's being added. Most changes will be to [Wrangler](packages/wrangler/src/__tests__) (using Vitest), [Miniflare](packages/miniflare/test) (using Ava), or [C3](packages/create-cloudflare/src/__tests__) (using Vitest), and should include unit tests within the testing harness of those packages.

If your PR includes functionality that's difficult to unit test, you can add a fixture test by creating a new package in the `fixtures/` folder. This allows for adding a test that requires a specific filesystem or worker setup (for instance, `fixtures/no-bundle-import` tests the interaction of Wrangler with a specific set of JS, WASM, text, and binary modules on the filesystem). When adding a fixture test, include a `vitest.config.ts` file within the new package, which will ensure it's run as part of the `workers-sdk` CI. You should merge your own configuration with the default config from the root of the repo.
If your PR includes functionality that's difficult to unit test, you can add a fixture test by creating a new package in the `fixtures/` folder. This allows for adding a test that requires a specific filesystem or worker setup (for instance, `fixtures/no-bundle-import` tests the interaction of Wrangler with a specific set of JS, WASM, text, and binary modules on the filesystem). When adding a fixture test, include a `vitest.config.mts` file within the new package, which will ensure it's run as part of the `workers-sdk` CI. You should merge your own configuration with the default config from the root of the repo.

A good default example is the following:

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion fixtures/local-mode-tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"target": "esnext",
"strict": true,
"noEmit": true,
"types": ["@cloudflare/workers-types"],
"types": ["@cloudflare/workers-types", "node"],
"lib": ["esnext"],
"skipLibCheck": true
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect, it } from "vitest";

it("dispatches fetch event", async () => {
// `SELF` here points to the worker running in the current isolate.
// This gets its handler from the `main` option in `vitest.config.ts`.
// This gets its handler from the `main` option in `vitest.config.mts`.
// Importantly, it uses the exact `import("../src").default` instance we could
// import in this file as its handler.
const response = await SELF.fetch("http://example.com");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect, it } from "vitest";

it("dispatches scheduled event", async () => {
// `SELF` here points to the worker running in the current isolate.
// This gets its handler from the `main` option in `vitest.config.ts`.
// This gets its handler from the `main` option in `vitest.config.mts`.
// Importantly, it uses the exact `import("../src").default` instance we could
// import in this file as its handler. Note the `SELF.scheduled()` method
// is experimental, and requires the `service_binding_extra_handlers`
Expand Down
2 changes: 1 addition & 1 deletion fixtures/vitest-pool-workers-examples/d1/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 📚 d1

This Worker implements a simple blog using D1 bindings. It uses migrations for setting up the database. These migrations are read in `vitest.config.ts`, and applied with the [test/apply-migrations.ts](test/apply-migrations.ts) setup file.
This Worker implements a simple blog using D1 bindings. It uses migrations for setting up the database. These migrations are read in `vitest.config.mts`, and applied with the [test/apply-migrations.ts](test/apply-migrations.ts) setup file.

| Test | Overview |
| --------------------------------------- | -------------------------------- |
Expand Down
2 changes: 1 addition & 1 deletion fixtures/vitest-pool-workers-examples/d1/test/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module "cloudflare:test" {
// Controls the type of `import("cloudflare:test").env`
interface ProvidedEnv extends Env {
TEST_MIGRATIONS: D1Migration[]; // Defined in `vitest.config.ts`
TEST_MIGRATIONS: D1Migration[]; // Defined in `vitest.config.mts`
}
}
2 changes: 1 addition & 1 deletion fixtures/vitest-pool-workers-examples/hyperdrive/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 🚀 hyperdrive

This Worker establishes a TCP connection with an echo server via Hyperdrive. The echo server is started/stopped by [global-setup.ts](global-setup.ts) on a random port, which is then provided to [vitest.config.ts](vitest.config.ts). In a real worker, Hyperdrive would be used with a database instead.
This Worker establishes a TCP connection with an echo server via Hyperdrive. The echo server is started/stopped by [global-setup.ts](global-setup.ts) on a random port, which is then provided to [vitest.config.mts](vitest.config.mts). In a real worker, Hyperdrive would be used with a database instead.

| Test | Overview |
| --------------------------------- | ----------------------------- |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ compatibility_flags = ["nodejs_compat"]
[[hyperdrive]]
binding = "ECHO_SERVER_HYPERDRIVE"
id = "00000000000000000000000000000000"
localConnectionString = "postgres://user:pass@127.0.0.1/db" # Overridden in `vitest.config.ts`
localConnectionString = "postgres://user:pass@127.0.0.1/db" # Overridden in `vitest.config.mts`
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ it("replaces defines from wrangler.toml", async () => {
expect(WRANGLER_NESTED.DEFINED.THING).toBe(WRANGLER_NESTED.DEFINED.THING);
});

it("replaces defines from vitest.config.ts", async () => {
it("replaces defines from vitest.config.mts", async () => {
expect(CONFIG_DEFINED_THING).toBe("thing");
expect(CONFIG_NESTED.DEFINED.THING).toStrictEqual([1, 2, 3]);
expect(CONFIG_NESTED.DEFINED.THING).toBe(CONFIG_NESTED.DEFINED.THING);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module "cloudflare:test" {
// Controls the type of `import("cloudflare:test").env`
interface ProvidedEnv extends Env {
TEST_AUTH_PUBLIC_KEY: string; // Defined in `vitest.config.ts`
TEST_AUTH_PUBLIC_KEY: string; // Defined in `vitest.config.mts`
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, expect, it } from "vitest";
describe("functions", () => {
it("calls function", async () => {
// `SELF` here points to the worker running in the current isolate.
// This gets its handler from the `main` option in `vitest.config.ts`.
// This gets its handler from the `main` option in `vitest.config.mts`.
const response = await SELF.fetch("http://example.com/api/ping");
// All `/api/*` requests go through `functions/api/_middleware.ts`,
// which makes all response bodies uppercase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect, it } from "vitest";

it("consumes queue messages", async () => {
// `SELF` here points to the worker running in the current isolate.
// This gets its handler from the `main` option in `vitest.config.ts`.
// This gets its handler from the `main` option in `vitest.config.mts`.
// Importantly, it uses the exact `import("../src").default` instance we could
// import in this file as its handler. Note the `SELF.queue()` method
// is experimental, and requires the `service_binding_extra_handlers`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ it("produces queue message with mocked send", async () => {
it("produces queue message with mocked consumer", async () => {
// Intercept calls to `worker.queue()`. Note the runner worker has a queue
// consumer configured that gets its handler from the `main` option in
// `vitest.config.ts`. Importantly, this uses the exact `worker` instance
// `vitest.config.mts`. Importantly, this uses the exact `worker` instance
// we're spying on here.
const consumerSpy = vi
.spyOn(worker, "queue")
Expand Down
2 changes: 1 addition & 1 deletion fixtures/vitest-pool-workers-examples/queues/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ queue = "queue"

[[queues.consumers]]
queue = "queue"
max_batch_timeout = 3 # Overridden in `vitest.config.ts`
max_batch_timeout = 3 # Overridden in `vitest.config.mts`
2 changes: 1 addition & 1 deletion fixtures/vitest-pool-workers-examples/vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default ["*/vitest.config.ts"];
export default ["*/vitest.config.*ts"];
File renamed without changes.
5 changes: 4 additions & 1 deletion lint-turbo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ for (const p of paths) {
}

// Ensure all packages with a vitest config file have a "test:ci" script
if (existsSync(path.join(p, "vitest.config.ts"))) {
if (
existsSync(path.join(p, "vitest.config.ts")) ||
existsSync(path.join(p, "vitest.config.mts"))
) {
assert(
pkg.scripts["test:ci"],
`Package "${p}" is missing a "test:ci" script in package.json`
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions packages/create-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
"test:e2e:pnpm": "pnpm run build && cross-env TEST_PM=pnpm vitest run --config ./vitest-e2e.config.ts",
"test:e2e:bun": "pnpm run build && cross-env TEST_PM=bun vitest run --config ./vitest-e2e.config.ts",
"test:e2e:yarn": "pnpm run build && cross-env TEST_PM=yarn vitest run --config ./vitest-e2e.config.ts",
"test:unit": "vitest run --config ./vitest.config.ts",
"test:unit:watch": "vitest --config ./vitest.config.ts",
"test:unit": "vitest run --config ./vitest.config.mts",
"test:unit:watch": "vitest --config ./vitest.config.mts",
"watch": "node -r esbuild-register scripts/build.ts --watch",
"test:ci": "vitest run --config ./vitest.config.ts"
"test:ci": "vitest run --config ./vitest.config.mts"
},
"devDependencies": {
"@babel/parser": "^7.21.3",
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/vitest-pool-workers/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
},
},
{
files: "test/**/vitest.config.ts",
files: "test/**/vitest.config.*ts",
parserOptions: {
ecmaVersion: 2022,
sourceType: "module",
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest-pool-workers/src/worker/fetch-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ setDispatcher((opts, handler) => {
// worker. We kind of have to do it this way, as `fetchMock` supports functions
// as reply callbacks, and we can't serialise arbitrary functions across worker
// boundaries. For mocking requests in other workers, Miniflare's `fetchMock`
// option can be used in the `vitest.config.ts`.
// option can be used in the `vitest.config.mts`.
globalThis.fetch = async (input, init) => {
const isActive = isMockActive(fetchMock);
if (!isActive) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest-pool-workers/test/chunking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test("chunks large WebSocket messages bi-directionally", async ({
const bigText = "xyz".repeat(400_000);
await seed({
"big.txt": bigText,
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest-pool-workers/test/console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test.skipIf(process.platform === "win32")(
"console.log()s include correct source-mapped locations",
async ({ expect, seed, vitestDev }) => {
await seed({
"vitest.config.ts": minimalVitestConfig,
"vitest.config.mts": minimalVitestConfig,
"index.test.ts": dedent`
import { describe, it } from "vitest";
console.log("global");
Expand Down Expand Up @@ -59,7 +59,7 @@ test("console.logs() inside `export default`ed handlers with SELF", async ({
vitestRun,
}) => {
await seed({
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
Expand Down
8 changes: 4 additions & 4 deletions packages/vitest-pool-workers/test/isolation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test("isolated storage with multiple workers", async ({
}
}
`,
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
import { DeterministicSequencer } from "./sequencer.ts";
Expand Down Expand Up @@ -126,7 +126,7 @@ test("isolated storage with single worker", async ({
}
}
`,
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
import { DeterministicSequencer } from "./sequencer.ts";
Expand Down Expand Up @@ -235,7 +235,7 @@ test("shared storage with multiple workers", async ({
}
}
`,
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
import { DeterministicSequencer } from "./sequencer.ts";
Expand Down Expand Up @@ -327,7 +327,7 @@ test(
}
}
`,
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
import { DeterministicSequencer } from "./sequencer.ts";
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest-pool-workers/test/snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test(
async ({ expect, seed, vitestRun, tmpPath }) => {
// Check writes new snapshots
await seed({
"vitest.config.ts": minimalVitestConfig,
"vitest.config.mts": minimalVitestConfig,
"index.test.ts": dedent`
import { it, expect } from "vitest";
it("matches snapshot", () => {
Expand Down Expand Up @@ -117,7 +117,7 @@ test.skipIf(process.platform === "win32")(
async ({ expect, seed, vitestRun, tmpPath }) => {
// Check writes new snapshots
await seed({
"vitest.config.ts": minimalVitestConfig,
"vitest.config.mts": minimalVitestConfig,
"index.test.ts": dedent`
import { it, expect } from "vitest";
it("matches snapshot", () => {
Expand Down Expand Up @@ -146,7 +146,7 @@ test.skipIf(process.platform === "win32")(

// Check fails if snapshots differ
await seed({
"vitest.config.ts": minimalVitestConfig,
"vitest.config.mts": minimalVitestConfig,
"index.test.ts": dedent`
import { it, expect } from "vitest";
it("matches snapshot", () => {
Expand Down
26 changes: 13 additions & 13 deletions packages/vitest-pool-workers/test/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test(

// Check top-level options validated
await seed({
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
Expand All @@ -29,7 +29,7 @@ test(
let result = await vitestRun();
expect(await result.exitCode).toBe(1);
let expected = dedent`
TypeError: Unexpected pool options in project ${path.join(tmpPathName, "vitest.config.ts")}:
TypeError: Unexpected pool options in project ${path.join(tmpPathName, "vitest.config.mts")}:
{
test: {
poolOptions: {
Expand All @@ -51,7 +51,7 @@ test(

// Check `miniflare` options validated with correct error paths
await seed({
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
Expand All @@ -70,7 +70,7 @@ test(
result = await vitestRun();
expect(await result.exitCode).toBe(1);
expected = dedent`
TypeError: Unexpected pool options in project ${path.join(tmpPathName, "vitest.config.ts")}:
TypeError: Unexpected pool options in project ${path.join(tmpPathName, "vitest.config.mts")}:
{
test: {
poolOptions: {
Expand All @@ -96,7 +96,7 @@ test(

// Check messages without Wrangler configuration path defined
await seed({
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({});
`,
Expand All @@ -105,13 +105,13 @@ test(
let result = await vitestRun();
expect(await result.exitCode).toBe(1);
let expected = dedent`
Error: In project ${path.join(tmpPathName, "vitest.config.ts")}, \`test.poolOptions.workers.miniflare.compatibilityFlags\` must contain "export_commonjs_default", or \`test.poolOptions.workers.miniflare.compatibilityDate\` must be >= "2022-10-31".
Error: In project ${path.join(tmpPathName, "vitest.config.mts")}, \`test.poolOptions.workers.miniflare.compatibilityFlags\` must contain "export_commonjs_default", or \`test.poolOptions.workers.miniflare.compatibilityDate\` must be >= "2022-10-31".
This flag is required to use \`@cloudflare/vitest-pool-workers\`.
`.replaceAll("\t", " ");
expect(result.stderr).toMatch(expected);

await seed({
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
Expand All @@ -127,14 +127,14 @@ test(
result = await vitestRun();
expect(await result.exitCode).toBe(1);
expected = dedent`
Error: In project ${path.join(tmpPathName, "vitest.config.ts")}, \`test.poolOptions.workers.miniflare.compatibilityFlags\` must contain "nodejs_compat".
Error: In project ${path.join(tmpPathName, "vitest.config.mts")}, \`test.poolOptions.workers.miniflare.compatibilityFlags\` must contain "nodejs_compat".
This flag is required to use \`@cloudflare/vitest-pool-workers\`.
`.replaceAll("\t", " ");
expect(result.stderr).toMatch(expected);

// Check messages with Wrangler configuration path defined
await seed({
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
Expand All @@ -151,7 +151,7 @@ test(
result = await vitestRun();
expect(await result.exitCode).toBe(1);
expected = dedent`
Error: In project ${path.join(tmpPathName, "vitest.config.ts")}'s configuration file ${path.join(tmpPathName, "wrangler.toml")}, \`compatibility_flags\` must contain "export_commonjs_default", or \`compatibility_date\` must be >= "2022-10-31".
Error: In project ${path.join(tmpPathName, "vitest.config.mts")}'s configuration file ${path.join(tmpPathName, "wrangler.toml")}, \`compatibility_flags\` must contain "export_commonjs_default", or \`compatibility_date\` must be >= "2022-10-31".
This flag is required to use \`@cloudflare/vitest-pool-workers\`.
`.replaceAll("\t", " ");
expect(result.stderr).toMatch(expected);
Expand All @@ -164,7 +164,7 @@ test(
result = await vitestRun();
expect(await result.exitCode).toBe(1);
expected = dedent`
Error: In project ${path.join(tmpPathName, "vitest.config.ts")}'s configuration file ${path.join(tmpPathName, "wrangler.toml")}, \`compatibility_flags\` must contain "nodejs_compat".
Error: In project ${path.join(tmpPathName, "vitest.config.mts")}'s configuration file ${path.join(tmpPathName, "wrangler.toml")}, \`compatibility_flags\` must contain "nodejs_compat".
This flag is required to use \`@cloudflare/vitest-pool-workers\`.
`.replaceAll("\t", " ");
expect(result.stderr).toMatch(expected);
Expand All @@ -179,7 +179,7 @@ test(

// Check with no entrypoint
await seed({
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
Expand Down Expand Up @@ -213,7 +213,7 @@ test(

// Check with service worker
await seed({
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest-pool-workers/test/watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test("automatically re-runs unit tests", async ({
vitestDev,
}) => {
await seed({
"vitest.config.ts": minimalVitestConfig,
"vitest.config.mts": minimalVitestConfig,
"index.ts": dedent`
export default {
async fetch(request, env, ctx) {
Expand Down Expand Up @@ -54,7 +54,7 @@ test("automatically re-runs integration tests", async ({
vitestDev,
}) => {
await seed({
"vitest.config.ts": dedent`
"vitest.config.mts": dedent`
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest-pool-workers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"./src/pool/**/*.ts",
"./src/shared/**/*.ts",
"./test/**/*.ts",
"./vitest.config.ts",
"./vitest.config.*ts",
"./vitest.workspace.ts"
]
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 68fac09

Please sign in to comment.