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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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)
})
})