Skip to content

Commit

Permalink
test: add config test
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Jun 21, 2024
1 parent ee00275 commit 2a60a86
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 0 deletions.
109 changes: 109 additions & 0 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1`] = `
[
{
"output": {
"distPath": {
"js": "./",
},
"filenameHash": false,
"minify": false,
},
"source": {
"alias": {
"bar": "bar",
"foo": "foo/esm",
},
"preEntry": "./b.js",
},
"tools": {
"htmlPlugin": false,
"rspack": {
"experiments": {
"outputModule": true,
},
"optimization": {
"concatenateModules": true,
},
"output": {
"library": {
"type": "module",
},
"module": true,
},
},
},
},
{
"output": {
"distPath": {
"js": "./",
},
"filenameHash": false,
"minify": false,
},
"source": {
"alias": {
"bar": "bar/cjs",
"foo": "foo",
},
"preEntry": [
"./a.js",
"./c.js",
"./d.js",
],
},
"tools": {
"htmlPlugin": false,
"rspack": {
"experiments": {
"outputModule": true,
},
"optimization": {
"concatenateModules": true,
},
"output": {
"library": {
"type": "module",
},
"module": true,
},
},
},
},
{
"output": {
"distPath": {
"js": "./",
},
"filenameHash": false,
"minify": false,
},
"source": {
"alias": {
"bar": "bar",
"foo": "foo",
},
"preEntry": "./a.js",
},
"tools": {
"htmlPlugin": false,
"rspack": {
"experiments": {
"outputModule": true,
},
"optimization": {
"concatenateModules": true,
},
"output": {
"library": {
"type": "module",
},
"module": true,
},
},
},
},
]
`;
185 changes: 185 additions & 0 deletions packages/core/tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { join } from 'node:path';
import { describe, expect, test } from 'vitest';
import { composeCreateRsbuildConfig, loadConfig } from '../src/config';
import type { RslibConfig } from '../src/types/config';

describe('Should load config file correctly', () => {
test('Load config.js in cjs project', async () => {
const fixtureDir = join(__dirname, 'config/cjs');
const configDir = join(fixtureDir, 'rslib.config.js');
const config = await loadConfig(configDir);
expect(config).toEqual({
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
_privateMeta: {
configFilePath: configDir,
},
});
});

test('Load config.mjs in cjs project', async () => {
const fixtureDir = join(__dirname, 'config/cjs');
const configDir = join(fixtureDir, 'rslib.config.mjs');
const config = await loadConfig(configDir);
expect(config).toEqual({
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
_privateMeta: {
configFilePath: configDir,
},
});
});

test('Load config.ts in cjs project', async () => {
const fixtureDir = join(__dirname, 'config/cjs');
const configDir = join(fixtureDir, 'rslib.config.ts');
const config = await loadConfig(configDir);
expect(config).toEqual({
lib: [],
source: {
entry: {
main: './foo/index.ts',
},
},
_privateMeta: {
configFilePath: configDir,
},
});
});

test('Load config.cjs with defineConfig in cjs project', async () => {
const fixtureDir = join(__dirname, 'config/cjs');
const configDir = join(fixtureDir, 'rslib.config.cjs');
const config = await loadConfig(configDir);
expect(config).toEqual({
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
_privateMeta: {
configFilePath: configDir,
},
});
});

test('Load config.js in esm project', async () => {
const fixtureDir = join(__dirname, 'config/esm');
const configDir = join(fixtureDir, 'rslib.config.js');
const config = await loadConfig(configDir);
expect(config).toEqual({
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
_privateMeta: {
configFilePath: configDir,
},
});
});

test('Load config.cjs in esm project', async () => {
const fixtureDir = join(__dirname, 'config/esm');
const configDir = join(fixtureDir, 'rslib.config.cjs');
const config = await loadConfig(configDir);
expect(config).toEqual({
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
_privateMeta: {
configFilePath: configDir,
},
});
});

test('Load config.ts in esm project', async () => {
const fixtureDir = join(__dirname, 'config/esm');
const configDir = join(fixtureDir, 'rslib.config.ts');
const config = await loadConfig(configDir);
expect(config).toEqual({
lib: [],
source: {
entry: {
main: './foo/index.ts',
},
},
_privateMeta: {
configFilePath: configDir,
},
});
});

test('Load config.mjs with defineConfig in esm project', async () => {
const fixtureDir = join(__dirname, 'config/esm');
const configDir = join(fixtureDir, 'rslib.config.mjs');
const config = await loadConfig(configDir);
expect(config).toEqual({
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
_privateMeta: {
configFilePath: configDir,
},
});
});
});

describe('Should compose create Rsbuild config correctly', () => {
test('Merge Rsbuild config', async () => {
const rslibConfig: RslibConfig = {
lib: [
{
format: 'esm',
source: {
alias: {
foo: 'foo/esm',
},
preEntry: './b.js',
},
},
{
format: 'cjs',
source: {
alias: {
bar: 'bar/cjs',
},
preEntry: ['./c.js', './d.js'],
},
},
{
format: 'umd',
},
],
source: {
alias: {
foo: 'foo',
bar: 'bar',
},
preEntry: './a.js',
},
output: {
filenameHash: false,
minify: true,
},
};
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);
expect(composedRsbuildConfig).toMatchSnapshot();
});
});
3 changes: 3 additions & 0 deletions packages/core/tests/config/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
10 changes: 10 additions & 0 deletions packages/core/tests/config/cjs/rslib.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { defineConfig } = require('../../../../core/src/config');

module.exports = defineConfig((args) => ({
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
}));
8 changes: 8 additions & 0 deletions packages/core/tests/config/cjs/rslib.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
};
8 changes: 8 additions & 0 deletions packages/core/tests/config/cjs/rslib.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
};
8 changes: 8 additions & 0 deletions packages/core/tests/config/cjs/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
lib: [],
source: {
entry: {
main: './foo/index.ts',
},
},
};
3 changes: 3 additions & 0 deletions packages/core/tests/config/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
8 changes: 8 additions & 0 deletions packages/core/tests/config/esm/rslib.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
};
8 changes: 8 additions & 0 deletions packages/core/tests/config/esm/rslib.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
};
10 changes: 10 additions & 0 deletions packages/core/tests/config/esm/rslib.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from '../../../../core/src/config';

export default defineConfig((args) => ({
lib: [],
source: {
entry: {
main: './foo/index.js',
},
},
}));
8 changes: 8 additions & 0 deletions packages/core/tests/config/esm/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
lib: [],
source: {
entry: {
main: './foo/index.ts',
},
},
};

0 comments on commit 2a60a86

Please sign in to comment.