1+ const { describe, it } = require ( 'node:test' ) ;
2+ const assert = require ( 'node:assert/strict' ) ;
13const path = require ( 'path' ) ;
2- const { expect } = require ( 'chai' ) ;
34const { readConfig } = require ( '../src/index' ) ;
45
56const configName = 'my-project.config' ;
67const packageCjsPath = path . resolve ( __dirname , 'fixtures' , 'package-cjs' ) ;
78const packageMjsPath = path . resolve ( __dirname , 'fixtures' , 'package-mjs' ) ;
89
9- async function expectThrowsOldNodeError ( configPath ) {
10- let thrown = false ;
11- try {
12- await readConfig ( configName , undefined , configPath ) ;
13- } catch ( error ) {
14- thrown = true ;
15- expect ( error . message ) . to . include (
16- 'You are trying to load a config as es module but your version of node does not support it' ,
17- ) ;
18- }
19- expect ( thrown ) . to . equal ( true ) ;
20- }
21-
2210describe ( 'cjs package' , ( ) => {
2311 it ( 'can load commonjs-in-.cjs' , async ( ) => {
2412 const result = await readConfig (
2513 configName ,
2614 undefined ,
2715 path . resolve ( packageCjsPath , 'commonjs-in-.cjs' ) ,
2816 ) ;
29- expect ( result ) . to . eql ( { foo : 'bar' } ) ;
17+ assert . deepStrictEqual ( result , { foo : 'bar' } ) ;
3018 } ) ;
3119
3220 it ( 'can load commonjs-in-.js' , async ( ) => {
@@ -35,56 +23,38 @@ describe('cjs package', () => {
3523 undefined ,
3624 path . resolve ( packageCjsPath , 'commonjs-in-.js' ) ,
3725 ) ;
38- expect ( result ) . to . eql ( { foo : 'bar' } ) ;
26+ assert . deepStrictEqual ( result , { foo : 'bar' } ) ;
3927 } ) ;
4028
41- it ( 'can load module-in-.mjs' , async ( ) => {
42- const result = await readConfig (
43- configName ,
44- undefined ,
45- path . resolve ( packageCjsPath , 'module-in-.mjs' ) ,
46- ) ;
47- expect ( result ) . to . eql ( { foo : 'bar' } ) ;
48- } ) ;
29+ it ( 'can load module-in-.mjs' , async ( ) => {
30+ const result = await readConfig (
31+ configName ,
32+ undefined ,
33+ path . resolve ( packageCjsPath , 'module-in-.mjs' ) ,
34+ ) ;
35+ assert . deepStrictEqual ( result , { foo : 'bar' } ) ;
36+ } ) ;
4937
5038 it ( 'throws when loading module-in-.cjs' , async ( ) => {
51- let thrown = false ;
52- try {
53- await readConfig ( configName , undefined , path . resolve ( packageCjsPath , 'module-in-.cjs' ) ) ;
54- } catch ( error ) {
55- thrown = true ;
56- expect ( error . message ) . to . include (
57- 'You are using es module syntax in a config loaded as CommonJS module.' ,
58- ) ;
59- }
60- expect ( thrown ) . to . equal ( true ) ;
39+ await assert . rejects (
40+ ( ) => readConfig ( configName , undefined , path . resolve ( packageCjsPath , 'module-in-.cjs' ) ) ,
41+ / Y o u a r e u s i n g e s m o d u l e s y n t a x i n a c o n f i g l o a d e d a s C o m m o n J S m o d u l e ./ ,
42+ ) ;
6143 } ) ;
6244
6345 it ( 'throws when loading module-in-.js' , async ( ) => {
64- let thrown = false ;
65- try {
66- await readConfig ( configName , undefined , path . resolve ( packageCjsPath , 'module-in-.js' ) ) ;
67- } catch ( error ) {
68- thrown = true ;
69- expect ( error . message ) . to . include (
70- 'You are using es module syntax in a config loaded as CommonJS module.' ,
71- ) ;
72- }
73- expect ( thrown ) . to . equal ( true ) ;
46+ await assert . rejects (
47+ ( ) => readConfig ( configName , undefined , path . resolve ( packageCjsPath , 'module-in-.js' ) ) ,
48+ / Y o u a r e u s i n g e s m o d u l e s y n t a x i n a c o n f i g l o a d e d a s C o m m o n J S m o d u l e ./ ,
49+ ) ;
7450 } ) ;
7551
76- it ( 'throws when loading commonjs-in-.mjs' , async ( ) => {
77- let thrown = false ;
78- try {
79- await readConfig ( configName , undefined , path . resolve ( packageCjsPath , 'commonjs-in-.mjs' ) ) ;
80- } catch ( error ) {
81- thrown = true ;
82- expect ( error . message ) . to . include (
83- 'You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module.' ,
84- ) ;
85- }
86- expect ( thrown ) . to . equal ( true ) ;
87- } ) ;
52+ it ( 'throws when loading commonjs-in-.mjs' , async ( ) => {
53+ await assert . rejects (
54+ ( ) => readConfig ( configName , undefined , path . resolve ( packageCjsPath , 'commonjs-in-.mjs' ) ) ,
55+ / Y o u a r e u s i n g C o m m o n J S s y n t a x s u c h a s " r e q u i r e " o r " m o d u l e .e x p o r t s " i n a c o n f i g l o a d e d a s e s m o d u l e ./ ,
56+ ) ;
57+ } ) ;
8858} ) ;
8959
9060describe ( 'mjs package' , ( ) => {
@@ -94,63 +64,45 @@ describe('mjs package', () => {
9464 undefined ,
9565 path . resolve ( packageMjsPath , 'commonjs-in-.cjs' ) ,
9666 ) ;
97- expect ( result ) . to . eql ( { foo : 'bar' } ) ;
67+ assert . deepStrictEqual ( result , { foo : 'bar' } ) ;
9868 } ) ;
9969
100- it ( 'throws when loading commonjs-in-.js' , async ( ) => {
101- let thrown = false ;
102- try {
103- await readConfig ( configName , undefined , path . resolve ( packageMjsPath , 'commonjs-in-.js' ) ) ;
104- } catch ( error ) {
105- thrown = true ;
106- expect ( error . message ) . to . include (
107- 'You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module.' ,
108- ) ;
109- }
110- expect ( thrown ) . to . equal ( true ) ;
111- } ) ;
70+ it ( 'throws when loading commonjs-in-.js' , async ( ) => {
71+ await assert . rejects (
72+ ( ) => readConfig ( configName , undefined , path . resolve ( packageMjsPath , 'commonjs-in-.js' ) ) ,
73+ / Y o u a r e u s i n g C o m m o n J S s y n t a x s u c h a s " r e q u i r e " o r " m o d u l e .e x p o r t s " i n a c o n f i g l o a d e d a s e s m o d u l e ./ ,
74+ ) ;
75+ } ) ;
11276
113- it ( 'throws when loading commonjs-in-.mjs' , async ( ) => {
114- let thrown = false ;
115- try {
116- await readConfig ( configName , undefined , path . resolve ( packageMjsPath , 'commonjs-in-.mjs' ) ) ;
117- } catch ( error ) {
118- thrown = true ;
119- expect ( error . message ) . to . include (
120- 'You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module.' ,
121- ) ;
122- }
123- expect ( thrown ) . to . equal ( true ) ;
124- } ) ;
77+ it ( 'throws when loading commonjs-in-.mjs' , async ( ) => {
78+ await assert . rejects (
79+ ( ) => readConfig ( configName , undefined , path . resolve ( packageMjsPath , 'commonjs-in-.mjs' ) ) ,
80+ / Y o u a r e u s i n g C o m m o n J S s y n t a x s u c h a s " r e q u i r e " o r " m o d u l e .e x p o r t s " i n a c o n f i g l o a d e d a s e s m o d u l e ./ ,
81+ ) ;
82+ } ) ;
12583
12684 it ( 'throws when loading module-in-.cjs' , async ( ) => {
127- let thrown = false ;
128- try {
129- await readConfig ( configName , undefined , path . resolve ( packageCjsPath , 'module-in-.cjs' ) ) ;
130- } catch ( error ) {
131- thrown = true ;
132- expect ( error . message ) . to . include (
133- 'You are using es module syntax in a config loaded as CommonJS module.' ,
134- ) ;
135- }
136- expect ( thrown ) . to . equal ( true ) ;
85+ await assert . rejects (
86+ ( ) => readConfig ( configName , undefined , path . resolve ( packageCjsPath , 'module-in-.cjs' ) ) ,
87+ / Y o u a r e u s i n g e s m o d u l e s y n t a x i n a c o n f i g l o a d e d a s C o m m o n J S m o d u l e ./ ,
88+ ) ;
13789 } ) ;
13890
139- it ( 'can load module-in-.js' , async ( ) => {
140- const result = await readConfig (
141- configName ,
142- undefined ,
143- path . resolve ( packageMjsPath , 'module-in-.js' ) ,
144- ) ;
145- expect ( result ) . to . eql ( { foo : 'bar' } ) ;
146- } ) ;
91+ it ( 'can load module-in-.js' , async ( ) => {
92+ const result = await readConfig (
93+ configName ,
94+ undefined ,
95+ path . resolve ( packageMjsPath , 'module-in-.js' ) ,
96+ ) ;
97+ assert . deepStrictEqual ( result , { foo : 'bar' } ) ;
98+ } ) ;
14799
148- it ( 'can load module-in-.mjs' , async ( ) => {
149- const result = await readConfig (
150- configName ,
151- undefined ,
152- path . resolve ( packageMjsPath , 'module-in-.mjs' ) ,
153- ) ;
154- expect ( result ) . to . eql ( { foo : 'bar' } ) ;
155- } ) ;
100+ it ( 'can load module-in-.mjs' , async ( ) => {
101+ const result = await readConfig (
102+ configName ,
103+ undefined ,
104+ path . resolve ( packageMjsPath , 'module-in-.mjs' ) ,
105+ ) ;
106+ assert . deepStrictEqual ( result , { foo : 'bar' } ) ;
107+ } ) ;
156108} ) ;
0 commit comments