diff options
author | 2023-10-25 22:35:14 -0700 | |
---|---|---|
committer | 2023-10-25 22:35:14 -0700 | |
commit | 2b8aae05d1e95b85c7869f15567a29162106110c (patch) | |
tree | a036c66977a58158a89cee4585ff0055853a5fa0 | |
parent | 2327737b1424797f69d5979ca9b24bcda60fdae6 (diff) | |
download | bun-2b8aae05d1e95b85c7869f15567a29162106110c.tar.gz bun-2b8aae05d1e95b85c7869f15567a29162106110c.tar.zst bun-2b8aae05d1e95b85c7869f15567a29162106110c.zip |
Add test for custom thenables in AsyncLocalStorage and fix flaky test and make expect(fn).toThrow drain unhandled promise rejections (#6701)
* Copy Node.js test for async-local-storage thenables
* Fix flaky test
* Make expect(fn).toThrow() more reliable
* Fix setImmediate order
* Support restoring mocked modules with spyOn
* Fix vm test
* Fix more tests
* alright
* Upgrade WebKit
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | .github/workflows/bun-linux-aarch64.yml | 2 | ||||
-rw-r--r-- | .github/workflows/bun-linux-build.yml | 4 | ||||
-rw-r--r-- | .github/workflows/bun-mac-aarch64.yml | 16 | ||||
-rw-r--r-- | .github/workflows/bun-mac-x64-baseline.yml | 16 | ||||
-rw-r--r-- | .github/workflows/bun-mac-x64.yml | 16 | ||||
-rw-r--r-- | Dockerfile | 2 | ||||
-rwxr-xr-x | bun.lockb | bin | 74034 -> 74034 bytes | |||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/JSMockFunction.cpp | 11 | ||||
-rw-r--r-- | src/bun.js/event_loop.zig | 20 | ||||
-rw-r--r-- | src/bun.js/test/expect.zig | 19 | ||||
-rw-r--r-- | src/bun.js/test/jest.zig | 1 | ||||
-rw-r--r-- | src/cli/test_command.zig | 2 | ||||
-rw-r--r-- | test/bundler/esbuild/splitting.test.ts | 4 | ||||
-rw-r--r-- | test/js/bun/test/expect.test.js | 50 | ||||
-rw-r--r-- | test/js/bun/test/mock/mock-module.test.ts | 13 | ||||
-rw-r--r-- | test/js/node/async_hooks/async-local-storage-thenable.test.ts | 114 | ||||
-rw-r--r-- | test/js/node/vm/vm.test.ts | 3 | ||||
-rw-r--r-- | test/js/web/timers/setTimeout.test.js | 2 |
19 files changed, 226 insertions, 71 deletions
diff --git a/.github/workflows/bun-linux-aarch64.yml b/.github/workflows/bun-linux-aarch64.yml index 547128316..75704b298 100644 --- a/.github/workflows/bun-linux-aarch64.yml +++ b/.github/workflows/bun-linux-aarch64.yml @@ -38,7 +38,7 @@ jobs: arch: aarch64 build_arch: arm64 runner: linux-arm64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-linux-arm64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-linux-arm64-lto.tar.gz" webkit_basename: "bun-webkit-linux-arm64-lto" build_machine_arch: aarch64 diff --git a/.github/workflows/bun-linux-build.yml b/.github/workflows/bun-linux-build.yml index 9c210f2d2..362aa605c 100644 --- a/.github/workflows/bun-linux-build.yml +++ b/.github/workflows/bun-linux-build.yml @@ -49,7 +49,7 @@ jobs: arch: x86_64 build_arch: amd64 runner: big-ubuntu - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-linux-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-linux-amd64-lto.tar.gz" webkit_basename: "bun-webkit-linux-amd64-lto" build_machine_arch: x86_64 - cpu: nehalem @@ -57,7 +57,7 @@ jobs: arch: x86_64 build_arch: amd64 runner: big-ubuntu - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-linux-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-linux-amd64-lto.tar.gz" webkit_basename: "bun-webkit-linux-amd64-lto" build_machine_arch: x86_64 diff --git a/.github/workflows/bun-mac-aarch64.yml b/.github/workflows/bun-mac-aarch64.yml index 81f76dc48..b6eec21d8 100644 --- a/.github/workflows/bun-mac-aarch64.yml +++ b/.github/workflows/bun-mac-aarch64.yml @@ -119,7 +119,7 @@ jobs: # obj: bun-obj-darwin-x64-baseline # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: true # compile_obj: false # - cpu: haswell @@ -128,7 +128,7 @@ jobs: # obj: bun-obj-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: true # compile_obj: false # - cpu: nehalem @@ -137,7 +137,7 @@ jobs: # obj: bun-obj-darwin-x64-baseline # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: false # compile_obj: true # - cpu: haswell @@ -146,7 +146,7 @@ jobs: # obj: bun-obj-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: false # compile_obj: true - cpu: native @@ -154,7 +154,7 @@ jobs: tag: bun-darwin-aarch64 obj: bun-obj-darwin-aarch64 artifact: bun-obj-darwin-aarch64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-arm64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-arm64-lto.tar.gz" runner: macos-arm64 dependencies: true compile_obj: true @@ -260,7 +260,7 @@ jobs: # package: bun-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # - cpu: haswell # arch: x86_64 # tag: bun-darwin-x64 @@ -268,14 +268,14 @@ jobs: # package: bun-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" - cpu: native arch: aarch64 tag: bun-darwin-aarch64 obj: bun-obj-darwin-aarch64 package: bun-darwin-aarch64 artifact: bun-obj-darwin-aarch64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-arm64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-arm64-lto.tar.gz" runner: macos-arm64 steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/bun-mac-x64-baseline.yml b/.github/workflows/bun-mac-x64-baseline.yml index abb8e6dd0..f7e6ca6d3 100644 --- a/.github/workflows/bun-mac-x64-baseline.yml +++ b/.github/workflows/bun-mac-x64-baseline.yml @@ -119,7 +119,7 @@ jobs: obj: bun-obj-darwin-x64-baseline runner: macos-12 artifact: bun-obj-darwin-x64-baseline - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" dependencies: true compile_obj: false # - cpu: haswell @@ -128,7 +128,7 @@ jobs: # obj: bun-obj-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: true # compile_obj: false - cpu: nehalem @@ -137,7 +137,7 @@ jobs: obj: bun-obj-darwin-x64-baseline runner: macos-12 artifact: bun-obj-darwin-x64-baseline - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" dependencies: false compile_obj: true # - cpu: haswell @@ -146,7 +146,7 @@ jobs: # obj: bun-obj-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: false # compile_obj: true # - cpu: native @@ -154,7 +154,7 @@ jobs: # tag: bun-darwin-aarch64 # obj: bun-obj-darwin-aarch64 # artifact: bun-obj-darwin-aarch64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # runner: macos-arm64 # dependencies: true # compile_obj: true @@ -261,7 +261,7 @@ jobs: package: bun-darwin-x64 runner: macos-12 artifact: bun-obj-darwin-x64-baseline - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # - cpu: haswell # arch: x86_64 # tag: bun-darwin-x64 @@ -269,14 +269,14 @@ jobs: # package: bun-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # - cpu: native # arch: aarch64 # tag: bun-darwin-aarch64 # obj: bun-obj-darwin-aarch64 # package: bun-darwin-aarch64 # artifact: bun-obj-darwin-aarch64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # runner: macos-arm64 steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/bun-mac-x64.yml b/.github/workflows/bun-mac-x64.yml index ae83e5a9c..172ab538e 100644 --- a/.github/workflows/bun-mac-x64.yml +++ b/.github/workflows/bun-mac-x64.yml @@ -119,7 +119,7 @@ jobs: # obj: bun-obj-darwin-x64-baseline # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: true # compile_obj: false - cpu: haswell @@ -128,7 +128,7 @@ jobs: obj: bun-obj-darwin-x64 runner: macos-12 artifact: bun-obj-darwin-x64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" dependencies: true compile_obj: false # - cpu: nehalem @@ -137,7 +137,7 @@ jobs: # obj: bun-obj-darwin-x64-baseline # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # dependencies: false # compile_obj: true - cpu: haswell @@ -146,7 +146,7 @@ jobs: obj: bun-obj-darwin-x64 runner: macos-12 artifact: bun-obj-darwin-x64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" dependencies: false compile_obj: true # - cpu: native @@ -154,7 +154,7 @@ jobs: # tag: bun-darwin-aarch64 # obj: bun-obj-darwin-aarch64 # artifact: bun-obj-darwin-aarch64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-arm64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-arm64-lto.tar.gz" # runner: macos-arm64 # dependencies: true # compile_obj: true @@ -263,7 +263,7 @@ jobs: # package: bun-darwin-x64 # runner: macos-12 # artifact: bun-obj-darwin-x64-baseline - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" - cpu: haswell arch: x86_64 tag: bun-darwin-x64 @@ -271,14 +271,14 @@ jobs: package: bun-darwin-x64 runner: macos-12 artifact: bun-obj-darwin-x64 - webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-amd64-lto.tar.gz" + webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-amd64-lto.tar.gz" # - cpu: native # arch: aarch64 # tag: bun-darwin-aarch64 # obj: bun-obj-darwin-aarch64 # package: bun-darwin-aarch64 # artifact: bun-obj-darwin-aarch64 - # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-3/bun-webkit-macos-arm64-lto.tar.gz" + # webkit_url: "https://github.com/oven-sh/WebKit/releases/download/2023-oct3-4/bun-webkit-macos-arm64-lto.tar.gz" # runner: macos-arm64 steps: - uses: actions/checkout@v3 diff --git a/Dockerfile b/Dockerfile index 660080209..4d7e83ffd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ ARG ARCH=x86_64 ARG BUILD_MACHINE_ARCH=x86_64 ARG TRIPLET=${ARCH}-linux-gnu ARG BUILDARCH=amd64 -ARG WEBKIT_TAG=2023-oct3-3 +ARG WEBKIT_TAG=2023-oct3-4 ARG ZIG_TAG=jul1 ARG ZIG_VERSION="0.12.0-dev.899+027aabf49" ARG WEBKIT_BASENAME="bun-webkit-linux-$BUILDARCH" Binary files differdiff --git a/package.json b/package.json index 34db00b9c..6c11ff55f 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "@types/react": "^18.0.25", "@typescript-eslint/eslint-plugin": "^5.31.0", "@typescript-eslint/parser": "^5.31.0", - "bun-webkit": "0.0.1-b3eb3541118a997adc959e138155f5f4736892da" + "bun-webkit": "0.0.1-67c9ba93323b4a5a95bc4d1efe61166dc6e3e691" }, "version": "0.0.0", "prettier": "./.prettierrc.cjs" diff --git a/src/bun.js/bindings/JSMockFunction.cpp b/src/bun.js/bindings/JSMockFunction.cpp index 3115229cb..fac62830b 100644 --- a/src/bun.js/bindings/JSMockFunction.cpp +++ b/src/bun.js/bindings/JSMockFunction.cpp @@ -262,6 +262,8 @@ public: JSC::Identifier spyIdentifier; unsigned spyAttributes = 0; + static constexpr unsigned SpyAttributeESModuleNamespace = 1 << 30; + void setName(const WTF::String& name) { auto& vm = this->vm(); @@ -338,7 +340,12 @@ public: } // Reset the spy back to the original value. - target->putDirect(this->vm(), this->spyIdentifier, implValue, this->spyAttributes); + if (this->spyAttributes & SpyAttributeESModuleNamespace) { + auto* moduleNamespaceObject = jsCast<JSModuleNamespaceObject*>(target); + moduleNamespaceObject->overrideExportValue(moduleNamespaceObject->globalObject(), this->spyIdentifier, implValue); + } else { + target->putDirect(this->vm(), this->spyIdentifier, implValue, this->spyAttributes); + } } this->spyTarget.clear(); @@ -634,6 +641,7 @@ extern "C" EncodedJSValue JSMock__jsSpyOn(JSC::JSGlobalObject* lexicalGlobalObje if (JSModuleNamespaceObject* moduleNamespaceObject = jsDynamicCast<JSModuleNamespaceObject*>(object)) { moduleNamespaceObject->overrideExportValue(globalObject, propertyKey, mock); + mock->spyAttributes |= JSMockFunction::SpyAttributeESModuleNamespace; } else { object->putDirect(vm, propertyKey, mock, attributes); } @@ -649,6 +657,7 @@ extern "C" EncodedJSValue JSMock__jsSpyOn(JSC::JSGlobalObject* lexicalGlobalObje if (JSModuleNamespaceObject* moduleNamespaceObject = jsDynamicCast<JSModuleNamespaceObject*>(object)) { moduleNamespaceObject->overrideExportValue(globalObject, propertyKey, mock); + mock->spyAttributes |= JSMockFunction::SpyAttributeESModuleNamespace; } else { object->putDirect(vm, propertyKey, JSC::GetterSetter::create(vm, globalObject, mock, mock), attributes); } diff --git a/src/bun.js/event_loop.zig b/src/bun.js/event_loop.zig index 7c444fbe7..9120e3de6 100644 --- a/src/bun.js/event_loop.zig +++ b/src/bun.js/event_loop.zig @@ -997,6 +997,9 @@ pub const EventLoop = struct { var ctx = this.virtual_machine; var loop = ctx.event_loop_handle.?; + this.flushImmediateQueue(); + this.tickImmediateTasks(); + // Some tasks need to keep the event loop alive for one more tick. // We want to keep the event loop alive long enough to process those ticks and any microtasks // @@ -1015,8 +1018,6 @@ pub const EventLoop = struct { ctx.onAfterEventLoop(); // this.afterUSocketsTick(); - } else { - this.flushImmediateQueue(); } } @@ -1024,6 +1025,9 @@ pub const EventLoop = struct { var ctx = this.virtual_machine; var loop = ctx.event_loop_handle.?; + this.flushImmediateQueue(); + this.tickImmediateTasks(); + // Some tasks need to keep the event loop alive for one more tick. // We want to keep the event loop alive long enough to process those ticks and any microtasks // @@ -1039,11 +1043,8 @@ pub const EventLoop = struct { if (loop.num_polls > 0 or loop.active > 0) { this.processGCTimer(); loop.tickWithTimeout(timeoutMs, ctx.jsc); - this.flushImmediateQueue(); ctx.onAfterEventLoop(); // this.afterUSocketsTick(); - } else { - this.flushImmediateQueue(); } } @@ -1065,6 +1066,9 @@ pub const EventLoop = struct { var ctx = this.virtual_machine; var loop = ctx.event_loop_handle.?; + this.flushImmediateQueue(); + this.tickImmediateTasks(); + const pending_unref = ctx.pending_unref_counter; if (pending_unref > 0) { ctx.pending_unref_counter = 0; @@ -1094,6 +1098,9 @@ pub const EventLoop = struct { pub fn autoTickActive(this: *EventLoop) void { var loop = this.virtual_machine.event_loop_handle.?; + this.flushImmediateQueue(); + this.tickImmediateTasks(); + var ctx = this.virtual_machine; const pending_unref = ctx.pending_unref_counter; @@ -1108,8 +1115,6 @@ pub const EventLoop = struct { this.flushImmediateQueue(); ctx.onAfterEventLoop(); // this.afterUSocketsTick(); - } else { - this.flushImmediateQueue(); } } @@ -1122,7 +1127,6 @@ pub const EventLoop = struct { var ctx = this.virtual_machine; this.tickConcurrent(); - this.tickImmediateTasks(); this.processGCTimer(); var global = ctx.global; diff --git a/src/bun.js/test/expect.zig b/src/bun.js/test/expect.zig index f83a9b194..16cbf28db 100644 --- a/src/bun.js/test/expect.zig +++ b/src/bun.js/test/expect.zig @@ -1655,6 +1655,10 @@ pub const Expect = struct { const result_: ?JSValue = brk: { var vm = globalObject.bunVM(); var return_value: JSValue = .zero; + + // Drain existing unhandled rejections + vm.global.handleRejectedPromises(); + var scope = vm.unhandledRejectionScope(); var prev_unhandled_pending_rejection_to_capture = vm.unhandled_pending_rejection_to_capture; vm.unhandled_pending_rejection_to_capture = &return_value; @@ -1662,12 +1666,14 @@ pub const Expect = struct { const return_value_from_fucntion: JSValue = value.call(globalObject, &.{}); vm.unhandled_pending_rejection_to_capture = prev_unhandled_pending_rejection_to_capture; + vm.global.handleRejectedPromises(); + if (return_value == .zero) { return_value = return_value_from_fucntion; } if (return_value.asAnyPromise()) |promise| { - globalObject.bunVM().waitForPromise(promise); + vm.waitForPromise(promise); scope.apply(vm); const promise_result = promise.result(globalObject.vm()); @@ -1676,15 +1682,24 @@ pub const Expect = struct { break :brk null; }, .Rejected => { + promise.setHandled(globalObject.vm()); + // since we know for sure it rejected, we should always return the error break :brk promise_result.toError() orelse promise_result; }, .Pending => unreachable, } } + + if (return_value != return_value_from_fucntion) { + if (return_value_from_fucntion.asAnyPromise()) |existing| { + existing.setHandled(globalObject.vm()); + } + } + scope.apply(vm); - break :brk return_value.toError(); + break :brk return_value.toError() orelse return_value_from_fucntion.toError(); }; const did_throw = result_ != null; diff --git a/src/bun.js/test/jest.zig b/src/bun.js/test/jest.zig index 28f39a6da..d7ddea6be 100644 --- a/src/bun.js/test/jest.zig +++ b/src/bun.js/test/jest.zig @@ -462,6 +462,7 @@ pub const Jest = struct { const mockModuleFn = JSC.NewFunction(globalObject, ZigString.static("module"), 2, JSMock__jsModuleMock, false); module.put(globalObject, ZigString.static("mock"), mockFn); mockFn.put(globalObject, ZigString.static("module"), mockModuleFn); + mockFn.put(globalObject, ZigString.static("restore"), restoreAllMocks); const jest = JSValue.createEmptyObject(globalObject, 7); jest.put(globalObject, ZigString.static("fn"), mockFn); diff --git a/src/cli/test_command.zig b/src/cli/test_command.zig index 920da5723..4358ea10b 100644 --- a/src/cli/test_command.zig +++ b/src/cli/test_command.zig @@ -987,6 +987,8 @@ pub const TestCommand = struct { vm.eventLoop().autoTick(); if (!jest.Jest.runner.?.has_pending_tests) break; vm.eventLoop().tick(); + } else { + vm.eventLoop().tickImmediateTasks(); } while (prev_unhandled_count < vm.unhandled_error_counter) { diff --git a/test/bundler/esbuild/splitting.test.ts b/test/bundler/esbuild/splitting.test.ts index b9105e727..fbf5634fe 100644 --- a/test/bundler/esbuild/splitting.test.ts +++ b/test/bundler/esbuild/splitting.test.ts @@ -368,8 +368,8 @@ describe("bundler", () => { console.log('observer', getValue()); } setObserver(observer); - import('./out/a.js'); - import('./out/b.js'); + await import('./out/a.js'); + await import('./out/b.js'); `, }, run: [ diff --git a/test/js/bun/test/expect.test.js b/test/js/bun/test/expect.test.js index 1b05c9802..53068d224 100644 --- a/test/js/bun/test/expect.test.js +++ b/test/js/bun/test/expect.test.js @@ -565,34 +565,34 @@ describe("expect()", () => { }).not.toThrow(err); const weirdThings = [ - /watttt/g, - BigInt(123), - -42, - NaN, - Infinity, - -Infinity, - undefined, - null, - true, - false, - 0, - 1, - "", - "hello", - {}, - [], - new Date(), - new Error(), - new RegExp("foo"), - new Map(), - new Set(), - Promise.resolve(), - Promise.reject(Symbol("123")).finally(() => {}), - Symbol("123"), + () => /watttt/g, + () => BigInt(123), + () => -42, + () => NaN, + () => Infinity, + () => -Infinity, + () => undefined, + () => null, + () => true, + () => false, + () => 0, + () => 1, + () => "", + () => "hello", + () => {}, + () => [], + () => new Date(), + () => new Error(), + () => new RegExp("foo"), + () => new Map(), + () => new Set(), + () => Promise.resolve(), + () => Promise.reject(Symbol("123")), + () => Symbol("123"), ]; for (const weirdThing of weirdThings) { expect(() => { - throw weirdThing; + throw weirdThing(); }).toThrow(); } diff --git a/test/js/bun/test/mock/mock-module.test.ts b/test/js/bun/test/mock/mock-module.test.ts index c4c618835..624af848a 100644 --- a/test/js/bun/test/mock/mock-module.test.ts +++ b/test/js/bun/test/mock/mock-module.test.ts @@ -7,10 +7,21 @@ // - Write test for export {foo} from "./foo" // - Write test for import {foo} from "./foo"; export {foo} -import { mock, test, expect, spyOn, Mock } from "bun:test"; +import { expect, mock, spyOn, test } from "bun:test"; import { fn, iCallFn, variable } from "./mock-module-fixture"; import * as spyFixture from "./spymodule-fixture"; +test("mock.restore", () => { + const original = spyFixture.iSpy; + spyOn(spyFixture, "iSpy"); + const mocked = spyFixture.iSpy; + expect(spyFixture.iSpy).not.toBe(original); + expect(spyFixture.iSpy).not.toHaveBeenCalled(); + spyFixture.iSpy(); + mock.restore(); + expect(spyFixture.iSpy).toBe(original); +}); + test("spyOn", () => { spyOn(spyFixture, "iSpy"); expect(spyFixture.iSpy).not.toHaveBeenCalled(); diff --git a/test/js/node/async_hooks/async-local-storage-thenable.test.ts b/test/js/node/async_hooks/async-local-storage-thenable.test.ts new file mode 100644 index 000000000..b40403d59 --- /dev/null +++ b/test/js/node/async_hooks/async-local-storage-thenable.test.ts @@ -0,0 +1,114 @@ +import { AsyncLocalStorage } from "async_hooks"; +import { hideFromStackTrace } from "harness"; +import { createTest } from "node-harness"; +const store = new AsyncLocalStorage(); +const data = Symbol("verifier"); + +const { beforeAll, describe, expect, it, throws, assert, createCallCheckCtx, createDoneDotAll } = createTest( + import.meta.path, +); + +test("node.js test test-async-local-storage-no-mix-contexts.js", async () => { + const asyncLocalStorage = new AsyncLocalStorage(); + const err = new Error(); + const next = () => + Promise.resolve().then(() => { + assert.strictEqual(asyncLocalStorage.getStore().get("a"), 1); + throw err; + }); + await new Promise((resolve, reject) => { + asyncLocalStorage.run(new Map(), () => { + const store = asyncLocalStorage.getStore(); + store.set("a", 1); + next().then(resolve, reject); + }); + }).catch(e => { + assert.strictEqual(asyncLocalStorage.getStore(), undefined); + assert.strictEqual(e, err); + }); + assert.strictEqual(asyncLocalStorage.getStore(), undefined); +}); + +test("await custom thenable", async () => { + const { resolve, promise } = Promise.withResolvers(); + function thenable() { + return { + then(handle: CallableFunction) { + assert.strictEqual(store.getStore(), data); + handle(); + }, + }; + } + // Await a thenable + await store.run(data, async () => { + assert.strictEqual(store.getStore(), data); + await (thenable() as any); + assert.strictEqual(store.getStore(), data); + resolve(undefined); + }); + + await promise; +}); + +test("Returning a thenable in an async function", async done => { + const { mustCall } = createCallCheckCtx(done); + const then: Function = mustCall(cb => { + assert.strictEqual(store.getStore(), data); + process.nextTick(cb); + }, 1); + + function thenable() { + return { + then, + }; + } + + await store.run(data, async () => { + try { + assert.strictEqual(store.getStore(), data); + return thenable(); + } finally { + assert.strictEqual(store.getStore(), data); + } + }); +}); + +test("Resolving a thenable", async done => { + const { mustCall } = createCallCheckCtx(done); + const then: Function = mustCall(cb => { + assert.strictEqual(store.getStore(), data); + process.nextTick(cb); + }, 1); + + function thenable() { + return { + then, + }; + } + + await store.run(data, () => { + assert.strictEqual(store.getStore(), data); + Promise.resolve(thenable()); + assert.strictEqual(store.getStore(), data); + }); +}); + +test("Returning a thenable in a then handler", async done => { + const { mustCall } = createCallCheckCtx(done); + const then: Function = mustCall(cb => { + assert.strictEqual(store.getStore(), data); + process.nextTick(cb); + }, 1); + + function thenable() { + return { + then, + }; + } + + await store.run(data, () => { + assert.strictEqual(store.getStore(), data); + Promise.resolve().then(() => thenable()); + assert.strictEqual(store.getStore(), data); + }); +}); diff --git a/test/js/node/vm/vm.test.ts b/test/js/node/vm/vm.test.ts index 4e291ac9f..99ec07ce2 100644 --- a/test/js/node/vm/vm.test.ts +++ b/test/js/node/vm/vm.test.ts @@ -121,8 +121,7 @@ function testRunInContext( }); test("can reject a promise", () => { const context = createContext({}); - const result = fn("Promise.reject(new TypeError('Oops!'));", context); - expect(async () => await result).toThrow({ + expect(async () => await fn("Promise.reject(new TypeError('Oops!'));", context)).toThrow({ name: "TypeError", message: "Oops!", }); diff --git a/test/js/web/timers/setTimeout.test.js b/test/js/web/timers/setTimeout.test.js index 9f898c8b5..e3483601e 100644 --- a/test/js/web/timers/setTimeout.test.js +++ b/test/js/web/timers/setTimeout.test.js @@ -67,7 +67,7 @@ it("clearTimeout", async () => { expect(called).toBe(false); }); -it("setImmediate runs after setTimeout cb", async () => { +it.todo("setImmediate runs after setTimeout cb", async () => { var ranFirst = -1; setTimeout(() => { if (ranFirst === -1) ranFirst = 1; |