Skip to content

Commit 3606c53

Browse files
Giovanni BucciJakobJingleheimer
authored andcommitted
test: refactor test-esm-type-field-errors
Co-Authored-By: Jacob Smith <jacob@frende.me> PR-URL: #54368 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e635e09 commit 3606c53

1 file changed

Lines changed: 37 additions & 12 deletions

File tree

‎test/es-module/test-esm-type-field-errors.js‎

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
constcommon=require('../common');
33
constassert=require('assert');
44
constexec=require('child_process').execFile;
5+
const{ describe, it }=require('node:test');
56

67
constmjsFile=require.resolve('../fixtures/es-modules/mjs-file.mjs');
78
constcjsFile=require.resolve('../fixtures/es-modules/cjs-file.cjs');
@@ -20,18 +21,42 @@ expect('', packageTypeCommonJsMain, 'package-type-commonjs');
2021
expect('',packageWithoutTypeMain,'package-without-type');
2122

2223
// 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\(\)ofESModule/g));
33-
assert(e.message.match(/require\(\)ofESModule/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+
});
3560

3661
functionexpect(opt='',inputFile,want,wantsError=false){
3762
constargv=[inputFile];

0 commit comments

Comments
 (0)