Skip to content

Commit 19de295

Browse files
authored
feat: support jsdom test (#284)
1 parent 04d93d7 commit 19de295

31 files changed

+1396
-37
lines changed

examples/react/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "@examples/react",
3+
"private": true,
4+
"scripts": {
5+
"dev": "rsbuild dev --open",
6+
"build": "rsbuild build",
7+
"preview": "rsbuild preview"
8+
},
9+
"dependencies": {
10+
"react": "^19.1.0",
11+
"react-dom": "^19.1.0"
12+
},
13+
"devDependencies": {
14+
"@rsbuild/core": "^1.4.0-beta.3",
15+
"@rsbuild/plugin-react": "^1.3.2",
16+
"@testing-library/dom": "^10.4.0",
17+
"@testing-library/react": "^16.3.0",
18+
"@types/react": "^19.1.8",
19+
"@types/react-dom": "^19.1.6",
20+
"jsdom": "^26.1.0",
21+
"typescript": "^5.8.3"
22+
}
23+
}

examples/react/rsbuild.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from '@rsbuild/core';
2+
import { pluginReact } from '@rsbuild/plugin-react';
3+
4+
export default defineConfig({
5+
plugins: [pluginReact()],
6+
});

examples/react/rstest.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from '@rstest/core';
2+
import rsbuildConfig from './rsbuild.config';
3+
4+
export default defineConfig({
5+
...rsbuildConfig,
6+
testEnvironment: 'jsdom',
7+
});

examples/react/src/App.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
body {
2+
margin: 0;
3+
color: #fff;
4+
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
5+
background-image: linear-gradient(to bottom, #020917, #101725);
6+
}
7+
8+
.content {
9+
display: flex;
10+
min-height: 100vh;
11+
line-height: 1.1;
12+
text-align: center;
13+
flex-direction: column;
14+
justify-content: center;
15+
}
16+
17+
.content h1 {
18+
font-size: 3.6rem;
19+
font-weight: 700;
20+
}
21+
22+
.content p {
23+
font-size: 1.2rem;
24+
font-weight: 400;
25+
opacity: 0.5;
26+
}

examples/react/src/App.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import './App.css';
2+
3+
const App = () => {
4+
return (
5+
<div className="content">
6+
<h1>Rsbuild with React</h1>
7+
<p>Start building amazing things with Rsbuild.</p>
8+
</div>
9+
);
10+
};
11+
12+
export default App;

examples/react/src/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="@rsbuild/core/types" />

examples/react/src/index.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom/client';
3+
import App from './App';
4+
5+
const rootEl = document.getElementById('root');
6+
if (rootEl) {
7+
const root = ReactDOM.createRoot(rootEl);
8+
root.render(
9+
<React.StrictMode>
10+
<App />
11+
</React.StrictMode>,
12+
);
13+
}

examples/react/test/App.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from '@rstest/core';
2+
import { render, screen } from '@testing-library/react';
3+
import App from '../src/App';
4+
5+
test('should render App correctly', async () => {
6+
render(<App />);
7+
8+
const element = screen.getByText('Rsbuild with React');
9+
10+
expect(element.tagName).toBe('H1');
11+
});

examples/react/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"lib": ["DOM", "ES2020"],
5+
"module": "ESNext",
6+
"jsx": "react-jsx",
7+
"strict": true,
8+
"skipLibCheck": true,
9+
"isolatedModules": true,
10+
"resolveJsonModule": true,
11+
"moduleResolution": "bundler",
12+
"useDefineForClassFields": true
13+
},
14+
"include": ["src"]
15+
}

packages/core/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
"@rslib/core": "0.9.2",
7171
"@rstest/tsconfig": "workspace:*",
7272
"@types/babel__code-frame": "^7.0.6",
73+
"@types/jsdom": "^21.1.7",
74+
"jsdom": "^26.1.0",
7375
"@types/source-map-support": "^0.5.10",
7476
"cac": "^6.7.14",
7577
"jest-diff": "^30.0.0",
@@ -80,6 +82,14 @@
8082
"tinyglobby": "^0.2.14",
8183
"tinyspy": "^4.0.3"
8284
},
85+
"peerDependencies": {
86+
"jsdom": "*"
87+
},
88+
"peerDependenciesMeta": {
89+
"jsdom": {
90+
"optional": true
91+
}
92+
},
8393
"engines": {
8494
"node": ">=18.0.0"
8595
},

0 commit comments

Comments
 (0)