-
Notifications
You must be signed in to change notification settings - Fork 0
feat(test): support inline Rstest projects #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| ["index", "commands"] | ||
| ["index", "commands", "testing"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # Testing | ||
|
|
||
| Rstack uses [Rstest](https://rstest.rs/) for testing. The value passed to `define.test()` is an Rstest configuration, similar to using Rstest's `defineConfig()` directly. | ||
|
|
||
| Test APIs and configuration helpers are available from `rstack/test`: | ||
|
|
||
| ```ts | ||
| import { defineInlineProject, expect, test } from 'rstack/test'; | ||
| ``` | ||
|
|
||
| ## Single project | ||
|
|
||
| For a single test project, pass the Rstest options directly: | ||
|
|
||
| ```ts title="rstack.config.ts" | ||
| import { define } from 'rstack'; | ||
|
|
||
| define.app({ | ||
| // Rsbuild configuration | ||
| }); | ||
|
|
||
| define.test({ | ||
| testEnvironment: 'happy-dom', | ||
| setupFiles: ['./tests/rstest.setup.ts'], | ||
| }); | ||
| ``` | ||
|
|
||
| Unless `extends` is provided explicitly, Rstack automatically extends the test configuration from `define.app()`. If there is no app configuration, it falls back to `define.lib()`. | ||
|
|
||
| ## Inline projects | ||
|
|
||
| Use Rstest inline projects when one app or library needs multiple test configurations, such as separate Node.js and DOM test environments: | ||
|
|
||
| ```ts title="rstack.config.ts" | ||
| import { define } from 'rstack'; | ||
| import { defineInlineProject } from 'rstack/test'; | ||
|
|
||
| define.app({ | ||
| // Shared by both inline projects | ||
| }); | ||
|
|
||
| define.test({ | ||
| coverage: { | ||
| enabled: true, | ||
| }, | ||
| projects: [ | ||
| defineInlineProject({ | ||
| name: 'node', | ||
| include: ['./tests/node/**/*.test.ts'], | ||
| testEnvironment: 'node', | ||
| }), | ||
| defineInlineProject({ | ||
| name: 'dom', | ||
| include: ['./tests/dom/**/*.test.tsx'], | ||
| testEnvironment: 'happy-dom', | ||
| setupFiles: ['./tests/rstest.setup.ts'], | ||
| }), | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| Rstack applies the shared `define.app()` or `define.lib()` adapter to every inline project that does not define its own `extends`. Function-based app or library configuration is resolved once and shared by all inline projects. | ||
|
|
||
| Run all projects: | ||
|
|
||
| ```bash | ||
| rs test | ||
| ``` | ||
|
|
||
| Run one project by name: | ||
|
|
||
| ```bash | ||
| rs test --project dom | ||
| ``` | ||
|
|
||
| See [`examples/rstest-inline-projects`](https://github.com/rstackjs/rstack-cli/tree/main/examples/rstest-inline-projects) for a complete example that tests React server rendering in Node.js and client rendering in happy-dom. | ||
|
|
||
| ## Custom `extends` | ||
|
|
||
| An inline project can opt out of automatic app or library inheritance by providing `extends` explicitly: | ||
|
|
||
| ```ts | ||
| import { defineInlineProject } from 'rstack/test'; | ||
|
|
||
| const customProject = defineInlineProject({ | ||
| name: 'custom', | ||
| extends: customAdapter(), | ||
| }); | ||
| ``` | ||
|
|
||
| If the root `define.test()` configuration provides `extends`, Rstack leaves the complete test configuration unchanged. | ||
|
|
||
| ## External projects | ||
|
|
||
| String project entries are passed to Rstest unchanged: | ||
|
|
||
| ```ts | ||
| import { define } from 'rstack'; | ||
|
|
||
| define.test({ | ||
| projects: ['./legacy/rstest.config.ts'], | ||
| }); | ||
| ``` | ||
|
|
||
| External string projects load their own Rstest configuration and do not inherit the current `define.app()` or `define.lib()` configuration. Use inline projects when projects should share the current Rstack build configuration. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "name": "@examples/rstest-inline-projects", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "rs build", | ||
| "dev": "rs dev", | ||
| "preview": "rs preview", | ||
| "test": "rs test" | ||
| }, | ||
| "dependencies": { | ||
| "react": "catalog:", | ||
| "react-dom": "catalog:" | ||
| }, | ||
| "devDependencies": { | ||
| "@rsbuild/plugin-react": "catalog:", | ||
| "@testing-library/dom": "catalog:", | ||
| "@testing-library/react": "catalog:", | ||
| "@types/node": "catalog:", | ||
| "@types/react": "catalog:", | ||
| "@types/react-dom": "catalog:", | ||
| "happy-dom": "catalog:", | ||
| "rstack": "workspace:*", | ||
| "typescript": "catalog:" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { define } from 'rstack'; | ||
| import { defineInlineProject } from 'rstack/test'; | ||
|
|
||
| define.app(async () => { | ||
| const { pluginReact } = await import('@rsbuild/plugin-react'); | ||
|
|
||
| return { | ||
| plugins: [pluginReact()], | ||
| }; | ||
| }); | ||
|
|
||
| define.test({ | ||
| projects: [ | ||
| defineInlineProject({ | ||
| name: 'ssr', | ||
| include: ['./tests/ssr.test.tsx'], | ||
| testEnvironment: 'node', | ||
| }), | ||
| defineInlineProject({ | ||
| name: 'dom', | ||
| include: ['./tests/dom.test.tsx'], | ||
| testEnvironment: 'happy-dom', | ||
| }), | ||
| ], | ||
| }); |
|
chenjiahan marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export default function App() { | ||
| return ( | ||
| <main> | ||
| <h1>Rstack React SSR</h1> | ||
| <p>Rendered on the server and hydrated in the browser.</p> | ||
| </main> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { hydrateRoot } from 'react-dom/client'; | ||
| import App from './App'; | ||
|
|
||
| const root = document.getElementById('root'); | ||
|
|
||
| if (root) { | ||
| hydrateRoot(root, <App />); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { expect, test } from 'rstack/test'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import App from '../src/App'; | ||
|
|
||
| test('renders the app in a DOM environment', () => { | ||
| render(<App />); | ||
|
|
||
| expect(screen.getByRole('heading', { name: 'Rstack React SSR' })).toBeTruthy(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { expect, test } from 'rstack/test'; | ||
| import { renderToString } from 'react-dom/server'; | ||
| import App from '../src/App'; | ||
|
|
||
| test('renders the app on the server', () => { | ||
| const html = renderToString(<App />); | ||
|
|
||
| expect(html).toContain('Rstack React SSR'); | ||
| expect(html).toContain('Rendered on the server'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "lib": ["DOM", "ES2023"], | ||
| "jsx": "react-jsx", | ||
| "target": "ES2023", | ||
| "noEmit": true, | ||
| "skipLibCheck": true, | ||
| "types": ["rstack/types", "node"], | ||
| "moduleDetection": "force", | ||
| "moduleResolution": "bundler", | ||
| "verbatimModuleSyntax": true, | ||
| "resolveJsonModule": true, | ||
| "allowImportingTsExtensions": true, | ||
| "strict": true | ||
| }, | ||
| "include": ["src", "tests", "rstack.config.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/rstack/test/config/define-test-projects-app/custom.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { expect, test } from 'rstack/test'; | ||
|
|
||
| declare const RSTACK_INHERITED_CONFIG: string; | ||
|
|
||
| test('an inline project can provide its own extends config', () => { | ||
| expect(RSTACK_INHERITED_CONFIG).toBe('custom'); | ||
| }); |
9 changes: 9 additions & 0 deletions
9
packages/rstack/test/config/define-test-projects-app/first.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { expect, test } from 'rstack/test'; | ||
|
|
||
| declare const RSTACK_APP_CONFIG_CALLS: number; | ||
| declare const RSTACK_INHERITED_CONFIG: string; | ||
|
|
||
| test('the first inline project inherits the app config', () => { | ||
| expect(RSTACK_INHERITED_CONFIG).toBe('app'); | ||
| expect(RSTACK_APP_CONFIG_CALLS).toBe(1); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will move ths page to Rstack CLI website in the future