|
2 | 2 | const common = require('../common'); |
3 | 3 | const assert = require('assert'); |
4 | 4 | const exec = require('child_process').execFile; |
| 5 | +const { describe, it } = require('node:test'); |
5 | 6 |
|
6 | 7 | const mjsFile = require.resolve('../fixtures/es-modules/mjs-file.mjs'); |
7 | 8 | const cjsFile = require.resolve('../fixtures/es-modules/cjs-file.cjs'); |
@@ -20,18 +21,42 @@ expect('', packageTypeCommonJsMain, 'package-type-commonjs'); |
20 | 21 | expect('', packageWithoutTypeMain, 'package-without-type'); |
21 | 22 |
|
22 | 23 | // Check that --input-type isn't allowed for files |
23 | | -expect('--input-type=module', packageTypeModuleMain, |
24 | | - 'ERR_INPUT_TYPE_NOT_ALLOWED', true); |
25 | | - |
26 | | -try { |
27 | | - require('../fixtures/es-modules/package-type-module/index.js'); |
28 | | - assert.fail('Expected CJS to fail loading from type: module package.'); |
29 | | -} catch (e) { |
30 | | - assert.strictEqual(e.name, 'Error'); |
31 | | - assert.strictEqual(e.code, 'ERR_REQUIRE_ESM'); |
32 | | - assert(e.toString().match(/require\(\) of ES Module/g)); |
33 | | - assert(e.message.match(/require\(\) of ES Module/g)); |
34 | | -} |
| 24 | +describe('ESM type field errors', { concurrency: true }, () => { |
| 25 | + it('.cjs file', () => { |
| 26 | + expect('', cjsFile, '.cjs file'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('.mjs file', () => { |
| 30 | + expect('', mjsFile, '.mjs file'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('package.json with "type": "module"', () => { |
| 34 | + expect('', packageTypeModuleMain, 'package-type-module'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('package.json with "type": "commonjs"', () => { |
| 38 | + expect('', packageTypeCommonJsMain, 'package-type-commonjs'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('package.json with no "type" field', () => { |
| 42 | + expect('', packageWithoutTypeMain, 'package-without-type'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('--input-type=module disallowed for files', () => { |
| 46 | + expect( |
| 47 | + '--input-type=module', |
| 48 | + packageTypeModuleMain, |
| 49 | + 'ERR_INPUT_TYPE_NOT_ALLOWED', |
| 50 | + true, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('--input-type=module disallowed for directories', () => { |
| 55 | + assert.throws(() => require('../fixtures/es-modules/package-type-module/index.js'), { |
| 56 | + code: 'ERR_REQUIRE_ESM' |
| 57 | + }); |
| 58 | + }); |
| 59 | +}); |
35 | 60 |
|
36 | 61 | function expect(opt = '', inputFile, want, wantsError = false) { |
37 | 62 | const argv = [inputFile]; |
|