Skip to content

Commit

Permalink
feat: add rsbuild-react-jest example
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Mar 19, 2024
1 parent 8ccd14b commit 98d5296
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rsbuild/react-jest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Local
.DS_Store
*.local
*.log*

# Dist
node_modules
dist/

# IDE
.vscode/*
!.vscode/extensions.json
.idea
29 changes: 29 additions & 0 deletions rsbuild/react-jest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Rsbuild Project

## Setup

Install the dependencies:

```bash
pnpm install
```

## Get Started

Start the dev server:

```bash
pnpm dev
```

Build the app for production:

```bash
pnpm build
```

Preview the production build locally:

```bash
pnpm preview
```
1 change: 1 addition & 0 deletions rsbuild/react-jest/jest-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/jest-dom";
28 changes: 28 additions & 0 deletions rsbuild/react-jest/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default {
testEnvironment: "jest-environment-jsdom",
setupFilesAfterEnv: ["<rootDir>/jest-setup.ts"],
transform: {
"^.+\\.(t|j)sx?$": [
"@swc/jest",
{
jsc: {
parser: {
tsx: true,
syntax: "typescript",
},
transform: {
react: {
runtime: "automatic",
},
},
},
isModule: "unknown",
},
],
},
moduleNameMapper: {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"<rootDir>/tests/__mocks__/fileMock.js",
"\\.(css|less|scss|sass)$": "<rootDir>/tests/__mocks__/styleMock.js",
},
};
30 changes: 30 additions & 0 deletions rsbuild/react-jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "rsbuild-react-jest",
"private": true,
"version": "1.0.0",
"scripts": {
"dev": "rsbuild dev --open",
"build": "rsbuild build",
"preview": "rsbuild preview",
"test": "jest"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@rsbuild/core": "^0.4.15",
"@rsbuild/plugin-react": "^0.4.15",
"@swc/core": "^1.4.8",
"@swc/jest": "^0.2.36",
"@testing-library/jest-dom": "^6.4.2",
"@testing-library/react": "^14.2.1",
"@types/jest": "^29.5.12",
"@types/react": "^18.2.47",
"@types/react-dom": "^18.2.18",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.2"
}
}
6 changes: 6 additions & 0 deletions rsbuild/react-jest/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';

export default defineConfig({
plugins: [pluginReact()],
});
26 changes: 26 additions & 0 deletions rsbuild/react-jest/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
body {
margin: 0;
color: #fff;
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
background-image: linear-gradient(to bottom, #020917, #101725);
}

.content {
display: flex;
min-height: 100vh;
line-height: 1.1;
text-align: center;
flex-direction: column;
justify-content: center;
}

.content h1 {
font-size: 3.6rem;
font-weight: 700;
}

.content p {
font-size: 1.2rem;
font-weight: 400;
opacity: 0.5;
}
12 changes: 12 additions & 0 deletions rsbuild/react-jest/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import './App.css';

const App = () => {
return (
<div className="content">
<h1>Rsbuild with React</h1>
<p>Start building amazing things with Rsbuild.</p>
</div>
);
};

export default App;
1 change: 1 addition & 0 deletions rsbuild/react-jest/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@rsbuild/core/types" />
10 changes: 10 additions & 0 deletions rsbuild/react-jest/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root')!);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
8 changes: 8 additions & 0 deletions rsbuild/react-jest/tests/App.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { render, screen } from "@testing-library/react";
import App from "../src/App";

test("Renders the main page", () => {
const testMessage = "Rsbuild with React";
render(<App />);
expect(screen.getByText(testMessage)).toBeInTheDocument();
});
1 change: 1 addition & 0 deletions rsbuild/react-jest/tests/__mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "test-file-stub";
1 change: 1 addition & 0 deletions rsbuild/react-jest/tests/__mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
7 changes: 7 additions & 0 deletions rsbuild/react-jest/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["@testing-library/jest-dom"]
},
"include": ["./"]
}
15 changes: 15 additions & 0 deletions rsbuild/react-jest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES2020",
"lib": ["DOM", "ES2020"],
"module": "ESNext",
"jsx": "react-jsx",
"strict": true,
"skipLibCheck": true,
"isolatedModules": true,
"resolveJsonModule": true,
"moduleResolution": "bundler",
"useDefineForClassFields": true
},
"include": ["src"]
}

0 comments on commit 98d5296

Please sign in to comment.