Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined is not an object then using node:async_hooks:72:37 #17860

Open
polRk opened this issue Mar 2, 2025 · 2 comments
Open

Undefined is not an object then using node:async_hooks:72:37 #17860

polRk opened this issue Mar 2, 2025 · 2 comments
Labels
bug Something isn't working node:async_hooks node.js Compatibility with Node.js APIs

Comments

@polRk
Copy link

polRk commented Mar 2, 2025

What version of Bun is running?

v1.2.4

What platform is your computer?

Darwin 24.3.0 arm64 arm

What steps can reproduce the bug?

const ctx = new AsyncLocalStorage<Store>();
import { afterEach, beforeEach, expect, spyOn, test } from "bun:test";

import { overview } from './index.js';
import { ctx } from "../ctx/index.js";
import { mockStore } from "../mocks/store.js";

let mockedFetch = spyOn(globalThis, 'fetch')
mockedFetch.mockImplementation(async () => new Response("{}"))

beforeEach(() => {
	ctx.enterWith(mockStore)
})

// Reset mocks between tests
afterEach(() => {
	mockedFetch.mockReset();
});

test('overview no retries', async () => ctx.run({ ...mockStore, retryAttempts: -1 }, () => {
	mockedFetch.mockImplementationOnce(async () => new Response("{}", { status: 500 }))
	expect(overview()).rejects.toThrow("Failed to fetch overview. Maximum retries exceeded.");
	expect(mockedFetch).toBeCalledTimes(0);
}))

test('overview positive retries', async () => ctx.run({ ...mockStore, retryAttempts: 3 }, () => {
	mockedFetch.mockImplementationOnce(async () => new Response("{}", { status: 500 }))
	mockedFetch.mockImplementationOnce(async () => new Response("{}", { status: 500 }))
	mockedFetch.mockImplementation(async () => new Response("{}", { status: 200 }))

	expect(overview()).resolves.toBeTruthy();
	expect(mockedFetch).toBeCalledTimes(3);
}))

And run

bun test

I got an error after test finished

36 |    mockedFetch.mockImplementationOnce(async () => new Response("{}", { status: 500 }))
37 |    expect(overview()).rejects.toThrow("Failed to fetch overview. Maximum retries exceeded.");
38 |    expect(mockedFetch).toBeCalledTimes(0);
39 | }))
40 | 
41 | test('overview positive retries', async () => ctx.run({ ...mockStore, retryAttempts: 3 }, () => {
                                       ^
TypeError: undefined is not an object (evaluating 'get')
      at run (node:async_hooks:72:37)
      at <anonymous> (index.spec.ts:41:35)
✗ overview positive retries [0.38ms]

What is the expected behavior?

Tests finished without error

What do you see instead?

41 | test('overview positive retries', async () => ctx.run({ ...mockStore, retryAttempts: 3 }, () => {
                                       ^
TypeError: undefined is not an object (evaluating 'get')
      at run (node:async_hooks:72:37)
      at <anonymous> (index.spec.ts:41:35)
✗ overview positive retries [0.38ms]

Additional information

No response

@polRk polRk added bug Something isn't working needs triage labels Mar 2, 2025
@polRk
Copy link
Author

polRk commented Mar 2, 2025

If i make something like this, i got an extended error

expect(overview).not.toThrow();
67 |       throw e;
68 |     } finally {
69 |       if (!wasDisabled) {
70 |         var context2 = get();
71 |         if (context2 === context && contextWasAlreadyInit)
72 |         else if (context2 = context2.slice(), hasPrevious)
                                         ^
TypeError: undefined is not an object (evaluating 'get')
      at run (node:async_hooks:72:37)
✗ overview positive retries [0.25ms]

@polRk
Copy link
Author

polRk commented Mar 2, 2025

But if i use enterWith, instead of run, all works fine.

test('overview positive retries', () => {
	ctx.enterWith({ ...mockStore, retryAttempts: 3 })

	mockedFetch.mockImplementationOnce(async () => new Response("{}", { status: 500 }))
	mockedFetch.mockImplementationOnce(async () => new Response("{}", { status: 500 }))
	mockedFetch.mockImplementation(async () => new Response("{}", { status: 200 }))

	expect(overview).not.toThrow();
	expect(mockedFetch).toBeCalledTimes(3);
})

@RiskyMH RiskyMH added node.js Compatibility with Node.js APIs node:async_hooks and removed needs triage labels Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node:async_hooks node.js Compatibility with Node.js APIs
Projects
None yet
Development

No branches or pull requests

2 participants