Skip to content

Commit 6a2768f

Browse files
Implement mock.module in bun:test (oven-sh#6685)
* Implement Module Mocks * Update javascript.zig * Add test for spyOn + ESM namespace * Bump WebKit * Bump WebKit * Drain microtasks at end of test executions --------- Co-authored-by: Jarred Sumner <[email protected]>
1 parent 7bcf603 commit 6a2768f

27 files changed

+664
-84
lines changed

.github/workflows/bun-linux-aarch64.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
arch: aarch64
3939
build_arch: arm64
4040
runner: linux-arm64
41-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-linux-arm64-lto.tar.gz"
41+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-linux-arm64-lto.tar.gz"
4242
webkit_basename: "bun-webkit-linux-arm64-lto"
4343
build_machine_arch: aarch64
4444

.github/workflows/bun-linux-build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ jobs:
4949
arch: x86_64
5050
build_arch: amd64
5151
runner: big-ubuntu
52-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-linux-amd64-lto.tar.gz"
52+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-linux-amd64-lto.tar.gz"
5353
webkit_basename: "bun-webkit-linux-amd64-lto"
5454
build_machine_arch: x86_64
5555
- cpu: nehalem
5656
tag: linux-x64-baseline
5757
arch: x86_64
5858
build_arch: amd64
5959
runner: big-ubuntu
60-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-linux-amd64-lto.tar.gz"
60+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-linux-amd64-lto.tar.gz"
6161
webkit_basename: "bun-webkit-linux-amd64-lto"
6262
build_machine_arch: x86_64
6363

.github/workflows/bun-mac-aarch64.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ jobs:
119119
# obj: bun-obj-darwin-x64-baseline
120120
# runner: macos-12
121121
# artifact: bun-obj-darwin-x64-baseline
122-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
122+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
123123
# dependencies: true
124124
# compile_obj: false
125125
# - cpu: haswell
@@ -128,7 +128,7 @@ jobs:
128128
# obj: bun-obj-darwin-x64
129129
# runner: macos-12
130130
# artifact: bun-obj-darwin-x64
131-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
131+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
132132
# dependencies: true
133133
# compile_obj: false
134134
# - cpu: nehalem
@@ -137,7 +137,7 @@ jobs:
137137
# obj: bun-obj-darwin-x64-baseline
138138
# runner: macos-12
139139
# artifact: bun-obj-darwin-x64-baseline
140-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
140+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
141141
# dependencies: false
142142
# compile_obj: true
143143
# - cpu: haswell
@@ -146,15 +146,15 @@ jobs:
146146
# obj: bun-obj-darwin-x64
147147
# runner: macos-12
148148
# artifact: bun-obj-darwin-x64
149-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
149+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
150150
# dependencies: false
151151
# compile_obj: true
152152
- cpu: native
153153
arch: aarch64
154154
tag: bun-darwin-aarch64
155155
obj: bun-obj-darwin-aarch64
156156
artifact: bun-obj-darwin-aarch64
157-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-arm64-lto.tar.gz"
157+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-arm64-lto.tar.gz"
158158
runner: macos-arm64
159159
dependencies: true
160160
compile_obj: true
@@ -260,22 +260,22 @@ jobs:
260260
# package: bun-darwin-x64
261261
# runner: macos-12
262262
# artifact: bun-obj-darwin-x64-baseline
263-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
263+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
264264
# - cpu: haswell
265265
# arch: x86_64
266266
# tag: bun-darwin-x64
267267
# obj: bun-obj-darwin-x64
268268
# package: bun-darwin-x64
269269
# runner: macos-12
270270
# artifact: bun-obj-darwin-x64
271-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
271+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
272272
- cpu: native
273273
arch: aarch64
274274
tag: bun-darwin-aarch64
275275
obj: bun-obj-darwin-aarch64
276276
package: bun-darwin-aarch64
277277
artifact: bun-obj-darwin-aarch64
278-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-arm64-lto.tar.gz"
278+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-arm64-lto.tar.gz"
279279
runner: macos-arm64
280280
steps:
281281
- uses: actions/checkout@v3

.github/workflows/bun-mac-x64-baseline.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ jobs:
119119
obj: bun-obj-darwin-x64-baseline
120120
runner: macos-12
121121
artifact: bun-obj-darwin-x64-baseline
122-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
122+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
123123
dependencies: true
124124
compile_obj: false
125125
# - cpu: haswell
@@ -128,7 +128,7 @@ jobs:
128128
# obj: bun-obj-darwin-x64
129129
# runner: macos-12
130130
# artifact: bun-obj-darwin-x64
131-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
131+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
132132
# dependencies: true
133133
# compile_obj: false
134134
- cpu: nehalem
@@ -137,7 +137,7 @@ jobs:
137137
obj: bun-obj-darwin-x64-baseline
138138
runner: macos-12
139139
artifact: bun-obj-darwin-x64-baseline
140-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
140+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
141141
dependencies: false
142142
compile_obj: true
143143
# - cpu: haswell
@@ -146,15 +146,15 @@ jobs:
146146
# obj: bun-obj-darwin-x64
147147
# runner: macos-12
148148
# artifact: bun-obj-darwin-x64
149-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
149+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
150150
# dependencies: false
151151
# compile_obj: true
152152
# - cpu: native
153153
# arch: aarch64
154154
# tag: bun-darwin-aarch64
155155
# obj: bun-obj-darwin-aarch64
156156
# artifact: bun-obj-darwin-aarch64
157-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
157+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
158158
# runner: macos-arm64
159159
# dependencies: true
160160
# compile_obj: true
@@ -261,22 +261,22 @@ jobs:
261261
package: bun-darwin-x64
262262
runner: macos-12
263263
artifact: bun-obj-darwin-x64-baseline
264-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
264+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
265265
# - cpu: haswell
266266
# arch: x86_64
267267
# tag: bun-darwin-x64
268268
# obj: bun-obj-darwin-x64
269269
# package: bun-darwin-x64
270270
# runner: macos-12
271271
# artifact: bun-obj-darwin-x64
272-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
272+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
273273
# - cpu: native
274274
# arch: aarch64
275275
# tag: bun-darwin-aarch64
276276
# obj: bun-obj-darwin-aarch64
277277
# package: bun-darwin-aarch64
278278
# artifact: bun-obj-darwin-aarch64
279-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
279+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
280280
# runner: macos-arm64
281281
steps:
282282
- uses: actions/checkout@v3

.github/workflows/bun-mac-x64.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ jobs:
119119
# obj: bun-obj-darwin-x64-baseline
120120
# runner: macos-12
121121
# artifact: bun-obj-darwin-x64-baseline
122-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
122+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
123123
# dependencies: true
124124
# compile_obj: false
125125
- cpu: haswell
@@ -128,7 +128,7 @@ jobs:
128128
obj: bun-obj-darwin-x64
129129
runner: macos-12
130130
artifact: bun-obj-darwin-x64
131-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
131+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
132132
dependencies: true
133133
compile_obj: false
134134
# - cpu: nehalem
@@ -137,7 +137,7 @@ jobs:
137137
# obj: bun-obj-darwin-x64-baseline
138138
# runner: macos-12
139139
# artifact: bun-obj-darwin-x64-baseline
140-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
140+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
141141
# dependencies: false
142142
# compile_obj: true
143143
- cpu: haswell
@@ -146,15 +146,15 @@ jobs:
146146
obj: bun-obj-darwin-x64
147147
runner: macos-12
148148
artifact: bun-obj-darwin-x64
149-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
149+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
150150
dependencies: false
151151
compile_obj: true
152152
# - cpu: native
153153
# arch: aarch64
154154
# tag: bun-darwin-aarch64
155155
# obj: bun-obj-darwin-aarch64
156156
# artifact: bun-obj-darwin-aarch64
157-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-arm64-lto.tar.gz"
157+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-arm64-lto.tar.gz"
158158
# runner: macos-arm64
159159
# dependencies: true
160160
# compile_obj: true
@@ -263,22 +263,22 @@ jobs:
263263
# package: bun-darwin-x64
264264
# runner: macos-12
265265
# artifact: bun-obj-darwin-x64-baseline
266-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
266+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
267267
- cpu: haswell
268268
arch: x86_64
269269
tag: bun-darwin-x64
270270
obj: bun-obj-darwin-x64
271271
package: bun-darwin-x64
272272
runner: macos-12
273273
artifact: bun-obj-darwin-x64
274-
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-amd64-lto.tar.gz"
274+
webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz"
275275
# - cpu: native
276276
# arch: aarch64
277277
# tag: bun-darwin-aarch64
278278
# obj: bun-obj-darwin-aarch64
279279
# package: bun-darwin-aarch64
280280
# artifact: bun-obj-darwin-aarch64
281-
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-2/bun-webkit-macos-arm64-lto.tar.gz"
281+
# webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-arm64-lto.tar.gz"
282282
# runner: macos-arm64
283283
steps:
284284
- uses: actions/checkout@v3

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ARG ARCH=x86_64
1010
ARG BUILD_MACHINE_ARCH=x86_64
1111
ARG TRIPLET=${ARCH}-linux-gnu
1212
ARG BUILDARCH=amd64
13-
ARG WEBKIT_TAG=2023-oct3-2
13+
ARG WEBKIT_TAG=2023-oct3-3
1414
ARG ZIG_TAG=jul1
1515
ARG ZIG_VERSION="0.12.0-dev.899+027aabf49"
1616
ARG WEBKIT_BASENAME="bun-webkit-linux-$BUILDARCH"

bun.lockb

-362 Bytes
Binary file not shown.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@types/react": "^18.0.25",
2828
"@typescript-eslint/eslint-plugin": "^5.31.0",
2929
"@typescript-eslint/parser": "^5.31.0",
30-
"bun-webkit": "0.0.1-1a49a1f94bf42ab4f8c6b11d7bbbb21e491d2d62"
30+
"bun-webkit": "0.0.1-b3eb3541118a997adc959e138155f5f4736892da"
3131
},
3232
"version": "0.0.0",
3333
"prettier": "./.prettierrc.cjs"

packages/bun-types/bun-test.d.ts

+32
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,38 @@ declare module "bun:test" {
2727

2828
export const mock: {
2929
<T extends AnyFunction>(Function: T): Mock<T>;
30+
31+
/**
32+
* Replace the module `id` with the return value of `factory`.
33+
*
34+
* This is useful for mocking modules.
35+
*
36+
* @param id module ID to mock
37+
* @param factory a function returning an object that will be used as the exports of the mocked module
38+
*
39+
* @example
40+
* ## Example
41+
* ```ts
42+
* import { mock } from "bun:test";
43+
*
44+
* mock.module("fs/promises", () => {
45+
* return {
46+
* readFile: () => Promise.resolve("hello world"),
47+
* };
48+
* });
49+
*
50+
* import { readFile } from "fs/promises";
51+
*
52+
* console.log(await readFile("hello.txt", "utf8")); // hello world
53+
* ```
54+
*
55+
* ## More notes
56+
*
57+
* If the module is already loaded, exports are overwritten with the return
58+
* value of `factory`. If the export didn't exist before, it will not be
59+
* added to existing import statements. This is due to how ESM works.
60+
*/
61+
module(id: string, factory: () => any): void | Promise<void>;
3062
};
3163

3264
/**

0 commit comments

Comments
 (0)