Skip to content

Commit 6d7a1db

Browse files
authored
test: add basic test for HMR (#23)
1 parent 4c02169 commit 6d7a1db

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.DS_Store
33
*.local
44
*.log*
5+
test-temp-*
56

67
# Dist
78
node_modules

test/react-hmr/index.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { promises } from 'node:fs';
2+
import { dirname, join } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
import { expect, test } from '@playwright/test';
5+
import { createRsbuild, loadConfig } from '@rsbuild/core';
6+
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
9+
const getMdxContent = (title: string) => `${title}
10+
11+
Below is an example of markdown in JSX.
12+
13+
<div id="test">
14+
<p style={{ padding: '1rem' }}>Test</p>
15+
</div>
16+
`;
17+
18+
test('should render MDX with React correctly', async ({ page }) => {
19+
const mdxPath = join(__dirname, 'src/test-temp-file.mdx');
20+
await promises.writeFile(mdxPath, getMdxContent('# Hello, world!'));
21+
22+
const rsbuild = await createRsbuild({
23+
cwd: __dirname,
24+
rsbuildConfig: (await loadConfig({ cwd: __dirname })).content,
25+
});
26+
27+
const { server, urls } = await rsbuild.startDevServer();
28+
await page.goto(urls[0]);
29+
expect(await page.innerHTML('h1')).toEqual('Hello, world!');
30+
expect(await page.innerHTML('#test')).toEqual(
31+
'<p style="padding: 1rem;">Test</p>',
32+
);
33+
34+
await promises.writeFile(mdxPath, getMdxContent('## Hello, world 2!'));
35+
expect(await page.innerHTML('h2')).toEqual('Hello, world 2!');
36+
37+
await server.close();
38+
});

test/react-hmr/rsbuild.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from '@rsbuild/core';
2+
import { pluginMdx } from '@rsbuild/plugin-mdx';
3+
import { pluginReact } from '@rsbuild/plugin-react';
4+
import { getRandomPort } from '../helper';
5+
6+
export default defineConfig({
7+
plugins: [pluginReact(), pluginMdx()],
8+
server: {
9+
port: getRandomPort(),
10+
},
11+
});

test/react-hmr/src/index.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import ReactDOM from 'react-dom/client';
2+
import App from './test-temp-file';
3+
4+
const root = ReactDOM.createRoot(document.getElementById('root'));
5+
root.render(<App />);

0 commit comments

Comments
 (0)