Skip to content

Commit 0c0851c

Browse files
committed
test: extract temp dir generator to helper module
1 parent a20c93f commit 0c0851c

File tree

5 files changed

+22
-36
lines changed

5 files changed

+22
-36
lines changed

test/basic/index.test.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@ import { pluginLess } from '@rsbuild/plugin-less';
77
import { pluginSass } from '@rsbuild/plugin-sass';
88
import { pluginStylus } from '@rsbuild/plugin-stylus';
99
import { pluginTypedCSSModules } from '../../dist';
10+
import { generatorTempDir } from '../helper';
1011

1112
const __dirname = dirname(fileURLToPath(import.meta.url));
1213

13-
const generatorTempDir = async (testDir: string) => {
14-
fs.rmSync(testDir, { recursive: true, force: true });
15-
await fs.promises.cp(join(__dirname, 'src'), testDir, { recursive: true });
16-
17-
return () => fs.promises.rm(testDir, { force: true, recursive: true });
18-
};
19-
2014
test('generator TS declaration for cssModules.auto true', async () => {
2115
const testDir = join(__dirname, 'test-temp-src-1');
22-
const clear = await generatorTempDir(testDir);
16+
const clear = await generatorTempDir(__dirname, testDir);
2317

2418
const rsbuild = await createRsbuild({
2519
cwd: __dirname,
@@ -82,7 +76,7 @@ export default cssExports;
8276

8377
test('generator TS declaration for cssModules.auto function', async () => {
8478
const testDir = join(__dirname, 'test-temp-src-2');
85-
const clear = await generatorTempDir(testDir);
79+
const clear = await generatorTempDir(__dirname, testDir);
8680

8781
const rsbuild = await createRsbuild({
8882
cwd: __dirname,
@@ -118,7 +112,7 @@ test('generator TS declaration for cssModules.auto function', async () => {
118112

119113
test('generator TS declaration for cssModules.auto Regexp', async () => {
120114
const testDir = join(__dirname, 'test-temp-src-3');
121-
const clear = await generatorTempDir(testDir);
115+
const clear = await generatorTempDir(__dirname, testDir);
122116

123117
const rsbuild = await createRsbuild({
124118
cwd: __dirname,
@@ -152,7 +146,7 @@ test('generator TS declaration for cssModules.auto Regexp', async () => {
152146

153147
test('generator TS declaration for `asIs` convention', async () => {
154148
const testDir = join(__dirname, 'test-temp-src-4');
155-
const clear = await generatorTempDir(testDir);
149+
const clear = await generatorTempDir(__dirname, testDir);
156150

157151
const rsbuild = await createRsbuild({
158152
cwd: __dirname,

test/helper.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import fs from 'node:fs';
2+
import { join } from 'node:path';
3+
14
const portMap = new Map();
25

36
export function getRandomPort(
@@ -12,3 +15,10 @@ export function getRandomPort(
1215
port++;
1316
}
1417
}
18+
19+
export const generatorTempDir = async (cwd: string, testDir: string) => {
20+
fs.rmSync(testDir, { recursive: true, force: true });
21+
await fs.promises.cp(join(cwd, 'src'), testDir, { recursive: true });
22+
23+
return () => fs.promises.rm(testDir, { force: true, recursive: true });
24+
};

test/loose-typing/index.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ import { expect, test } from '@playwright/test';
55
import { createRsbuild } from '@rsbuild/core';
66
import { pluginSass } from '@rsbuild/plugin-sass';
77
import { pluginTypedCSSModules } from '../../dist';
8+
import { generatorTempDir } from '../helper';
89

910
const __dirname = dirname(fileURLToPath(import.meta.url));
1011
const fixtures = __dirname;
1112

12-
const generatorTempDir = async (testDir: string) => {
13-
fs.rmSync(testDir, { recursive: true, force: true });
14-
await fs.promises.cp(join(fixtures, 'src'), testDir, { recursive: true });
15-
16-
return () => fs.promises.rm(testDir, { force: true, recursive: true });
17-
};
18-
1913
test('generator TS declaration with loose typing', async () => {
2014
const testDir = join(fixtures, 'test-temp-src-1');
21-
const clear = await generatorTempDir(testDir);
15+
const clear = await generatorTempDir(__dirname, testDir);
2216

2317
const rsbuild = await createRsbuild({
2418
cwd: __dirname,

test/multiple-rules/index.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@ import { createRsbuild } from '@rsbuild/core';
66
import { pluginLess } from '@rsbuild/plugin-less';
77
import { pluginSass } from '@rsbuild/plugin-sass';
88
import { pluginTypedCSSModules } from '../../dist';
9+
import { generatorTempDir } from '../helper';
910

1011
const __dirname = dirname(fileURLToPath(import.meta.url));
1112

12-
const generatorTempDir = async (testDir: string) => {
13-
fs.rmSync(testDir, { recursive: true, force: true });
14-
await fs.promises.cp(join(__dirname, 'src'), testDir, { recursive: true });
15-
16-
return () => fs.promises.rm(testDir, { force: true, recursive: true });
17-
};
18-
1913
test('generator TS declaration for multiple Less or Sass plugins', async () => {
2014
const testDir = join(__dirname, 'test-temp-src-1');
21-
const clear = await generatorTempDir(testDir);
15+
const clear = await generatorTempDir(__dirname, testDir);
2216

2317
const rsbuild = await createRsbuild({
2418
cwd: __dirname,

test/named-export/index.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@ import { createRsbuild } from '@rsbuild/core';
66
import { pluginLess } from '@rsbuild/plugin-less';
77
import { pluginSass } from '@rsbuild/plugin-sass';
88
import { pluginTypedCSSModules } from '../../dist';
9+
import { generatorTempDir } from '../helper';
910

1011
const __dirname = dirname(fileURLToPath(import.meta.url));
1112
const fixtures = __dirname;
1213

13-
const generatorTempDir = async (testDir: string) => {
14-
fs.rmSync(testDir, { recursive: true, force: true });
15-
await fs.promises.cp(join(fixtures, 'src'), testDir, { recursive: true });
16-
17-
return () => fs.promises.rm(testDir, { force: true, recursive: true });
18-
};
19-
2014
test('generator TS declaration for namedExport true', async () => {
2115
const testDir = join(fixtures, 'test-temp-src-1');
22-
const clear = await generatorTempDir(testDir);
16+
const clear = await generatorTempDir(__dirname, testDir);
2317

2418
const rsbuild = await createRsbuild({
2519
cwd: __dirname,
@@ -71,7 +65,7 @@ export const theCClass: string;
7165

7266
test('generator TS declaration for namedExport true and `asIs` convention', async () => {
7367
const testDir = join(fixtures, 'test-temp-src-4');
74-
const clear = await generatorTempDir(testDir);
68+
const clear = await generatorTempDir(__dirname, testDir);
7569

7670
const rsbuild = await createRsbuild({
7771
cwd: __dirname,

0 commit comments

Comments
 (0)