Skip to content

Commit d5c5598

Browse files
committed
test: use shared test utilities
1 parent e586d17 commit d5c5598

15 files changed

Lines changed: 52 additions & 357 deletions

File tree

packages/rstack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
},
6464
"devDependencies": {
6565
"@rspress/core": "catalog:",
66+
"@rstackjs/test-utils": "catalog:",
6667
"@rstest/adapter-rsbuild": "catalog:",
6768
"@rstest/adapter-rslib": "catalog:",
6869
"@types/node": "catalog:",

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { rm } from 'node:fs/promises';
2-
import path from 'node:path';
3-
import { getDistFiles, getFileContent, test } from '#test-helpers';
1+
import { getDistFiles, getFileContent } from '@rstackjs/test-utils';
2+
import { test } from '#test-helpers';
43

5-
test('should build with rstack --config', async ({ cwd, execCli, expect }) => {
6-
await rm(path.join(cwd, 'dist'), { recursive: true, force: true });
4+
test('should build with rstack --config', async ({ prepareDist, execCli, expect }) => {
5+
const distPath = await prepareDist();
76

87
execCli('build --config ./custom.config.ts');
98

10-
const files = await getDistFiles(path.join(cwd, 'dist'));
9+
const files = await getDistFiles(distPath);
1110
const output = getFileContent(files, 'static/js/index.js');
1211

1312
expect(output).toContain('specify config works');

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { rm } from 'node:fs/promises';
2-
import path from 'node:path';
3-
import { getDistFiles, getFileContent, test } from '#test-helpers';
2+
import { getDistFiles, getFileContent } from '@rstackjs/test-utils';
3+
import { test } from '#test-helpers';
44

55
const expectedText = 'define.app works';
66

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

1110
try {
1211
execCli('build');

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { rm } from 'node:fs/promises';
2-
import path from 'node:path';
3-
import { getDistFiles, getFileContent, test } from '#test-helpers';
1+
import { getDistFiles, getFileContent } from '@rstackjs/test-utils';
2+
import { test } from '#test-helpers';
43

54
const expectedText = 'define.doc works';
65

7-
test('should build docs with define.doc config', async ({ cwd, execCli, expect }) => {
8-
const distPath = path.join(cwd, 'doc_build');
9-
await rm(distPath, { recursive: true, force: true });
6+
test('should build docs with define.doc config', async ({ prepareDist, execCli, expect }) => {
7+
const distPath = await prepareDist('doc_build');
108

119
execCli('doc build');
1210

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { rm } from 'node:fs/promises';
2-
import path from 'node:path';
3-
import { getDistFiles, getFileContent, test } from '#test-helpers';
1+
import { getDistFiles, getFileContent } from '@rstackjs/test-utils';
2+
import { test } from '#test-helpers';
43

54
const expectedText = 'define.lib works';
65

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

119
execCli('lib');
1210

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { rm, writeFile } from 'node:fs/promises';
22
import path from 'node:path';
3-
import { expectFile, getRandomPort, test } from '#test-helpers';
3+
import { getRandomPort, waitForFile } from '@rstackjs/test-utils';
4+
import { test } from '#test-helpers';
45

56
test('should restart dev server and reload config when Rstack config changes', async ({
7+
prepareDist,
68
execCliAsync,
79
}) => {
8-
const dist1 = path.join(import.meta.dirname, 'dist');
9-
const dist2 = path.join(import.meta.dirname, 'dist-2');
10+
const dist1 = await prepareDist();
11+
const dist2 = await prepareDist('dist-2');
1012
const configFile = path.join(import.meta.dirname, 'test-temp-rstack.config.ts');
1113

12-
await rm(dist1, { recursive: true, force: true });
13-
await rm(dist2, { recursive: true, force: true });
1414
await rm(configFile, { force: true });
1515

1616
await writeFile(
@@ -28,7 +28,7 @@ define.app({
2828

2929
execCliAsync('dev --config test-temp-rstack.config.ts');
3030

31-
await expectFile(dist1);
31+
await waitForFile(dist1, { interval: 50 });
3232

3333
await writeFile(
3434
configFile,
@@ -46,7 +46,7 @@ define.app({
4646
`,
4747
);
4848

49-
await expectFile(dist2);
49+
await waitForFile(dist2, { interval: 50 });
5050
}, 30_000);
5151

5252
test('should reload config when an imported file changes', async ({ execCliAsync, logHelper }) => {
@@ -66,7 +66,7 @@ define.app({
6666
);
6767

6868
execCliAsync('dev --config test-temp-import.config.ts');
69-
await logHelper.expectBuildEnd();
69+
await logHelper.expectLog('built in');
7070
logHelper.clearLogs();
7171

7272
await writeFile(importedFile, '// changed\n');

packages/rstack/test/helpers/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type ExecSyncOptions, execSync } from 'node:child_process';
22
import path from 'node:path';
3-
import type { LogHelper } from './logs.ts';
3+
import type { LogHelper } from '@rstackjs/test-utils';
44

55
export const RSTACK_BIN_PATH: string = path.join(import.meta.dirname, '../../bin/rs.js');
66

packages/rstack/test/helpers/cliTest.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { type ChildProcess, type SpawnOptions, spawn as nodeSpawn } from 'node:child_process';
22
import path from 'node:path';
3+
import {
4+
type ExtendedLogHelper,
5+
prepareDist as basePrepareDist,
6+
proxyConsole,
7+
} from '@rstackjs/test-utils';
38
import { test as baseTest } from 'rstack/test';
49
import { execCli as baseExecCli, type ExecCli, RSTACK_BIN_PATH } from './cli.ts';
5-
import { type ExtendedLogHelper, proxyConsole } from './logs.ts';
610

711
type Exec = (
812
command: string,
@@ -11,12 +15,15 @@ type Exec = (
1115
childProcess: ChildProcess;
1216
};
1317

18+
type PrepareDist = (distFolderName?: string) => Promise<string>;
19+
1420
export type CliTestFixtures = {
1521
cwd: string;
1622
exec: Exec;
1723
execCli: ExecCli;
1824
execCliAsync: Exec;
1925
logHelper: ExtendedLogHelper;
26+
prepareDist: PrepareDist;
2027
};
2128

2229
type CliTest = ReturnType<typeof baseTest.extend<CliTestFixtures>>;
@@ -49,6 +56,11 @@ export const test: CliTest = baseTest.extend<CliTestFixtures>({
4956

5057
await use(path.dirname(testPath));
5158
},
59+
prepareDist: async ({ cwd }, use) => {
60+
const prepareDist: PrepareDist = (distFolderName = 'dist') =>
61+
basePrepareDist(path.join(cwd, distFolderName));
62+
await use(prepareDist);
63+
},
5264
logHelper: [
5365
async ({ onTestFailed, task }, use) => {
5466
const logHelper = proxyConsole();

packages/rstack/test/helpers/constants.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/rstack/test/helpers/file.ts

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)