Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Dec 24, 2024
1 parent b618bd0 commit 4d6ea80
Showing 1 changed file with 109 additions and 97 deletions.
206 changes: 109 additions & 97 deletions e2e/cases/config/inspect-config/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { createRsbuild, proxyConsole, rspackOnlyTest } from '@e2e/helper';
import { expect, test } from '@playwright/test';
import { expect } from '@playwright/test';

const rsbuildConfig = path.resolve(
__dirname,
Expand All @@ -21,114 +21,126 @@ const bundlerNodeConfig = path.resolve(
`./dist/.rsbuild/${process.env.PROVIDE_TYPE || 'rspack'}.config.node.mjs`,
);

test('should generate config files when writeToDisk is true', async () => {
const { logs, restore } = proxyConsole();

const rsbuild = await createRsbuild({
cwd: __dirname,
});
await rsbuild.inspectConfig({
writeToDisk: true,
});

expect(fs.existsSync(bundlerConfig)).toBeTruthy();
expect(fs.existsSync(rsbuildConfig)).toBeTruthy();

expect(
logs.some((log) => log.includes('Inspect config succeed')),
).toBeTruthy();

fs.rmSync(rsbuildConfig, { force: true });
fs.rmSync(bundlerConfig, { force: true });

restore();
});

test('should generate config files correctly when output is specified', async () => {
const { logs, restore } = proxyConsole();

const rsbuild = await createRsbuild({
cwd: __dirname,
});
await rsbuild.inspectConfig({
writeToDisk: true,
outputPath: 'foo',
});

const bundlerConfig = path.resolve(
__dirname,
`./dist/foo/${process.env.PROVIDE_TYPE || 'rspack'}.config.web.mjs`,
);
rspackOnlyTest(
'should generate config files when writeToDisk is true',
async () => {
const { logs, restore } = proxyConsole();

const rsbuildConfig = path.resolve(
__dirname,
'./dist/foo/rsbuild.config.mjs',
);
const rsbuild = await createRsbuild({
cwd: __dirname,
});
await rsbuild.inspectConfig({
writeToDisk: true,
});

expect(fs.existsSync(bundlerConfig)).toBeTruthy();
expect(fs.existsSync(rsbuildConfig)).toBeTruthy();
expect(fs.existsSync(bundlerConfig)).toBeTruthy();
expect(fs.existsSync(rsbuildConfig)).toBeTruthy();

expect(
logs.some((log) => log.includes('Inspect config succeed')),
).toBeTruthy();
expect(
logs.some((log) => log.includes('Inspect config succeed')),
).toBeTruthy();

fs.rmSync(rsbuildConfig, { force: true });
fs.rmSync(bundlerConfig, { force: true });
fs.rmSync(rsbuildConfig, { force: true });
fs.rmSync(bundlerConfig, { force: true });

restore();
});
restore();
},
);

test('should generate bundler config for node when target contains node', async () => {
const { logs, restore } = proxyConsole();
rspackOnlyTest(
'should generate config files correctly when output is specified',
async () => {
const { logs, restore } = proxyConsole();

const rsbuild = await createRsbuild({
cwd: __dirname,
});
await rsbuild.inspectConfig({
writeToDisk: true,
outputPath: 'foo',
});

const bundlerConfig = path.resolve(
__dirname,
`./dist/foo/${process.env.PROVIDE_TYPE || 'rspack'}.config.web.mjs`,
);

const rsbuildConfig = path.resolve(
__dirname,
'./dist/foo/rsbuild.config.mjs',
);

expect(fs.existsSync(bundlerConfig)).toBeTruthy();
expect(fs.existsSync(rsbuildConfig)).toBeTruthy();

expect(
logs.some((log) => log.includes('Inspect config succeed')),
).toBeTruthy();

fs.rmSync(rsbuildConfig, { force: true });
fs.rmSync(bundlerConfig, { force: true });

restore();
},
);

const rsbuild = await createRsbuild({
cwd: __dirname,
rsbuildConfig: {
environments: {
web: {
output: {
target: 'web',
rspackOnlyTest(
'should generate bundler config for node when target contains node',
async () => {
const { logs, restore } = proxyConsole();

const rsbuild = await createRsbuild({
cwd: __dirname,
rsbuildConfig: {
environments: {
web: {
output: {
target: 'web',
},
},
},
node: {
output: {
target: 'node',
node: {
output: {
target: 'node',
},
},
},
},
},
});
await rsbuild.inspectConfig({
writeToDisk: true,
});

expect(fs.existsSync(rsbuildNodeConfig)).toBeTruthy();
expect(fs.existsSync(bundlerConfig)).toBeTruthy();
expect(fs.existsSync(bundlerNodeConfig)).toBeTruthy();

expect(
logs.some((log) => log.includes('Inspect config succeed')),
).toBeTruthy();

fs.rmSync(rsbuildConfig, { force: true });
fs.rmSync(rsbuildNodeConfig, { force: true });
fs.rmSync(bundlerConfig, { force: true });
fs.rmSync(bundlerNodeConfig, { force: true });

restore();
});

test('should not generate config files when writeToDisk is false', async () => {
const rsbuild = await createRsbuild({
cwd: __dirname,
});
await rsbuild.inspectConfig({
writeToDisk: false,
});
});
await rsbuild.inspectConfig({
writeToDisk: true,
});

expect(fs.existsSync(rsbuildNodeConfig)).toBeTruthy();
expect(fs.existsSync(bundlerConfig)).toBeTruthy();
expect(fs.existsSync(bundlerNodeConfig)).toBeTruthy();

expect(
logs.some((log) => log.includes('Inspect config succeed')),
).toBeTruthy();

fs.rmSync(rsbuildConfig, { force: true });
fs.rmSync(rsbuildNodeConfig, { force: true });
fs.rmSync(bundlerConfig, { force: true });
fs.rmSync(bundlerNodeConfig, { force: true });

restore();
},
);

expect(fs.existsSync(rsbuildConfig)).toBeFalsy();
expect(fs.existsSync(bundlerConfig)).toBeFalsy();
});
rspackOnlyTest(
'should not generate config files when writeToDisk is false',
async () => {
const rsbuild = await createRsbuild({
cwd: __dirname,
});
await rsbuild.inspectConfig({
writeToDisk: false,
});

expect(fs.existsSync(rsbuildConfig)).toBeFalsy();
expect(fs.existsSync(bundlerConfig)).toBeFalsy();
},
);

rspackOnlyTest('should allow to specify absolute output path', async () => {
const { logs, restore } = proxyConsole();
Expand Down

0 comments on commit 4d6ea80

Please sign in to comment.