Skip to content

Commit

Permalink
update types to match vitest v2
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Sep 13, 2024
1 parent ce23e11 commit 4095808
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions fixtures/local-mode-tests/tests/logging.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import path from "node:path";
import { setTimeout } from "node:timers/promises";
import util from "node:util";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { afterEach, beforeEach, expect, it, Mock, vi } from "vitest";
import { unstable_dev } from "wrangler";

let output = "";
function spyOnConsoleMethod(name: keyof typeof console) {
vi.spyOn(console, name).mockImplementation((...args: unknown[]) => {
(vi.spyOn(console, name) as Mock).mockImplementation((...args: unknown[]) => {
output += util.format(...args) + "\n";
});
}
Expand Down
2 changes: 2 additions & 0 deletions packages/create-cloudflare/e2e-tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from "assert";
import {
createWriteStream,
mkdirSync,
Expand Down Expand Up @@ -284,6 +285,7 @@ export const waitForExit = async (
export const createTestLogStream = (ctx: TaskContext) => {
// The .ansi extension allows for editor extensions that format ansi terminal codes
const fileName = `${normalizeTestName(ctx)}.ansi`;
assert(ctx.task.suite, "Suite must be defined");
return createWriteStream(path.join(getLogPath(ctx.task.suite), fileName), {
flags: "a",
});
Expand Down
19 changes: 15 additions & 4 deletions packages/vitest-pool-workers/src/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ import type {
import type { Readable } from "node:stream";
import type { MessagePort } from "node:worker_threads";
import type {
ResolvedConfig,
RunnerRPC,
RuntimeRPC,
SerializedConfig,
WorkerContext,
} from "vitest";
import type { ProcessPool, Vitest, WorkspaceProject } from "vitest/node";

interface SerializedOptions {
// Defined in `src/pool/index.ts`
main?: string;
durableObjectBindingDesignators?: Map<
string /* bound name */,
DurableObjectDesignator
>;
isolatedStorage?: boolean;
}

// https://github.com/vitest-dev/vitest/blob/v2.0.5/packages/vite-node/src/client.ts#L468
declare const __vite_ssr_import__: unknown;
assert(
Expand Down Expand Up @@ -678,7 +688,7 @@ async function runTests(
mf: Miniflare,
workerName: string,
project: Project,
config: ResolvedConfig,
config: SerializedConfig,
files: string[],
invalidates: string[] = []
) {
Expand Down Expand Up @@ -970,6 +980,7 @@ export default function (ctx: Vitest): ProcessPool {
// serialisable. `getSerializableConfig()` may also return references to
// the same objects, so override it with a new object.
config.poolOptions = {
// @ts-expect-error Vitest provides no way to extend this type
threads: {
// Allow workers to be re-used by removing the isolation requirement
isolate: false,
Expand All @@ -991,7 +1002,7 @@ export default function (ctx: Vitest): ProcessPool {
// project, so we know whether to call out to the loopback service
// to push/pop the storage stack between tests.
isolatedStorage: project.options.isolatedStorage,
},
} satisfies SerializedOptions,
};

const mf = await getProjectMiniflare(ctx, project);
Expand Down Expand Up @@ -1063,7 +1074,7 @@ export default function (ctx: Vitest): ProcessPool {
// assert(testModule && thingModule);
// thingModule.importers.add(testModule);
},
async collectTests(specs, invalidates) {
async collectTests(_specs, _invalidates) {
// TODO: This is a new API introduced in Vitest v2+ which we should consider supporting at some point
throw new Error(
"The Cloudflare Workers Vitest integration does not support the `.collect()` or `vitest list` APIs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { VitestTestRunner } from "vitest/runners";
import workerdUnsafe from "workerd:unsafe";
import type { CancelReason, Suite, Test } from "@vitest/runner";
import type { ResolvedConfig, WorkerGlobalState, WorkerRPC } from "vitest";
import type { SerializedConfig, WorkerGlobalState, WorkerRPC } from "vitest";

// When `DEBUG` is `true`, runner operations will be logged and slowed down
// TODO(soon): remove this
Expand Down Expand Up @@ -114,7 +114,7 @@ export default class WorkersTestRunner extends VitestTestRunner {
readonly state: WorkerGlobalState;
readonly isolatedStorage: boolean;

constructor(config: ResolvedConfig) {
constructor(config: SerializedConfig) {
super(config);

// @ts-expect-error `this.workerState` has "private" access, how quaint :D
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/__tests__/vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ vi.mock("execa", async (importOriginal) => {
const realModule = await importOriginal<typeof import("execa")>();
return {
...realModule,
execa: vi.fn<Parameters<typeof realModule.execa>>((...args) => {
execa: vi.fn((...args: Parameters<typeof realModule.execa>) => {
return args[0] === "mockpm"
? Promise.resolve()
: realModule.execa(...args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ describe("writeChangeSet()", () => {
`;
writeChangeSet("some-prefix", "1234", header, body);
expect(writeFileSync).toHaveBeenCalledOnce();
expect((writeFileSync as Mock).mock.lastCall[0]).toMatchInlineSnapshot(
expect((writeFileSync as Mock).mock.lastCall?.[0]).toMatchInlineSnapshot(
`".changeset/some-prefix-1234.md"`
);
expect((writeFileSync as Mock).mock.lastCall[1]).toMatchInlineSnapshot(`
expect((writeFileSync as Mock).mock.lastCall?.[1]).toMatchInlineSnapshot(`
"---
"package-name": patch
---
Expand Down

0 comments on commit 4095808

Please sign in to comment.