Skip to content

Commit

Permalink
test: preserving ALS
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieowoce committed Oct 7, 2024
1 parent fbdd249 commit b014c14
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/next/src/server/after/after-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,52 @@ describe('AfterContext', () => {
expect(store1 === workStore).toBe(true)
expect(store2 === store1).toBe(true)
})

it('preserves the ALS context the callback was created in', async () => {
type TestStore = string
const testStorage = new AsyncLocalStorage<TestStore>()

const waitUntil = jest.fn()

let onCloseCallback: (() => void) | undefined = undefined
const onClose = jest.fn((cb) => {
onCloseCallback = cb
})

const afterContext = new AfterContext({
waitUntil,
onClose,
})

const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, workStore)

// ==================================

const stores = new DetachedPromise<
[TestStore | undefined, TestStore | undefined]
>()

await testStorage.run('value', () =>
run(async () => {
const store1 = testStorage.getStore()
after(() => {
const store2 = testStorage.getStore()
stores.resolve([store1, store2])
})
})
)

// the response is done.
onCloseCallback!()

const [store1, store2] = await stores.promise
// if we use .toBe, the proxy from createMockWorkStore throws because jest checks '$$typeof'
expect(store1).toBeDefined()
expect(store2).toBeDefined()
expect(store1 === 'value').toBe(true)
expect(store2 === store1).toBe(true)
})
})

const createMockWorkStore = (afterContext: AfterContext): WorkStore => {
Expand Down

0 comments on commit b014c14

Please sign in to comment.