Skip to content

Commit 747be53

Browse files
authored
test: give each Rspack test case its own fixtures (#17)
1 parent c5dd02c commit 747be53

15 files changed

Lines changed: 232 additions & 160 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ test-results
1313
!.vscode/settings.json
1414
!.vscode/extensions.json
1515
.idea
16-
playground/test-temp-*/
16+
test-temp-*/

‎test/basic/index.test.ts‎

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

‎test/helpers.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { mkdtemp, rm } from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { promisify } from 'node:util';
4+
import { type Configuration, rspack } from '@rspack/core';
5+
import { expect, test as base } from '@rstest/playwright';
6+
7+
export const test = base.extend<{ tempDir: string }>({
8+
tempDir: async ({ task }, use) => {
9+
const dir = await mkdtemp(
10+
path.join(path.dirname(task.filepath!), 'test-temp-'),
11+
);
12+
try {
13+
await use(dir);
14+
} finally {
15+
await rm(dir, { recursive: true, force: true });
16+
}
17+
},
18+
});
19+
20+
export async function build(config: Configuration) {
21+
const stats = await promisify(rspack)(config);
22+
expect(stats?.toJson({ all: false, errors: true }).errors).toEqual([]);
23+
}

‎test/hmr/index.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { cp } from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { rspack } from '@rspack/core';
4+
import { RspackDevServer } from '@rspack/dev-server';
5+
import { editFile, getRandomPort } from '@rstackjs/test-utils';
6+
import { expect } from '@rstest/playwright';
7+
import { test } from '../helpers';
8+
import createConfig from './rspack.config';
9+
10+
for (const refresh of ['automatic', 'explicit Babel', 'pre-loader'] as const) {
11+
test(`preserves parent state during HMR with ${refresh} refresh`, async ({
12+
page,
13+
tempDir,
14+
}) => {
15+
const src = path.join(tempDir, 'src');
16+
await cp(path.join(import.meta.dirname, 'src'), src, { recursive: true });
17+
const port = await getRandomPort();
18+
const compiler = rspack({ ...createConfig({ refresh }), context: tempDir });
19+
const server = new RspackDevServer(
20+
{ host: '127.0.0.1', port, hot: true },
21+
compiler,
22+
);
23+
try {
24+
await server.start();
25+
await page.goto(`http://127.0.0.1:${port}`);
26+
const button = page.getByRole('button');
27+
await button.click();
28+
await expect(button).toHaveText('Count: 1');
29+
await editFile(path.join(src, 'Counter.tsx'), (code) =>
30+
code.replace('Count:', 'Updated:'),
31+
);
32+
await expect(button).toHaveText('Updated: 1');
33+
} finally {
34+
await server.stop();
35+
}
36+
});
37+
}

‎test/hmr/rspack.config.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {
2+
type Configuration,
3+
HtmlRspackPlugin,
4+
type RuleSetRule,
5+
} from '@rspack/core';
6+
7+
type Refresh = 'automatic' | 'explicit Babel' | 'pre-loader';
8+
9+
export default ({
10+
refresh = 'automatic',
11+
}: { refresh?: Refresh } = {}): Configuration => {
12+
const test = /\.[mc]?[jt]sx$/i;
13+
const rules: RuleSetRule[] = [];
14+
if (refresh === 'pre-loader') {
15+
rules.push({ test, enforce: 'pre', loader: 'solid-loader/refresh' });
16+
}
17+
18+
rules.push(
19+
{
20+
test,
21+
use: [
22+
{ loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } },
23+
refresh === 'automatic'
24+
? 'solid-loader'
25+
: {
26+
loader: 'solid-loader',
27+
options: {
28+
compiler: 'babel',
29+
refresh: refresh === 'explicit Babel',
30+
},
31+
},
32+
],
33+
},
34+
{
35+
descriptionData: { name: /^@solidjs\/web$/ },
36+
parser: { importDynamic: false },
37+
},
38+
);
39+
40+
return {
41+
context: import.meta.dirname,
42+
mode: 'development',
43+
entry: './src/index.tsx',
44+
resolve: {
45+
extensions: ['.tsx', '...'],
46+
conditionNames: ['solid', 'development', '...'],
47+
},
48+
module: { rules },
49+
plugins: [new HtmlRspackPlugin()],
50+
};
51+
};

‎test/hmr/src/Counter.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function Counter(props: { count: number; increment: () => void }) {
2+
return <button onClick={props.increment}>Count: {props.count}</button>;
3+
}

‎test/hmr/src/index.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { render } from '@solidjs/web';
2+
import { createSignal } from 'solid-js';
3+
import { Counter } from './Counter';
4+
5+
function App() {
6+
const [count, setCount] = createSignal<number>(0);
7+
return <Counter count={count()} increment={() => setCount(count() + 1)} />;
8+
}
9+
10+
render(() => <App />, document.body);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { build, test } from '../helpers';
2+
import config from './rspack.config';
3+
4+
test('builds a TSX component that imports a plain TypeScript module', async ({
5+
tempDir,
6+
}) => {
7+
await build({ ...config, output: { path: tempDir } });
8+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Configuration } from '@rspack/core';
2+
3+
export default {
4+
context: import.meta.dirname,
5+
mode: 'production',
6+
entry: './src/index.tsx',
7+
resolve: { conditionNames: ['solid', '...'] },
8+
module: {
9+
rules: [
10+
{
11+
test: /\.[mc]?[jt]sx?$/i,
12+
use: [
13+
{ loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } },
14+
'solid-loader',
15+
],
16+
},
17+
{
18+
descriptionData: { name: /^@solidjs\/web$/ },
19+
parser: { importDynamic: false },
20+
},
21+
],
22+
},
23+
} satisfies Configuration;

‎test/plain-typescript/src/count.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const count: number = 42;

0 commit comments

Comments
 (0)