test: refactor test-esm-type-field-errors

Co-Authored-By: Jacob Smith <jacob@frende.me>
PR-URL: https://github.com/nodejs/node/pull/54368
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Giovanni Bucci 2024-09-06 10:29:26 +02:00 committed by GitHub
parent 94419bf64d
commit e4fdd0b492
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const exec = require('child_process').execFile; const exec = require('child_process').execFile;
const { describe, it } = require('node:test');
const mjsFile = require.resolve('../fixtures/es-modules/mjs-file.mjs'); const mjsFile = require.resolve('../fixtures/es-modules/mjs-file.mjs');
const cjsFile = require.resolve('../fixtures/es-modules/cjs-file.cjs'); const cjsFile = require.resolve('../fixtures/es-modules/cjs-file.cjs');
@ -20,18 +21,42 @@ expect('', packageTypeCommonJsMain, 'package-type-commonjs');
expect('', packageWithoutTypeMain, 'package-without-type'); expect('', packageWithoutTypeMain, 'package-without-type');
// Check that --input-type isn't allowed for files // Check that --input-type isn't allowed for files
expect('--input-type=module', packageTypeModuleMain, describe('ESM type field errors', { concurrency: true }, () => {
'ERR_INPUT_TYPE_NOT_ALLOWED', true); it('.cjs file', () => {
expect('', cjsFile, '.cjs file');
});
try { it('.mjs file', () => {
require('../fixtures/es-modules/package-type-module/index.js'); expect('', mjsFile, '.mjs file');
assert.fail('Expected CJS to fail loading from type: module package.'); });
} catch (e) {
assert.strictEqual(e.name, 'Error'); it('package.json with "type": "module"', () => {
assert.strictEqual(e.code, 'ERR_REQUIRE_ESM'); expect('', packageTypeModuleMain, 'package-type-module');
assert(e.toString().match(/require\(\) of ES Module/g)); });
assert(e.message.match(/require\(\) of ES Module/g));
} it('package.json with "type": "commonjs"', () => {
expect('', packageTypeCommonJsMain, 'package-type-commonjs');
});
it('package.json with no "type" field', () => {
expect('', packageWithoutTypeMain, 'package-without-type');
});
it('--input-type=module disallowed for files', () => {
expect(
'--input-type=module',
packageTypeModuleMain,
'ERR_INPUT_TYPE_NOT_ALLOWED',
true,
);
});
it('--input-type=module disallowed for directories', () => {
assert.throws(() => require('../fixtures/es-modules/package-type-module/index.js'), {
code: 'ERR_REQUIRE_ESM'
});
});
});
function expect(opt = '', inputFile, want, wantsError = false) { function expect(opt = '', inputFile, want, wantsError = false) {
const argv = [inputFile]; const argv = [inputFile];