Back to Repositories

Testing Query String Operations in Koa Framework

This test suite validates the query string handling functionality in Koa’s request context, covering both query string parsing and manipulation. The tests ensure proper behavior of ctx.query operations and URL transformations in the Koa framework.

Test Coverage Overview

The test suite provides comprehensive coverage of query string operations in Koa’s context object.

  • Tests empty query string handling and object persistence
  • Validates query string parsing from URLs
  • Verifies query string modification and URL updates
  • Ensures originalUrl remains unchanged during modifications

Implementation Analysis

The testing approach uses Node’s built-in test framework with a modular structure focusing on ctx.query operations.

The implementation employs describe/it blocks for clear test organization and uses test-helpers/context for consistent test environment setup. The tests validate both read and write operations on query parameters.

Technical Details

  • Node.js test runner (node:test)
  • Assert module for assertions
  • Custom context test helper
  • Strict mode JavaScript
  • Nested describe blocks for logical grouping

Best Practices Demonstrated

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

  • Isolated test cases with clear descriptions
  • Consistent test helper usage
  • Comprehensive edge case coverage
  • Proper assertion usage for validations
  • Maintainable test structure with logical grouping

koajs/koa

__tests__/request/query.test.js

            
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')

describe('ctx.query', () => {
  describe('when missing', () => {
    it('should return an empty object', () => {
      const ctx = context({ url: '/' })
      assert(!Object.keys(ctx.query).length)
    })

    it('should return the same object each time it\'s accessed', () => {
      const ctx = context({ url: '/' })
      ctx.query.a = '2'
      assert.strictEqual(ctx.query.a, '2')
    })
  })

  it('should return a parsed query string', () => {
    const ctx = context({ url: '/?page=2' })
    assert.strictEqual(ctx.query.page, '2')
  })
})

describe('ctx.query=', () => {
  it('should stringify and replace the query string and search', () => {
    const ctx = context({ url: '/store/shoes' })
    ctx.query = { page: 2, color: 'blue' }
    assert.strictEqual(ctx.url, '/store/shoes?page=2&color=blue')
    assert.strictEqual(ctx.querystring, 'page=2&color=blue')
    assert.strictEqual(ctx.search, '?page=2&color=blue')
  })

  it('should change .url but not .originalUrl', () => {
    const ctx = context({ url: '/store/shoes' })
    ctx.query = { page: 2 }
    assert.strictEqual(ctx.url, '/store/shoes?page=2')
    assert.strictEqual(ctx.originalUrl, '/store/shoes')
    assert.strictEqual(ctx.request.originalUrl, '/store/shoes')
  })
})