Testing Request Object Property Inheritance in Koa.js
This test suite evaluates the request handling functionality in Koa’s application layer, focusing on property inheritance and prototype isolation. It verifies how custom properties are merged into request objects and ensures proper encapsulation between different Koa application instances.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
koajs/koa
__tests__/application/request.test.js
'use strict'
const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
describe('app.request', () => {
const app1 = new Koa()
app1.request.message = 'hello'
const app2 = new Koa()
it('should merge properties', () => {
app1.use((ctx, next) => {
assert.strictEqual(ctx.request.message, 'hello')
ctx.status = 204
})
return request(app1.callback())
.get('/')
.expect(204)
})
it('should not affect the original prototype', () => {
app2.use((ctx, next) => {
assert.strictEqual(ctx.request.message, undefined)
ctx.status = 204
})
return request(app2.callback())
.get('/')
.expect(204)
})
})