Testing Middleware Composition Patterns in Koa.js
This test suite validates the compose functionality in Koa.js middleware system, focusing on both default and configurable composition patterns. It ensures proper middleware execution order and async handling in the request-response cycle.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
koajs/koa
__tests__/application/compose.test.js
'use strict'
const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')
describe('app.compose', () => {
it('should work with default compose ', async () => {
const app = new Koa()
const calls = []
app.use((ctx, next) => {
calls.push(1)
return next().then(() => {
calls.push(4)
})
})
app.use((ctx, next) => {
calls.push(2)
return next().then(() => {
calls.push(3)
})
})
await request(app.callback())
.get('/')
.expect(404)
assert.deepStrictEqual(calls, [1, 2, 3, 4])
})
it('should work with configurable compose', async () => {
const calls = []
let count = 0
const app = new Koa({
compose (fns) {
return async (ctx) => {
const dispatch = async () => {
count++
const fn = fns.shift()
fn && fn(ctx, dispatch)
}
dispatch()
}
}
})
app.use((ctx, next) => {
calls.push(1)
next()
calls.push(4)
})
app.use((ctx, next) => {
calls.push(2)
next()
calls.push(3)
})
await request(app.callback())
.get('/')
assert.deepStrictEqual(calls, [1, 2, 3, 4])
assert.equal(count, 3)
})
})