* `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>
This commit is contained in:
Jarred Sumner
2023-11-15 06:00:28 +01:00
committed by GitHub
parent 5946e13e27
commit e6e4ffb4ae
14 changed files with 80 additions and 3 deletions

View File

@@ -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}")

View File

@@ -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<void>;
/**
* Restore the previous value of mocks.
*/
restore(): void;
};
/**

View File

@@ -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");
});
});

View File

@@ -0,0 +1,2 @@
import { trim } from "lodash";
export const a = () => trim(" XXX ");

View File

@@ -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");
});
});

View File

@@ -0,0 +1,2 @@
import { trim } from "lodash";
export const b = () => trim(" XXX ");

View File

@@ -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
});

View File

@@ -0,0 +1 @@
export default "hello";

View File

@@ -0,0 +1,3 @@
import foo from "./first";
export { foo };

View File

@@ -0,0 +1,3 @@
import hello from "./first";
export const bar = hello;

View File

@@ -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";

View File

@@ -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 {

View File

@@ -0,0 +1 @@
export const rexported = 42;