From e6e4ffb4aebaf506a9f046a17c2b4e4b95f49e50 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Wed, 15 Nov 2023 06:00:28 +0100 Subject: [PATCH] Fixes #6789 (#7129) * `zig fmt` * Fixes #6879 * Update bun-test.d.ts * More tests * Bump WebKit * [autofix.ci] apply automated fixes * woops --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- CMakeLists.txt | 2 +- packages/bun-types/bun-test.d.ts | 4 ++++ src/bun.js/WebKit | 2 +- test/js/bun/test/mock/6874/A.test.ts | 10 ++++++++++ test/js/bun/test/mock/6874/A.ts | 2 ++ test/js/bun/test/mock/6874/B.test.ts | 10 ++++++++++ test/js/bun/test/mock/6874/B.ts | 2 ++ test/js/bun/test/mock/6879/6879.test.ts | 15 ++++++++++++++ test/js/bun/test/mock/6879/first.ts | 1 + test/js/bun/test/mock/6879/second.ts | 3 +++ test/js/bun/test/mock/6879/third.ts | 3 +++ test/js/bun/test/mock/mock-module-fixture.ts | 7 +++++++ test/js/bun/test/mock/mock-module.test.ts | 21 +++++++++++++++++++- test/js/bun/test/mock/re-export-fixture.ts | 1 + 14 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 test/js/bun/test/mock/6874/A.test.ts create mode 100644 test/js/bun/test/mock/6874/A.ts create mode 100644 test/js/bun/test/mock/6874/B.test.ts create mode 100644 test/js/bun/test/mock/6874/B.ts create mode 100644 test/js/bun/test/mock/6879/6879.test.ts create mode 100644 test/js/bun/test/mock/6879/first.ts create mode 100644 test/js/bun/test/mock/6879/second.ts create mode 100644 test/js/bun/test/mock/6879/third.ts create mode 100644 test/js/bun/test/mock/re-export-fixture.ts diff --git a/CMakeLists.txt b/CMakeLists.txt index 29ab2a77b3..b4ae2099f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_policy(SET CMP0091 NEW) cmake_policy(SET CMP0067 NEW) set(Bun_VERSION "1.0.11") -set(WEBKIT_TAG 16badcb5df6b1190051b4b3caa1a5aeb4e2fc441) +set(WEBKIT_TAG 63d0e18c06399180e9b3fb63d2b9132af5e79ed4) set(BUN_WORKDIR "${CMAKE_CURRENT_BINARY_DIR}") message(STATUS "Configuring Bun ${Bun_VERSION} in ${BUN_WORKDIR}") diff --git a/packages/bun-types/bun-test.d.ts b/packages/bun-types/bun-test.d.ts index 50cc8129a8..05594e5945 100644 --- a/packages/bun-types/bun-test.d.ts +++ b/packages/bun-types/bun-test.d.ts @@ -59,6 +59,10 @@ declare module "bun:test" { * added to existing import statements. This is due to how ESM works. */ module(id: string, factory: () => any): void | Promise; + /** + * Restore the previous value of mocks. + */ + restore(): void; }; /** diff --git a/src/bun.js/WebKit b/src/bun.js/WebKit index 7cd84abfa7..63d0e18c06 160000 --- a/src/bun.js/WebKit +++ b/src/bun.js/WebKit @@ -1 +1 @@ -Subproject commit 7cd84abfa787e4b96b27c3ef1e28a4eb1aa49aa1 +Subproject commit 63d0e18c06399180e9b3fb63d2b9132af5e79ed4 diff --git a/test/js/bun/test/mock/6874/A.test.ts b/test/js/bun/test/mock/6874/A.test.ts new file mode 100644 index 0000000000..b2f3ab5f82 --- /dev/null +++ b/test/js/bun/test/mock/6874/A.test.ts @@ -0,0 +1,10 @@ +import { expect, it, mock, describe } from "bun:test"; +import { a } from "./A.ts"; + +mock.module(require.resolve("lodash"), () => ({ trim: () => "mocked" })); + +describe("A", () => { + it("should be mocked", () => { + expect(a()).toEqual("mocked"); + }); +}); diff --git a/test/js/bun/test/mock/6874/A.ts b/test/js/bun/test/mock/6874/A.ts new file mode 100644 index 0000000000..2f22c5bffd --- /dev/null +++ b/test/js/bun/test/mock/6874/A.ts @@ -0,0 +1,2 @@ +import { trim } from "lodash"; +export const a = () => trim(" XXX "); diff --git a/test/js/bun/test/mock/6874/B.test.ts b/test/js/bun/test/mock/6874/B.test.ts new file mode 100644 index 0000000000..cd386824b7 --- /dev/null +++ b/test/js/bun/test/mock/6874/B.test.ts @@ -0,0 +1,10 @@ +import { expect, it, mock, describe } from "bun:test"; +import { b } from "./B.ts"; + +mock.module(require.resolve("lodash"), () => ({ trim: () => "mocked" })); + +describe("B", () => { + it("should be mocked", () => { + expect(b()).toEqual("mocked"); + }); +}); diff --git a/test/js/bun/test/mock/6874/B.ts b/test/js/bun/test/mock/6874/B.ts new file mode 100644 index 0000000000..b69cf22cc7 --- /dev/null +++ b/test/js/bun/test/mock/6874/B.ts @@ -0,0 +1,2 @@ +import { trim } from "lodash"; +export const b = () => trim(" XXX "); diff --git a/test/js/bun/test/mock/6879/6879.test.ts b/test/js/bun/test/mock/6879/6879.test.ts new file mode 100644 index 0000000000..fc34283002 --- /dev/null +++ b/test/js/bun/test/mock/6879/6879.test.ts @@ -0,0 +1,15 @@ +import { expect, mock, test } from "bun:test"; +import { foo } from "./second"; +import { bar } from "./third"; + +test("mocks re-export from export list", () => { + expect(foo).toBe("hello"); + mock.module("./second.ts", () => ({ foo: "world" })); + expect(foo).toBe("world"); // success +}); + +test("mocks named re-export", () => { + expect(bar).toBe("hello"); + mock.module("./third.ts", () => ({ bar: "world" })); + expect(bar).toBe("world"); // success +}); diff --git a/test/js/bun/test/mock/6879/first.ts b/test/js/bun/test/mock/6879/first.ts new file mode 100644 index 0000000000..34b58e6e12 --- /dev/null +++ b/test/js/bun/test/mock/6879/first.ts @@ -0,0 +1 @@ +export default "hello"; diff --git a/test/js/bun/test/mock/6879/second.ts b/test/js/bun/test/mock/6879/second.ts new file mode 100644 index 0000000000..ba43d198db --- /dev/null +++ b/test/js/bun/test/mock/6879/second.ts @@ -0,0 +1,3 @@ +import foo from "./first"; + +export { foo }; diff --git a/test/js/bun/test/mock/6879/third.ts b/test/js/bun/test/mock/6879/third.ts new file mode 100644 index 0000000000..05882b62a9 --- /dev/null +++ b/test/js/bun/test/mock/6879/third.ts @@ -0,0 +1,3 @@ +import hello from "./first"; + +export const bar = hello; diff --git a/test/js/bun/test/mock/mock-module-fixture.ts b/test/js/bun/test/mock/mock-module-fixture.ts index 1bc820d5c7..3255606869 100644 --- a/test/js/bun/test/mock/mock-module-fixture.ts +++ b/test/js/bun/test/mock/mock-module-fixture.ts @@ -1,3 +1,5 @@ +import { rexported } from "./re-export-fixture"; + export function fn() { return 42; } @@ -7,3 +9,8 @@ export function iCallFn() { } export const variable = 7; + +export default "original"; +export { rexported }; + +export { rexported as rexportedAs } from "./re-export-fixture"; diff --git a/test/js/bun/test/mock/mock-module.test.ts b/test/js/bun/test/mock/mock-module.test.ts index 624af848a7..2e516156ca 100644 --- a/test/js/bun/test/mock/mock-module.test.ts +++ b/test/js/bun/test/mock/mock-module.test.ts @@ -8,7 +8,7 @@ // - Write test for import {foo} from "./foo"; export {foo} import { expect, mock, spyOn, test } from "bun:test"; -import { fn, iCallFn, variable } from "./mock-module-fixture"; +import { fn, iCallFn, variable, default as defaultValue, rexported, rexportedAs } from "./mock-module-fixture"; import * as spyFixture from "./spymodule-fixture"; test("mock.restore", () => { @@ -17,6 +17,7 @@ test("mock.restore", () => { const mocked = spyFixture.iSpy; expect(spyFixture.iSpy).not.toBe(original); expect(spyFixture.iSpy).not.toHaveBeenCalled(); + // @ts-expect-error spyFixture.iSpy(); mock.restore(); expect(spyFixture.iSpy).toBe(original); @@ -32,15 +33,24 @@ test("spyOn", () => { test("mocking a local file", async () => { expect(fn()).toEqual(42); expect(variable).toEqual(7); + expect(defaultValue).toEqual("original"); + expect(rexported).toEqual(42); mock.module("./mock-module-fixture.ts", () => { return { fn: () => 1, variable: 8, + default: 42, + rexported: 43, }; }); expect(fn()).toEqual(1); expect(variable).toEqual(8); + // @ts-expect-error + // expect(defaultValue).toEqual(42); + expect(rexported).toEqual(43); + expect(rexportedAs).toEqual(43); + expect((await import("./re-export-fixture")).rexported).toEqual(43); mock.module("./mock-module-fixture.ts", () => { return { fn: () => 2, @@ -62,6 +72,15 @@ test("mocking a local file", async () => { expect(iCallFn()).toBe(3); }); +test.todo("adding a default on a module with no default", async () => { + mock.module("./re-export-fixture.ts", () => { + return { + default: 42, + }; + }); + expect((await import("./re-export-fixture")).default).toBe(42); +}); + test("mocking a package", async () => { mock.module("ha-ha-ha", () => { return { diff --git a/test/js/bun/test/mock/re-export-fixture.ts b/test/js/bun/test/mock/re-export-fixture.ts new file mode 100644 index 0000000000..382d24b29b --- /dev/null +++ b/test/js/bun/test/mock/re-export-fixture.ts @@ -0,0 +1 @@ +export const rexported = 42;