Skip to content

Commit 59000c8

Browse files
committed
test: add new CLI test fixture
1 parent b1c60d1 commit 59000c8

6 files changed

Lines changed: 40 additions & 16 deletions

File tree

packages/rstack/test/cli/specify-config/index.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { rm } from 'node:fs/promises';
22
import path from 'node:path';
3-
import { expect, test } from 'rstack/test';
4-
import { execCli, getDistFiles, getFileContent } from '#test-helpers';
3+
import { getDistFiles, getFileContent, test } from '#test-helpers';
54

6-
const cwd = import.meta.dirname;
7-
8-
test('should build with rstack --config', async () => {
5+
test('should build with rstack --config', async ({ cwd, execCli, expect }) => {
96
await rm(path.join(cwd, 'dist'), { recursive: true, force: true });
107

11-
execCli(['--config', './custom.config.ts', 'build'], { cwd });
8+
execCli(['--config', './custom.config.ts', 'build']);
129

1310
const files = await getDistFiles(path.join(cwd, 'dist'));
1411
const output = getFileContent(files, 'static/js/index.js');

packages/rstack/test/config/define-app/index.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { rm } from 'node:fs/promises';
22
import path from 'node:path';
3-
import { expect, test } from 'rstack/test';
4-
import { execCli, getDistFiles, getFileContent } from '#test-helpers';
3+
import { getDistFiles, getFileContent, test } from '#test-helpers';
54

6-
const cwd = import.meta.dirname;
75
const expectedText = 'define.app works';
86

9-
test('should build app with define.app config', async () => {
7+
test('should build app with define.app config', async ({ cwd, execCli, expect }) => {
108
const distPath = path.join(cwd, 'dist');
119
await rm(distPath, { recursive: true, force: true });
1210

1311
try {
14-
execCli(['build'], { cwd });
12+
execCli(['build']);
1513

1614
const files = await getDistFiles(distPath);
1715
const output = getFileContent(files, 'static/js/index.js');

packages/rstack/test/config/define-lib/index.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { rm } from 'node:fs/promises';
22
import path from 'node:path';
3-
import { expect, test } from 'rstack/test';
4-
import { execCli, getDistFiles, getFileContent } from '#test-helpers';
3+
import { getDistFiles, getFileContent, test } from '#test-helpers';
54

6-
const cwd = import.meta.dirname;
75
const expectedText = 'define.lib works';
86

9-
test('should build lib with define.lib config', async () => {
7+
test('should build lib with define.lib config', async ({ cwd, execCli, expect }) => {
108
const distPath = path.join(cwd, 'dist');
119
await rm(distPath, { recursive: true, force: true });
1210

13-
execCli(['lib'], { cwd });
11+
execCli(['lib']);
1412

1513
const files = await getDistFiles(distPath);
1614
const output = getFileContent(files, 'index.js');

packages/rstack/test/helpers/cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import path from 'node:path';
33

44
const RSTACK_BIN_PATH = path.join(import.meta.dirname, '../../bin/rs.js');
55

6+
export type ExecCli = (args: string[], options?: ExecFileSyncOptions) => string;
7+
68
export const execCli = (args: string[], options: ExecFileSyncOptions = {}): string => {
79
const output = execFileSync(process.execPath, [RSTACK_BIN_PATH, ...args], {
810
...options,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import path from 'node:path';
2+
import { test as baseTest } from 'rstack/test';
3+
import { execCli as baseExecCli, type ExecCli } from './cli.ts';
4+
5+
export type CliTestFixtures = {
6+
cwd: string;
7+
execCli: ExecCli;
8+
};
9+
10+
type CliTest = ReturnType<typeof baseTest.extend<CliTestFixtures>>;
11+
12+
export const test: CliTest = baseTest.extend<CliTestFixtures>({
13+
cwd: async ({ expect }, use) => {
14+
const { testPath } = expect.getState();
15+
16+
if (!testPath) {
17+
throw new Error('Unable to resolve current test file path from expect state.');
18+
}
19+
20+
await use(path.dirname(testPath));
21+
},
22+
execCli: async ({ cwd }, use) => {
23+
const execCli: ExecCli = (args, options = {}) =>
24+
baseExecCli(args, { ...options, cwd: options.cwd ?? cwd });
25+
26+
await use(execCli);
27+
},
28+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './cli.ts';
2+
export * from './cliTest.ts';
23
export * from './file.ts';

0 commit comments

Comments
 (0)