☕️ Classic, reliable, trusted test framework for Node.js and the browser
test: add esm loader test (#5383)
Chengzhong Wuf58e49f08df2
diff --git a/eslint.config.js b/eslint.config.jsindex 026dffc5..dbdcc123 100644--- a/eslint.config.js+++ b/eslint.config.js@@ -68,6 +68,16 @@ module.exports = [sourceType: 'module'}},+ {+ files: [+ 'test/compiler-esm/*.js'+ ],+ languageOptions: {+ sourceType: 'module',+ // For top-level await support.+ ecmaVersion: 2022,+ }+ },{files: ['test/**/*.{js,mjs}'],languageOptions: {diff --git a/test/compiler-esm/package.json b/test/compiler-esm/package.jsonnew file mode 100644index 00000000..48cd36f0--- /dev/null+++ b/test/compiler-esm/package.json@@ -0,0 +1,4 @@+{+ "//": "Setting default type for .ts files as ESM as well",+ "type": "module"+}diff --git a/test/compiler-esm/test-tla.js b/test/compiler-esm/test-tla.jsnew file mode 100644index 00000000..55705485--- /dev/null+++ b/test/compiler-esm/test-tla.js@@ -0,0 +1,11 @@+const obj = { foo: 'bar' };++describe('esm written in esm with top-level-await', () => {+ it('should work', () => {+ expect(obj, 'to equal', { foo: 'bar' });+ });+});++await undefined;++export const foo = 'bar';diff --git a/test/compiler-esm/test-tla.js.compiled b/test/compiler-esm/test-tla.js.compilednew file mode 100644index 00000000..55705485--- /dev/null+++ b/test/compiler-esm/test-tla.js.compiled@@ -0,0 +1,11 @@+const obj = { foo: 'bar' };++describe('esm written in esm with top-level-await', () => {+ it('should work', () => {+ expect(obj, 'to equal', { foo: 'bar' });+ });+});++await undefined;++export const foo = 'bar';diff --git a/test/compiler-esm/test-tla.ts b/test/compiler-esm/test-tla.tsnew file mode 100644index 00000000..ec6e0e05--- /dev/null+++ b/test/compiler-esm/test-tla.ts@@ -0,0 +1,14 @@+const obj: unknown = { foo: 'bar' };+enum Foo {+ Bar = 'bar',+}++describe('esm written in esm with top-level-await', () => {+ it('should work', () => {+ expect(obj, 'to equal', { foo: Foo.Bar });+ });+});++await undefined;++export const foo = 'bar';diff --git a/test/compiler-esm/test-tla.ts.compiled b/test/compiler-esm/test-tla.ts.compilednew file mode 100644index 00000000..dbafd398--- /dev/null+++ b/test/compiler-esm/test-tla.ts.compiled@@ -0,0 +1,14 @@+const obj = { foo: 'bar' };+const Foo = {+ Bar: 'bar',+};++describe('esm written in esm with top-level-await', () => {+ it('should work', () => {+ expect(obj, 'to equal', { foo: Foo.Bar });+ });+});++await undefined;++export const foo = 'bar';diff --git a/test/compiler-esm/test.js b/test/compiler-esm/test.jsnew file mode 100644index 00000000..bb457784--- /dev/null+++ b/test/compiler-esm/test.js@@ -0,0 +1,9 @@+const obj = { foo: 'bar' };++describe('esm written in esm', () => {+ it('should work', () => {+ expect(obj, 'to equal', { foo: 'bar' });+ });+});++export const foo = 'bar';diff --git a/test/compiler-esm/test.js.compiled b/test/compiler-esm/test.js.compilednew file mode 100644index 00000000..bb457784--- /dev/null+++ b/test/compiler-esm/test.js.compiled@@ -0,0 +1,9 @@+const obj = { foo: 'bar' };++describe('esm written in esm', () => {+ it('should work', () => {+ expect(obj, 'to equal', { foo: 'bar' });+ });+});++export const foo = 'bar';diff --git a/test/compiler-esm/test.ts b/test/compiler-esm/test.tsnew file mode 100644index 00000000..b21a1f08--- /dev/null+++ b/test/compiler-esm/test.ts@@ -0,0 +1,12 @@+const obj: unknown = { foo: 'bar' };+enum Foo {+ Bar = 'bar',+};++describe('esm written in esm', () => {+ it('should work', () => {+ expect(obj, 'to equal', { foo: Foo.Bar });+ });+});++export const foo = 'bar';diff --git a/test/compiler-esm/test.ts.compiled b/test/compiler-esm/test.ts.compilednew file mode 100644index 00000000..cf03e3af--- /dev/null+++ b/test/compiler-esm/test.ts.compiled@@ -0,0 +1,12 @@+const obj = { foo: 'bar' };+const Foo = {+ Bar: 'bar',+};++describe('esm written in esm', () => {+ it('should work', () => {+ expect(obj, 'to equal', { foo: Foo.Bar });+ });+});++export const foo = 'bar';diff --git a/test/compiler-fixtures/esm-loader.fixture.mjs b/test/compiler-fixtures/esm-loader.fixture.mjsnew file mode 100644index 00000000..45c0a3ff--- /dev/null+++ b/test/compiler-fixtures/esm-loader.fixture.mjs@@ -0,0 +1,15 @@+import fsPromises from 'node:fs/promises';+import { fileURLToPath } from 'node:url';++export async function load(url, context, nextLoad) {+ if (!url.includes('compiler-esm') && !url.includes('compiler-cjs')) {+ return nextLoad(url, context);+ }+ const format = url.includes('compiler-esm') ? 'module' : 'commonjs';+ const content = await fsPromises.readFile(fileURLToPath(url + '.compiled'), 'utf8');+ return {+ format,+ source: content,+ shortCircuit: true,+ }+}diff --git a/test/compiler-fixtures/esm.fixture.js b/test/compiler-fixtures/esm.fixture.jsnew file mode 100644index 00000000..c7e09332--- /dev/null+++ b/test/compiler-fixtures/esm.fixture.js@@ -0,0 +1,5 @@+const Module = require('node:module');+const { pathToFileURL } = require('node:url');+const path = require('node:path');++Module.register(pathToFileURL(path.resolve(__dirname, 'esm-loader.fixture.mjs')));diff --git a/test/integration/compiler-esm.spec.js b/test/integration/compiler-esm.spec.jsnew file mode 100644index 00000000..10a486a7--- /dev/null+++ b/test/integration/compiler-esm.spec.js@@ -0,0 +1,70 @@+'use strict';++var exec = require('node:child_process').exec;+var path = require('node:path');++describe('support ESM module loader compilers', function () {+ it('should support ESM .js extension', function (done) {+ exec(+ '"' ++ process.execPath ++ '" "' ++ path.join('bin', 'mocha') ++ '" -R json --require test/compiler-fixtures/esm.fixture "test/compiler-esm/*.js"',+ {cwd: path.join(__dirname, '..', '..')},+ function (error, stdout) {+ if (error && !stdout) {+ return done(error);+ }+ var results = JSON.parse(stdout);+ expect(results, 'to have property', 'tests');+ var titles = [];+ for (var index = 0; index < results.tests.length; index += 1) {+ expect(results.tests[index], 'to have property', 'fullTitle');+ titles.push(results.tests[index].fullTitle);+ }+ expect(+ titles,+ 'to contain',+ 'esm written in esm should work',+ ).and(+ 'to contain',+ 'esm written in esm with top-level-await should work',+ ).and('to have length', 2);+ done();+ }+ );+ });++ it('should support ESM .ts extension', function (done) {+ exec(+ '"' ++ process.execPath ++ '" "' ++ path.join('bin', 'mocha') ++ '" -R json --require test/compiler-fixtures/esm.fixture "test/compiler-esm/*.ts"',+ {cwd: path.join(__dirname, '..', '..')},+ function (error, stdout) {+ if (error && !stdout) {+ return done(error);+ }+ var results = JSON.parse(stdout);+ expect(results, 'to have property', 'tests');+ var titles = [];+ for (var index = 0; index < results.tests.length; index += 1) {+ expect(results.tests[index], 'to have property', 'fullTitle');+ titles.push(results.tests[index].fullTitle);+ }+ expect(+ titles,+ 'to contain',+ 'esm written in esm should work',+ ).and(+ 'to contain',+ 'esm written in esm with top-level-await should work',+ ).and('to have length', 2);+ done();+ }+ );+ });+});