Back to Repositories

Testing AsyncLocalStorage Context Management in Koa.js

This test suite evaluates the currentContext functionality in Koa’s application handling, focusing on AsyncLocalStorage integration. It verifies context management across asynchronous operations and error handling scenarios, ensuring proper context isolation between requests.

Test Coverage Overview

The test suite provides comprehensive coverage of Koa’s currentContext implementation.

Key areas tested include:
  • AsyncLocalStorage support validation
  • Context persistence across async operations
  • Multiple concurrent request handling
  • Error handling scenarios
  • Context behavior with AsyncLocalStorage enabled vs disabled

Implementation Analysis

The testing approach employs Jest’s describe/it blocks with async/await patterns for handling asynchronous operations. The implementation utilizes supertest for HTTP request simulation and node’s built-in assert module for validations.

Notable patterns include:
  • Promise-based async testing
  • Timeout and setImmediate assertions
  • Concurrent request testing
  • Error event handling verification

Technical Details

Testing tools and configuration:
  • Node.js test runner with describe/it blocks
  • supertest for HTTP request simulation
  • assert module for assertions
  • async_hooks API integration
  • Promise-based error handling
  • Multiple middleware configurations

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Node.js application testing.

Notable practices include:
  • Proper async/await usage
  • Comprehensive error scenario coverage
  • Isolated test cases
  • Feature detection for platform compatibility
  • Concurrent request handling validation
  • Clean test organization and structure

koajs/koa

__tests__/application/currentContext.test.js

            
'use strict'

const { describe, it } = require('node:test')
const request = require('supertest')
const assert = require('assert')
const Koa = require('../..')

describe('app.currentContext', () => {
  it('should throw error if AsyncLocalStorage not support', () => {
    if (require('async_hooks').AsyncLocalStorage) return
    assert.throws(() => new Koa({ asyncLocalStorage: true }),
      /Requires node 12\.17\.0 or higher to enable asyncLocalStorage/)
  })

  it('should get currentContext return context when asyncLocalStorage enable', async () => {
    if (!require('async_hooks').AsyncLocalStorage) return

    const app = new Koa({ asyncLocalStorage: true })

    app.use(async ctx => {
      assert(ctx === app.currentContext)
      await new Promise(resolve => {
        setTimeout(() => {
          assert(ctx === app.currentContext)
          resolve()
        }, 1)
      })
      await new Promise(resolve => {
        assert(ctx === app.currentContext)
        setImmediate(() => {
          assert(ctx === app.currentContext)
          resolve()
        })
      })
      assert(ctx === app.currentContext)
      app.currentContext.body = 'ok'
    })

    const requestServer = async () => {
      assert(app.currentContext === undefined)
      await request(app.callback()).get('/').expect('ok')
      assert(app.currentContext === undefined)
    }

    await Promise.all([
      requestServer(),
      requestServer(),
      requestServer(),
      requestServer(),
      requestServer()
    ])
  })

  it('should get currentContext return undefined when asyncLocalStorage disable', async () => {
    const app = new Koa()

    app.use(async ctx => {
      assert(app.currentContext === undefined)
      ctx.body = 'ok'
    })

    await request(app.callback()).get('/').expect('ok')
  })

  it('should get currentContext return context in error handler when asyncLocalStorage enable', async () => {
    const app = new Koa({ asyncLocalStorage: true })

    app.use(async () => {
      throw new Error('error message')
    })

    const handleError = new Promise((resolve, reject) => {
      app.on('error', (err, ctx) => {
        try {
          assert.strictEqual(err.message, 'error message')
          assert.strictEqual(app.currentContext, ctx)
          resolve()
        } catch (e) {
          reject(e)
        }
      })
    })

    await request(app.callback()).get('/').expect('Internal Server Error')
    await handleError
  })

  it('should get currentContext return undefined in error handler when asyncLocalStorage disable', async () => {
    const app = new Koa()

    app.use(async () => {
      throw new Error('error message')
    })

    const handleError = new Promise((resolve, reject) => {
      app.on('error', (err, ctx) => {
        try {
          assert.strictEqual(err.message, 'error message')
          assert.strictEqual(app.currentContext, undefined)
          resolve()
        } catch (e) {
          reject(e)
        }
      })
    })

    await request(app.callback()).get('/').expect('Internal Server Error')
    await handleError
  })
})