Back to Repositories

Testing ESM Module Loading Compatibility in Koa.js

This test suite validates ESM (ECMAScript Modules) compatibility in Koa.js, ensuring proper module loading and exports between CommonJS and ESM formats. It specifically tests the default export functionality and property matching between different module systems.

Test Coverage Overview

The test suite provides comprehensive coverage of Koa.js module loading behavior across different module systems.

Key areas tested include:
  • Default export validation for ESM imports
  • Property name matching between ESM and CommonJS exports
  • CommonJS default property existence and self-referencing
  • Version-specific ESM support validation

Implementation Analysis

The testing approach employs Node’s native test framework with async/await patterns for module loading tests. It utilizes dynamic imports through eval for ESM compatibility and implements version checking for proper Node.js version support.

Key implementation features:
  • Dynamic ESM import handling
  • Property comparison using Set data structures
  • Strict equality assertions for export validation
  • Version-based test skipping logic

Technical Details

Testing tools and configuration:
  • Node.js test runner (node:test)
  • Native assert module
  • beforeAll hooks for environment setup
  • Dynamic import evaluation for ESM support
  • Version detection for conditional test execution
  • Set data structures for property comparison

Best Practices Demonstrated

The test suite exemplifies several testing best practices for module system compatibility testing.

Notable practices include:
  • Environment-aware test execution
  • Proper isolation of module loading concerns
  • Comprehensive export verification
  • Careful handling of module system differences
  • Clear separation of setup and test cases

koajs/koa

__tests__/load-with-esm.test.js

            
const { describe, it, beforeAll } = require('node:test')
const assert = require('assert')

let importESM = () => {}

describe.skip('Load with esm', () => {
  beforeAll(function () {
    // ESM support is flagged on v12.x.
    const majorVersion = +process.version.split('.')[0].slice(1)
    if (majorVersion < 12) {
      this.skip()
    } else {
      // eslint-disable-next-line no-eval
      importESM = eval('(specifier) => import(specifier)')
    }
  })

  it('should default export koa', async () => {
    const exported = await importESM('koa')
    const required = require('../')
    assert.strictEqual(exported.default, required)
  })

  it('should match exports own property names', async () => {
    const exported = new Set(Object.getOwnPropertyNames(await importESM('koa')))
    const required = new Set(Object.getOwnPropertyNames(require('../')))

    // Remove constructor properties + default export.
    for (const k of ['prototype', 'length', 'name']) {
      required.delete(k)
    }

    // Commented out to "fix" CommonJS, ESM, bundling issue.
    // @see https://github.com/koajs/koa/issues/1513
    // exported.delete('default');

    assert.strictEqual(exported.size, required.size)
    assert.strictEqual([...exported].every(property => required.has(property)), true)
  })

  it('CommonJS exports default property', async () => {
    const required = require('../')
    assert.strictEqual(Object.prototype.hasOwnProperty.call(required, 'default'), true)
  })

  it('CommonJS exports default property referencing self', async () => {
    const required = require('../')
    assert.strictEqual(required.default, required)
  })
})