From fe529723e1bf9fc212dc0298d07d3a3dc780e803 Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 19 Nov 2026 19:06:42 +0800 Subject: [PATCH 1/2] test: give each Rspack test case its own fixtures --- .gitignore | 2 +- test/basic/index.test.ts | 159 ------------------------- test/helpers.ts | 23 ++++ test/hmr/index.test.ts | 37 ++++++ test/hmr/rspack.config.ts | 51 ++++++++ test/hmr/src/Counter.tsx | 3 + test/hmr/src/index.tsx | 10 ++ test/plain-typescript/index.test.ts | 8 ++ test/plain-typescript/rspack.config.ts | 23 ++++ test/plain-typescript/src/count.ts | 1 + test/plain-typescript/src/index.tsx | 4 + test/ssr/index.test.ts | 22 ++++ test/ssr/rspack.config.ts | 31 +++++ test/ssr/src/app.tsx | 4 + 14 files changed, 218 insertions(+), 160 deletions(-) delete mode 100644 test/basic/index.test.ts create mode 100644 test/helpers.ts create mode 100644 test/hmr/index.test.ts create mode 100644 test/hmr/rspack.config.ts create mode 100644 test/hmr/src/Counter.tsx create mode 100644 test/hmr/src/index.tsx create mode 100644 test/plain-typescript/index.test.ts create mode 100644 test/plain-typescript/rspack.config.ts create mode 100644 test/plain-typescript/src/count.ts create mode 100644 test/plain-typescript/src/index.tsx create mode 100644 test/ssr/index.test.ts create mode 100644 test/ssr/rspack.config.ts create mode 100644 test/ssr/src/app.tsx diff --git a/.gitignore b/.gitignore index a312e7f..cc25b8f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ test-results !.vscode/settings.json !.vscode/extensions.json .idea -playground/test-temp-*/ +test-temp-*/ diff --git a/test/basic/index.test.ts b/test/basic/index.test.ts deleted file mode 100644 index 0a43795..0000000 --- a/test/basic/index.test.ts +++ /dev/null @@ -1,159 +0,0 @@ -import { cp, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; -import { createRequire } from 'node:module'; -import path from 'node:path'; -import { promisify } from 'node:util'; -import { type RuleSetRule, rspack } from '@rspack/core'; -import { RspackDevServer } from '@rspack/dev-server'; -import { expect, test } from '@rstest/playwright'; -import { getRandomPort } from '@rstackjs/test-utils'; -import playgroundConfig from '../../playground/rspack.config.ts'; - -const playground = path.resolve(import.meta.dirname, '../../playground'); - -test('builds a TSX component that imports a plain TypeScript module', async () => { - const cwd = await mkdtemp(path.join(playground, 'test-temp-')); - try { - await writeFile( - path.join(cwd, 'count.ts'), - 'export const count: number = 42;', - ); - await writeFile( - path.join(cwd, 'app.tsx'), - `import { render } from '@solidjs/web'; - import { count } from './count.ts'; - render(() =>

{count}

, document.body);`, - ); - const stats = await promisify(rspack)({ - ...playgroundConfig({}, { mode: 'production' }), - context: cwd, - entry: './app.tsx', - output: { path: path.join(cwd, 'dist') }, - }); - expect(stats.toJson({ all: false, errors: true }).errors).toEqual([]); - } finally { - await rm(cwd, { recursive: true, force: true }); - } -}); - -for (const compiler of ['native', 'babel']) { - test(`renders typed SSR output with ${compiler}`, async () => { - const cwd = await mkdtemp(path.join(playground, 'test-temp-')); - try { - await writeFile( - path.join(cwd, 'app.tsx'), - ` - import { renderToString } from '@solidjs/web'; - const title: string = 'Hello'; - export const html = renderToString(() =>

{title}

); - `, - ); - await new Promise((resolve, reject) => { - rspack( - { - context: cwd, - mode: 'production', - target: 'node', - entry: './app.tsx', - devtool: 'source-map', - output: { - path: cwd, - filename: 'app.cjs', - library: { type: 'commonjs2' }, - }, - resolve: { conditionNames: ['solid', '...'] }, - module: { - rules: [ - { - test: /\.tsx$/, - use: [ - { - loader: 'builtin:swc-loader', - options: { detectSyntax: 'auto' }, - }, - { - loader: 'solid-loader', - options: { compiler, solid: { generate: 'ssr' } }, - }, - ], - }, - ], - }, - }, - (err, stats) => { - if (err) reject(err); - else if (stats?.hasErrors()) - reject(new Error(stats.toString({ all: false, errors: true }))); - else resolve(); - }, - ); - }); - const { html } = createRequire(import.meta.url)( - path.join(cwd, 'app.cjs'), - ); - expect(html).toBe('

Hello

'); - const map = JSON.parse( - await readFile(path.join(cwd, 'app.cjs.map'), 'utf8'), - ); - expect(map.sources).toContainEqual(expect.stringMatching(/app\.tsx$/)); - } finally { - await rm(cwd, { recursive: true, force: true }); - } - }); -} - -for (const mode of [ - 'automatic refresh', - 'explicit Babel refresh', - 'pre-loader', -]) { - test(`preserves parent state during HMR with ${mode}`, async ({ page }) => { - const cwd = await mkdtemp(path.join(playground, 'test-temp-')); - try { - await cp(path.join(playground, 'src'), path.join(cwd, 'src'), { - recursive: true, - }); - const port = await getRandomPort(); - const config = playgroundConfig({}, { mode: 'development' }); - config.context = cwd; - if (mode !== 'automatic refresh') { - const rules = config.module!.rules!; - const jsxRule = rules[0] as RuleSetRule; - const standalone = mode === 'pre-loader'; - jsxRule.use = [ - { loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } }, - { - loader: 'solid-loader', - options: { compiler: 'babel', refresh: !standalone }, - }, - ]; - if (standalone) { - rules.unshift({ - test: /\.[mc]?[jt]sx$/i, - enforce: 'pre', - loader: 'solid-loader/refresh', - }); - } - } - const compiler = rspack(config); - const server = new RspackDevServer( - { host: '127.0.0.1', port, hot: true }, - compiler, - ); - await server.start(); - try { - await page.goto(`http://127.0.0.1:${port}`); - const button = page.getByRole('button'); - await button.click(); - await expect(button).toHaveText('Count: 1'); - const file = path.join(cwd, 'src/Counter.tsx'); - const source = await readFile(file, 'utf8'); - await writeFile(file, source.replace('Count:', 'Updated:')); - await expect(button).toHaveText('Updated: 1'); - } finally { - await server.stop(); - } - } finally { - await rm(cwd, { recursive: true, force: true }); - } - }); -} diff --git a/test/helpers.ts b/test/helpers.ts new file mode 100644 index 0000000..845a9e6 --- /dev/null +++ b/test/helpers.ts @@ -0,0 +1,23 @@ +import { mkdtemp, rm } from 'node:fs/promises'; +import path from 'node:path'; +import { promisify } from 'node:util'; +import { type Configuration, rspack } from '@rspack/core'; +import { expect, test as base } from '@rstest/playwright'; + +export const test = base.extend<{ tempDir: string }>({ + tempDir: async ({ task }, use) => { + const dir = await mkdtemp( + path.join(path.dirname(task.filepath!), 'test-temp-'), + ); + try { + await use(dir); + } finally { + await rm(dir, { recursive: true, force: true }); + } + }, +}); + +export async function build(config: Configuration) { + const stats = await promisify(rspack)(config); + expect(stats?.toJson({ all: false, errors: true }).errors).toEqual([]); +} diff --git a/test/hmr/index.test.ts b/test/hmr/index.test.ts new file mode 100644 index 0000000..3898eeb --- /dev/null +++ b/test/hmr/index.test.ts @@ -0,0 +1,37 @@ +import { cp } from 'node:fs/promises'; +import path from 'node:path'; +import { rspack } from '@rspack/core'; +import { RspackDevServer } from '@rspack/dev-server'; +import { editFile, getRandomPort } from '@rstackjs/test-utils'; +import { expect } from '@rstest/playwright'; +import { test } from '../helpers'; +import createConfig from './rspack.config'; + +for (const refresh of ['automatic', 'explicit Babel', 'pre-loader'] as const) { + test(`preserves parent state during HMR with ${refresh} refresh`, async ({ + page, + tempDir, + }) => { + const src = path.join(tempDir, 'src'); + await cp(path.join(import.meta.dirname, 'src'), src, { recursive: true }); + const port = await getRandomPort(); + const compiler = rspack({ ...createConfig({ refresh }), context: tempDir }); + const server = new RspackDevServer( + { host: '127.0.0.1', port, hot: true }, + compiler, + ); + try { + await server.start(); + await page.goto(`http://127.0.0.1:${port}`); + const button = page.getByRole('button'); + await button.click(); + await expect(button).toHaveText('Count: 1'); + await editFile(path.join(src, 'Counter.tsx'), (code) => + code.replace('Count:', 'Updated:'), + ); + await expect(button).toHaveText('Updated: 1'); + } finally { + await server.stop(); + } + }); +} diff --git a/test/hmr/rspack.config.ts b/test/hmr/rspack.config.ts new file mode 100644 index 0000000..f65ebee --- /dev/null +++ b/test/hmr/rspack.config.ts @@ -0,0 +1,51 @@ +import { + type Configuration, + HtmlRspackPlugin, + type RuleSetRule, +} from '@rspack/core'; + +type Refresh = 'automatic' | 'explicit Babel' | 'pre-loader'; + +export default ({ + refresh = 'automatic', +}: { refresh?: Refresh } = {}): Configuration => { + const test = /\.[mc]?[jt]sx$/i; + const rules: RuleSetRule[] = []; + if (refresh === 'pre-loader') { + rules.push({ test, enforce: 'pre', loader: 'solid-loader/refresh' }); + } + + rules.push( + { + test, + use: [ + { loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } }, + refresh === 'automatic' + ? 'solid-loader' + : { + loader: 'solid-loader', + options: { + compiler: 'babel', + refresh: refresh === 'explicit Babel', + }, + }, + ], + }, + { + descriptionData: { name: /^@solidjs\/web$/ }, + parser: { importDynamic: false }, + }, + ); + + return { + context: import.meta.dirname, + mode: 'development', + entry: './src/index.tsx', + resolve: { + extensions: ['.tsx', '...'], + conditionNames: ['solid', 'development', '...'], + }, + module: { rules }, + plugins: [new HtmlRspackPlugin()], + }; +}; diff --git a/test/hmr/src/Counter.tsx b/test/hmr/src/Counter.tsx new file mode 100644 index 0000000..eca04b0 --- /dev/null +++ b/test/hmr/src/Counter.tsx @@ -0,0 +1,3 @@ +export function Counter(props: { count: number; increment: () => void }) { + return ; +} diff --git a/test/hmr/src/index.tsx b/test/hmr/src/index.tsx new file mode 100644 index 0000000..1b710c9 --- /dev/null +++ b/test/hmr/src/index.tsx @@ -0,0 +1,10 @@ +import { render } from '@solidjs/web'; +import { createSignal } from 'solid-js'; +import { Counter } from './Counter'; + +function App() { + const [count, setCount] = createSignal(0); + return setCount(count() + 1)} />; +} + +render(() => , document.body); diff --git a/test/plain-typescript/index.test.ts b/test/plain-typescript/index.test.ts new file mode 100644 index 0000000..8464ac1 --- /dev/null +++ b/test/plain-typescript/index.test.ts @@ -0,0 +1,8 @@ +import { build, test } from '../helpers'; +import config from './rspack.config'; + +test('builds a TSX component that imports a plain TypeScript module', async ({ + tempDir, +}) => { + await build({ ...config, output: { path: tempDir } }); +}); diff --git a/test/plain-typescript/rspack.config.ts b/test/plain-typescript/rspack.config.ts new file mode 100644 index 0000000..9d518ff --- /dev/null +++ b/test/plain-typescript/rspack.config.ts @@ -0,0 +1,23 @@ +import type { Configuration } from '@rspack/core'; + +export default { + context: import.meta.dirname, + mode: 'production', + entry: './src/index.tsx', + resolve: { conditionNames: ['solid', '...'] }, + module: { + rules: [ + { + test: /\.[mc]?[jt]sx?$/i, + use: [ + { loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } }, + 'solid-loader', + ], + }, + { + descriptionData: { name: /^@solidjs\/web$/ }, + parser: { importDynamic: false }, + }, + ], + }, +} satisfies Configuration; diff --git a/test/plain-typescript/src/count.ts b/test/plain-typescript/src/count.ts new file mode 100644 index 0000000..5b342da --- /dev/null +++ b/test/plain-typescript/src/count.ts @@ -0,0 +1 @@ +export const count: number = 42; diff --git a/test/plain-typescript/src/index.tsx b/test/plain-typescript/src/index.tsx new file mode 100644 index 0000000..9dce92a --- /dev/null +++ b/test/plain-typescript/src/index.tsx @@ -0,0 +1,4 @@ +import { render } from '@solidjs/web'; +import { count } from './count.ts'; + +render(() =>

{count}

, document.body); diff --git a/test/ssr/index.test.ts b/test/ssr/index.test.ts new file mode 100644 index 0000000..364e538 --- /dev/null +++ b/test/ssr/index.test.ts @@ -0,0 +1,22 @@ +import { readFile } from 'node:fs/promises'; +import { createRequire } from 'node:module'; +import path from 'node:path'; +import { expect } from '@rstest/playwright'; +import { build, test } from '../helpers'; +import createConfig from './rspack.config'; + +for (const compiler of ['native', 'babel'] as const) { + test(`renders typed SSR output with ${compiler}`, async ({ tempDir }) => { + const config = createConfig({ compiler }); + await build({ ...config, output: { ...config.output, path: tempDir } }); + + const { html } = createRequire(import.meta.url)( + path.join(tempDir, 'app.cjs'), + ); + expect(html).toBe('

Hello

'); + const map = JSON.parse( + await readFile(path.join(tempDir, 'app.cjs.map'), 'utf8'), + ); + expect(map.sources).toContainEqual(expect.stringMatching(/app\.tsx$/)); + }); +} diff --git a/test/ssr/rspack.config.ts b/test/ssr/rspack.config.ts new file mode 100644 index 0000000..58b5447 --- /dev/null +++ b/test/ssr/rspack.config.ts @@ -0,0 +1,31 @@ +import type { Configuration } from '@rspack/core'; +import type { SolidCompiler } from 'solid-loader'; + +export default ({ + compiler = 'native', +}: { compiler?: SolidCompiler } = {}): Configuration => ({ + context: import.meta.dirname, + mode: 'production', + target: 'node', + entry: './src/app.tsx', + devtool: 'source-map', + output: { + filename: 'app.cjs', + library: { type: 'commonjs2' }, + }, + resolve: { conditionNames: ['solid', '...'] }, + module: { + rules: [ + { + test: /\.tsx$/, + use: [ + { loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } }, + { + loader: 'solid-loader', + options: { compiler, solid: { generate: 'ssr' } }, + }, + ], + }, + ], + }, +}); diff --git a/test/ssr/src/app.tsx b/test/ssr/src/app.tsx new file mode 100644 index 0000000..55d3031 --- /dev/null +++ b/test/ssr/src/app.tsx @@ -0,0 +1,4 @@ +import { renderToString } from '@solidjs/web'; + +const title: string = 'Hello'; +export const html = renderToString(() =>

{title}

); From 9520ac5e8c2002c4f2c73f7639f8dd0b8d26193d Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 19 Nov 2026 19:11:25 +0800 Subject: [PATCH 2/2] test: configure TypeScript for test fixtures --- test/tsconfig.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 test/tsconfig.json diff --git a/test/tsconfig.json b/test/tsconfig.json new file mode 100644 index 0000000..7dfff5b --- /dev/null +++ b/test/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "ES2023", + "module": "preserve", + "types": ["node"], + "jsx": "preserve", + "jsxImportSource": "@solidjs/web", + "allowImportingTsExtensions": true, + "skipLibCheck": true, + "noEmit": true + }, + "include": ["**/*.ts", "**/*.tsx"], + "exclude": ["**/test-temp-*"] +}