Skip to content

Commit

Permalink
🔧 test: ensure response normalization doesn't mutate
Browse files Browse the repository at this point in the history
  • Loading branch information
crishoj committed Oct 31, 2024
1 parent d759a6f commit f31fc5b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/core/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,39 @@ describe('Normalize', () => {
expect(response.status).toBe(422)
})

it('response normalization does not mutate', async () => {
// Long-lived object has a `token` property
const service = {
name: 'nagisa',
status: 'online',
token: 'secret',
}

// ...but this property is hidden by the response schema
const responseSchema = t.Object({
name: t.String(),
status: t.String(),
})

const app = new Elysia({
normalize: true,
}).get('/test', () => service, {
response: responseSchema,
})

expect(service).toHaveProperty('token')
const origService = structuredClone(service)

const response = await app.handle(new Request('http://localhost/test'))
expect(response.body).not.toHaveProperty('token')

// Expect the `token` property to remain present after `service` object was used in a response
expect(service).toHaveProperty('token')

// In fact, expect the `service` to not be mutated at all
expect(service).toEqual(origService)
})

// it('normalize response with getter fields on class', async () => {
// const app = new Elysia({
// normalize: true
Expand Down

0 comments on commit f31fc5b

Please # to comment.