|
| 1 | +import { test } from "node:test"; |
| 2 | +import { strict as assert } from "node:assert"; |
| 3 | +import { createRequire } from "module"; |
| 4 | +import path from "path"; |
| 5 | +import { fileURLToPath } from "url"; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | + |
| 10 | +test("CommonJS module import", { timeout: 5000 }, (t) => { |
| 11 | + const require = createRequire(__filename); |
| 12 | + const cjsPath = path.resolve(__dirname, "../../dist/cjs/index.js"); |
| 13 | + |
| 14 | + const cjsModule = require(cjsPath); |
| 15 | + |
| 16 | + assert.equal(typeof cjsModule, "object", "CommonJS module should be an object"); |
| 17 | + assert.ok(cjsModule, "CommonJS module should be truthy"); |
| 18 | +}); |
| 19 | + |
| 20 | +test("ESM module import", { timeout: 5000 }, async (t) => { |
| 21 | + const esmPath = path.resolve(__dirname, "../../dist/esm/d.js"); |
| 22 | + |
| 23 | + const esmModule = await import(esmPath); |
| 24 | + |
| 25 | + assert.equal(typeof esmModule, "object", "ESM module should be an object"); |
| 26 | + assert.ok(esmModule, "ESM module should be truthy"); |
| 27 | +}); |
| 28 | + |
| 29 | +test("module exports comparison", { timeout: 5000 }, async () => { |
| 30 | + const require = createRequire(__filename); |
| 31 | + const cjsPath = path.resolve(__dirname, "../../dist/cjs/index.js"); |
| 32 | + const esmPath = path.resolve(__dirname, "../../dist/esm/d.js"); |
| 33 | + |
| 34 | + const cjsModule = require(cjsPath); |
| 35 | + const esmModule = await import(esmPath); |
| 36 | + |
| 37 | + const cjsKeys = Object.keys(cjsModule).sort(); |
| 38 | + const esmKeys = Object.keys(esmModule).sort(); |
| 39 | + |
| 40 | + assert.deepEqual( |
| 41 | + cjsKeys, |
| 42 | + esmKeys, |
| 43 | + `CommonJS and ESM modules should export the same keys.\nCommonJS: ${JSON.stringify(cjsKeys)}\nESM: ${JSON.stringify(esmKeys)}` |
| 44 | + ); |
| 45 | +}); |
0 commit comments