Back to Repositories

Testing Error Handling Middleware Implementation in Koa

This test suite examines the error handling functionality in Koa’s application layer, specifically focusing on the onerror middleware. It verifies various error scenarios and logging behaviors to ensure robust error management in Koa applications.

Test Coverage Overview

The test suite provides comprehensive coverage of Koa’s error handling mechanisms:

  • Validation of error object types
  • Handling of external scope errors
  • 404 status code scenarios
  • Silent mode behavior
  • Error logging to stderr

Implementation Analysis

The testing approach utilizes Node’s native test framework with mock capabilities to isolate and verify error handling behavior. The tests employ assertion patterns to validate both positive and negative scenarios, with particular attention to error object validation and console output verification.

Technical Details

Testing tools and configuration:

  • Node.js native test module with describe/it blocks
  • Assert module for assertions
  • Mock functionality for console.error testing
  • VM module for external error scope testing

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Proper test isolation using mocks
  • Cleanup of mocked functions
  • Comprehensive error scenario coverage
  • Clear test case organization
  • Effective use of setup and teardown patterns

koajs/koa

__tests__/application/onerror.test.js

            
'use strict'

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

describe('app.onerror(err)', () => {
  it('should throw an error if a non-error is given', () => {
    const app = new Koa()

    assert.throws(() => {
      app.onerror('foo')
    }, TypeError, 'non-error thrown: foo')
  })

  it('should accept errors coming from other scopes', () => {
    const ExternError = require('vm').runInNewContext('Error')

    const app = new Koa()
    const error = Object.assign(new ExternError('boom'), {
      status: 418,
      expose: true
    })

    assert.doesNotThrow(() => app.onerror(error))
  })

  it('should do nothing if status is 404', () => {
    const app = new Koa()
    const err = new Error()

    err.status = 404

    const spy = mock.method(console, 'error', () => {})
    app.onerror(err)
    assert.strictEqual(spy.mock.calls.length, 0)
    spy.mock.restore()
  })

  it('should do nothing if .silent', () => {
    const app = new Koa()
    app.silent = true
    const err = new Error()

    const spy = mock.method(console, 'error', () => {})
    app.onerror(err)
    assert.strictEqual(spy.mock.calls.length, 0)
    spy.mock.restore()
  })

  it('should log the error to stderr', () => {
    const app = new Koa()
    app.env = 'dev'

    const err = new Error()
    err.stack = 'Foo'

    const spy = mock.method(console, 'error', () => {})
    app.onerror(err)
    assert.notStrictEqual(spy.mock.calls.length, 0)
    spy.mock.restore()
  })
})