Skip to content

Commit f31fc5b

Browse files
committed
🔧 test: ensure response normalization doesn't mutate
1 parent d759a6f commit f31fc5b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test/core/normalize.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,39 @@ describe('Normalize', () => {
432432
expect(response.status).toBe(422)
433433
})
434434

435+
it('response normalization does not mutate', async () => {
436+
// Long-lived object has a `token` property
437+
const service = {
438+
name: 'nagisa',
439+
status: 'online',
440+
token: 'secret',
441+
}
442+
443+
// ...but this property is hidden by the response schema
444+
const responseSchema = t.Object({
445+
name: t.String(),
446+
status: t.String(),
447+
})
448+
449+
const app = new Elysia({
450+
normalize: true,
451+
}).get('/test', () => service, {
452+
response: responseSchema,
453+
})
454+
455+
expect(service).toHaveProperty('token')
456+
const origService = structuredClone(service)
457+
458+
const response = await app.handle(new Request('http://localhost/test'))
459+
expect(response.body).not.toHaveProperty('token')
460+
461+
// Expect the `token` property to remain present after `service` object was used in a response
462+
expect(service).toHaveProperty('token')
463+
464+
// In fact, expect the `service` to not be mutated at all
465+
expect(service).toEqual(origService)
466+
})
467+
435468
// it('normalize response with getter fields on class', async () => {
436469
// const app = new Elysia({
437470
// normalize: true

0 commit comments

Comments
 (0)