Skip to content

Commit 60893ee

Browse files
authored
Merge pull request #3079 from modernweb-dev/migrate/config-loader-node-test
refactor(config-loader): migrate tests to node:test
2 parents 30f62b4 + 5bd282c commit 60893ee

2 files changed

Lines changed: 69 additions & 117 deletions

File tree

packages/config-loader/package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@
1515
"homepage": "https://github.com/modernweb-dev/web/tree/master/packages/config-loader",
1616
"main": "src/index.js",
1717
"type": "commonjs",
18+
"exports": {
19+
".": {
20+
"types": "./index.d.ts",
21+
"import": "./index.mjs",
22+
"require": "./src/index.js"
23+
}
24+
},
1825
"engines": {
1926
"node": ">=22.0.0"
2027
},
2128
"scripts": {
2229
"build": "tsc",
23-
"test:node": "mocha test/**/*.test.js --reporter dot",
24-
"test:watch": "mocha test/**/*.test.js --watch --watch-files .,src,test --reporter dot"
30+
"test:node": "node --test --test-force-exit test/**/*.test.js",
31+
"test:watch": "node --test --test-force-exit --watch test/**/*.test.js"
2532
},
2633
"files": [
2734
"*.d.ts",
@@ -39,12 +46,5 @@
3946
"es module"
4047
],
4148
"dependencies": {},
42-
"types": "dist/index.d.ts",
43-
"exports": {
44-
".": {
45-
"types": "./index.d.ts",
46-
"import": "./index.mjs",
47-
"require": "./src/index.js"
48-
}
49-
}
49+
"types": "dist/index.d.ts"
5050
}
Lines changed: 59 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
1+
const { describe, it } = require('node:test');
2+
const assert = require('node:assert/strict');
13
const path = require('path');
2-
const { expect } = require('chai');
34
const { readConfig } = require('../src/index');
45

56
const configName = 'my-project.config';
67
const packageCjsPath = path.resolve(__dirname, 'fixtures', 'package-cjs');
78
const 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-
2210
describe('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+
/You are using es module syntax in a config loaded as CommonJS module./,
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+
/You are using es module syntax in a config loaded as CommonJS module./,
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+
/You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module./,
56+
);
57+
});
8858
});
8959

9060
describe('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+
/You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module./,
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+
/You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module./,
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+
/You are using es module syntax in a config loaded as CommonJS module./,
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

Comments
 (0)