Skip to content

Commit 1a4b4ba

Browse files
authored
fix: ensure console methods are bound to instance (#6633)
* fix: ensure console methods are bound to instance * chore: add changeset * chore: better description --------- Co-authored-by: Andy Jessop <[email protected]>
1 parent 8d1d464 commit 1a4b4ba

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

.changeset/real-cooks-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
Ensures console methods are bound to their instance in Vitest Pool Worker tests

packages/vitest-pool-workers/src/worker/lib/node/console.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ export class Console {
3333
const colors =
3434
typeof opts.colorMode === "string" ? false : opts.colorMode ?? false;
3535
this.#inspectOptions = opts.inspectOptions ?? { colors };
36+
37+
// Ensure methods are bound to the instance
38+
return new Proxy(this, {
39+
get(target, prop) {
40+
const value = target[prop as keyof Console];
41+
if (typeof value === "function") {
42+
return value.bind(target);
43+
}
44+
return value;
45+
},
46+
});
3647
}
3748

3849
// Vitest expects this function to be called `value`:

packages/vitest-pool-workers/test/console.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ test.skipIf(process.platform === "win32")(
5353
}
5454
);
5555

56+
test("handles detatched console methods", async ({
57+
expect,
58+
seed,
59+
vitestDev,
60+
}) => {
61+
await seed({
62+
"vitest.config.mts": minimalVitestConfig,
63+
"index.test.ts": dedent`
64+
import { SELF } from "cloudflare:test";
65+
import { expect, it } from "vitest";
66+
it("does not crash when using a detached console method", async () => {
67+
const fn = console["debug"];
68+
fn("Does not crash");
69+
expect(true).toBe(true);
70+
});
71+
`,
72+
});
73+
const result = vitestDev();
74+
expect(result.stderr).toMatch("");
75+
});
76+
5677
test("console.logs() inside `export default`ed handlers with SELF", async ({
5778
expect,
5879
seed,

0 commit comments

Comments
 (0)