Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/config-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
"homepage": "https://github.com/modernweb-dev/web/tree/master/packages/config-loader",
"main": "src/index.js",
"type": "commonjs",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.mjs",
"require": "./src/index.js"
}
},
"engines": {
"node": ">=22.0.0"
},
"scripts": {
"build": "tsc",
"test:node": "mocha test/**/*.test.js --reporter dot",
"test:watch": "mocha test/**/*.test.js --watch --watch-files .,src,test --reporter dot"
"test:node": "node --test --test-force-exit test/**/*.test.js",
"test:watch": "node --test --test-force-exit --watch test/**/*.test.js"
},
"files": [
"*.d.ts",
Expand All @@ -39,12 +46,5 @@
"es module"
],
"dependencies": {},
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.mjs",
"require": "./src/index.js"
}
}
"types": "dist/index.d.ts"
}
202 changes: 98 additions & 104 deletions packages/config-loader/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
const { describe, it } = require('node:test');
const assert = require('node:assert/strict');
const path = require('path');
const { expect } = require('chai');
const { readConfig } = require('../src/index');

const configName = 'my-project.config';
const packageCjsPath = path.resolve(__dirname, 'fixtures', 'package-cjs');
const packageMjsPath = path.resolve(__dirname, 'fixtures', 'package-mjs');

async function expectThrowsOldNodeError(configPath) {
let thrown = false;
try {
await readConfig(configName, undefined, configPath);
} catch (error) {
thrown = true;
expect(error.message).to.include(
'You are trying to load a config as es module but your version of node does not support it',
);
}
expect(thrown).to.equal(true);
}

describe('cjs package', () => {
it('can load commonjs-in-.cjs', async () => {
const result = await readConfig(
configName,
undefined,
path.resolve(packageCjsPath, 'commonjs-in-.cjs'),
);
expect(result).to.eql({ foo: 'bar' });
assert.deepEqual(result, { foo: 'bar' });
});

it('can load commonjs-in-.js', async () => {
Expand All @@ -35,56 +23,59 @@ describe('cjs package', () => {
undefined,
path.resolve(packageCjsPath, 'commonjs-in-.js'),
);
expect(result).to.eql({ foo: 'bar' });
assert.deepEqual(result, { foo: 'bar' });
});

it('can load module-in-.mjs', async () => {
const result = await readConfig(
configName,
undefined,
path.resolve(packageCjsPath, 'module-in-.mjs'),
);
expect(result).to.eql({ foo: 'bar' });
});
it('can load module-in-.mjs', async () => {
const result = await readConfig(
configName,
undefined,
path.resolve(packageCjsPath, 'module-in-.mjs'),
);
assert.deepEqual(result, { foo: 'bar' });
});

it('throws when loading module-in-.cjs', async () => {
let thrown = false;
try {
await readConfig(configName, undefined, path.resolve(packageCjsPath, 'module-in-.cjs'));
} catch (error) {
thrown = true;
expect(error.message).to.include(
'You are using es module syntax in a config loaded as CommonJS module.',
);
}
expect(thrown).to.equal(true);
await assert.rejects(
() => readConfig(configName, undefined, path.resolve(packageCjsPath, 'module-in-.cjs')),
error => {
assert.ok(
error.message.includes(
'You are using es module syntax in a config loaded as CommonJS module.',
),
);
return true;
},
);
});

it('throws when loading module-in-.js', async () => {
let thrown = false;
try {
await readConfig(configName, undefined, path.resolve(packageCjsPath, 'module-in-.js'));
} catch (error) {
thrown = true;
expect(error.message).to.include(
'You are using es module syntax in a config loaded as CommonJS module.',
);
}
expect(thrown).to.equal(true);
await assert.rejects(
() => readConfig(configName, undefined, path.resolve(packageCjsPath, 'module-in-.js')),
error => {
assert.ok(
error.message.includes(
'You are using es module syntax in a config loaded as CommonJS module.',
),
);
return true;
},
);
});

it('throws when loading commonjs-in-.mjs', async () => {
let thrown = false;
try {
await readConfig(configName, undefined, path.resolve(packageCjsPath, 'commonjs-in-.mjs'));
} catch (error) {
thrown = true;
expect(error.message).to.include(
'You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module.',
it('throws when loading commonjs-in-.mjs', async () => {
await assert.rejects(
() => readConfig(configName, undefined, path.resolve(packageCjsPath, 'commonjs-in-.mjs')),
error => {
assert.ok(
error.message.includes(
'You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module.',
),
);
}
expect(thrown).to.equal(true);
});
return true;
},
);
});
});

describe('mjs package', () => {
Expand All @@ -94,63 +85,66 @@ describe('mjs package', () => {
undefined,
path.resolve(packageMjsPath, 'commonjs-in-.cjs'),
);
expect(result).to.eql({ foo: 'bar' });
assert.deepEqual(result, { foo: 'bar' });
});

it('throws when loading commonjs-in-.js', async () => {
let thrown = false;
try {
await readConfig(configName, undefined, path.resolve(packageMjsPath, 'commonjs-in-.js'));
} catch (error) {
thrown = true;
expect(error.message).to.include(
'You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module.',
it('throws when loading commonjs-in-.js', async () => {
await assert.rejects(
() => readConfig(configName, undefined, path.resolve(packageMjsPath, 'commonjs-in-.js')),
error => {
assert.ok(
error.message.includes(
'You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module.',
),
);
}
expect(thrown).to.equal(true);
});
return true;
},
);
});

it('throws when loading commonjs-in-.mjs', async () => {
let thrown = false;
try {
await readConfig(configName, undefined, path.resolve(packageMjsPath, 'commonjs-in-.mjs'));
} catch (error) {
thrown = true;
expect(error.message).to.include(
'You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module.',
it('throws when loading commonjs-in-.mjs', async () => {
await assert.rejects(
() => readConfig(configName, undefined, path.resolve(packageMjsPath, 'commonjs-in-.mjs')),
error => {
assert.ok(
error.message.includes(
'You are using CommonJS syntax such as "require" or "module.exports" in a config loaded as es module.',
),
);
}
expect(thrown).to.equal(true);
});
return true;
},
);
});

it('throws when loading module-in-.cjs', async () => {
let thrown = false;
try {
await readConfig(configName, undefined, path.resolve(packageCjsPath, 'module-in-.cjs'));
} catch (error) {
thrown = true;
expect(error.message).to.include(
'You are using es module syntax in a config loaded as CommonJS module.',
);
}
expect(thrown).to.equal(true);
await assert.rejects(
() => readConfig(configName, undefined, path.resolve(packageCjsPath, 'module-in-.cjs')),
error => {
assert.ok(
error.message.includes(
'You are using es module syntax in a config loaded as CommonJS module.',
),
);
return true;
},
);
});

it('can load module-in-.js', async () => {
const result = await readConfig(
configName,
undefined,
path.resolve(packageMjsPath, 'module-in-.js'),
);
expect(result).to.eql({ foo: 'bar' });
});
it('can load module-in-.js', async () => {
const result = await readConfig(
configName,
undefined,
path.resolve(packageMjsPath, 'module-in-.js'),
);
assert.deepEqual(result, { foo: 'bar' });
});

it('can load module-in-.mjs', async () => {
const result = await readConfig(
configName,
undefined,
path.resolve(packageMjsPath, 'module-in-.mjs'),
);
expect(result).to.eql({ foo: 'bar' });
});
it('can load module-in-.mjs', async () => {
const result = await readConfig(
configName,
undefined,
path.resolve(packageMjsPath, 'module-in-.mjs'),
);
assert.deepEqual(result, { foo: 'bar' });
});
});
Loading