@modern-js/main-doc 3.1.5 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/docs/en/community/contributing-guide.mdx +1 -1
  2. package/docs/en/components/international/init-options-desc.mdx +1 -1
  3. package/docs/en/components/international/install-command.mdx +4 -17
  4. package/docs/en/components/international/instance-code.mdx +4 -14
  5. package/docs/en/components/international/introduce.mdx +4 -1
  6. package/docs/en/configure/app/server/tsconfig-path.mdx +63 -0
  7. package/docs/en/configure/app/source/enable-async-pre-entry.mdx +30 -0
  8. package/docs/en/configure/app/tools/dev-server.mdx +0 -4
  9. package/docs/en/guides/advanced-features/international/_meta.json +0 -1
  10. package/docs/en/guides/advanced-features/international/advanced.mdx +48 -109
  11. package/docs/en/guides/advanced-features/international/api.mdx +125 -290
  12. package/docs/en/guides/advanced-features/international/best-practices.mdx +203 -48
  13. package/docs/en/guides/advanced-features/international/configuration.mdx +108 -315
  14. package/docs/en/guides/advanced-features/international/locale-detection.mdx +62 -208
  15. package/docs/en/guides/advanced-features/international/quick-start.mdx +41 -55
  16. package/docs/en/guides/advanced-features/international/resource-loading.mdx +63 -322
  17. package/docs/en/guides/advanced-features/international/routing.mdx +60 -138
  18. package/docs/en/guides/advanced-features/international.mdx +19 -27
  19. package/docs/en/guides/basic-features/alias.mdx +1 -1
  20. package/docs/en/guides/basic-features/html.mdx +2 -2
  21. package/docs/en/guides/basic-features/static-assets.mdx +1 -2
  22. package/docs/en/guides/basic-features/testing/_meta.json +1 -1
  23. package/docs/en/guides/concept/entries.mdx +2 -2
  24. package/docs/en/guides/get-started/tech-stack.mdx +6 -0
  25. package/docs/zh/community/contributing-guide.mdx +1 -1
  26. package/docs/zh/components/international/init-options-desc.mdx +1 -1
  27. package/docs/zh/components/international/install-command.mdx +4 -16
  28. package/docs/zh/components/international/instance-code.mdx +4 -14
  29. package/docs/zh/components/international/introduce.mdx +5 -2
  30. package/docs/zh/configure/app/server/tsconfig-path.mdx +63 -0
  31. package/docs/zh/configure/app/source/enable-async-pre-entry.mdx +77 -0
  32. package/docs/zh/configure/app/tools/dev-server.mdx +0 -4
  33. package/docs/zh/guides/advanced-features/bff/function.mdx +2 -2
  34. package/docs/zh/guides/advanced-features/international/_meta.json +0 -1
  35. package/docs/zh/guides/advanced-features/international/advanced.mdx +48 -109
  36. package/docs/zh/guides/advanced-features/international/api.mdx +126 -292
  37. package/docs/zh/guides/advanced-features/international/best-practices.mdx +204 -49
  38. package/docs/zh/guides/advanced-features/international/configuration.mdx +105 -318
  39. package/docs/zh/guides/advanced-features/international/locale-detection.mdx +62 -236
  40. package/docs/zh/guides/advanced-features/international/quick-start.mdx +40 -54
  41. package/docs/zh/guides/advanced-features/international/resource-loading.mdx +62 -324
  42. package/docs/zh/guides/advanced-features/international/routing.mdx +58 -136
  43. package/docs/zh/guides/advanced-features/international.mdx +19 -26
  44. package/docs/zh/guides/basic-features/alias.mdx +1 -1
  45. package/docs/zh/guides/basic-features/html.mdx +2 -2
  46. package/docs/zh/guides/basic-features/static-assets.mdx +1 -2
  47. package/docs/zh/guides/basic-features/testing/_meta.json +1 -1
  48. package/docs/zh/guides/concept/entries.mdx +2 -2
  49. package/docs/zh/guides/get-started/tech-stack.mdx +6 -0
  50. package/package.json +4 -4
  51. package/docs/en/components/rspackPrecautions.mdx +0 -6
  52. package/docs/en/guides/advanced-features/international/basic.mdx +0 -417
  53. package/docs/en/guides/basic-features/testing/cypress.mdx +0 -95
  54. package/docs/en/guides/basic-features/testing/jest.mdx +0 -148
  55. package/docs/en/guides/basic-features/testing/vitest.mdx +0 -100
  56. package/docs/zh/components/rspackPrecautions.mdx +0 -6
  57. package/docs/zh/guides/advanced-features/international/basic.mdx +0 -416
  58. package/docs/zh/guides/basic-features/testing/cypress.mdx +0 -95
  59. package/docs/zh/guides/basic-features/testing/jest.mdx +0 -148
  60. package/docs/zh/guides/basic-features/testing/vitest.mdx +0 -100
@@ -1,148 +0,0 @@
1
- # Jest
2
-
3
- Jest 是一个 JavaScript 测试框架,它主要和 React Testing Library 一起用于单元测试和 Snapshot 测试。
4
-
5
- 在 Modern.js 中使用 Jest 需要先安装依赖,可以执行以下命令:
6
-
7
- import { PackageManagerTabs } from '@theme';
8
-
9
- <PackageManagerTabs command={{
10
- npm: "npm install -D jest jest-environment-jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom",
11
- yarn: "yarn add -D jest jest-environment-jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom",
12
- pnpm: "pnpm install -D jest jest-environment-jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom"
13
- }} />
14
-
15
- 随后,你可以运行以下命令,这将自动在项目中初始化 Jest,并生成一个基础的 `jest.config.ts` 配置:
16
-
17
- <PackageManagerTabs command={{
18
- npm: "npm init jest@latest",
19
- yarn: "yarn create jest@latest",
20
- pnpm: "pnpm create jest@latest"
21
- }} />
22
-
23
- ## 配置文件
24
-
25
- :::note
26
- 本章节会使用 `.ts` 文件来完成 Jest 测试。
27
- :::
28
-
29
- 相比于其他的测试框架,Jest 在构建层面需要更多的配置,例如处理 JSX 和 ESM 语法,因此首先需要安装一些额外的依赖:
30
-
31
- <PackageManagerTabs command={{
32
- npm: "npm install -D babel-jest @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript",
33
- yarn: "yarn add -D babel-jest @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript",
34
- pnpm: "pnpm install -D babel-jest @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript"
35
- }} />
36
-
37
- ### 配置 Jest
38
-
39
- 你需要进一步配置 `jest.config.ts` 文件,以便让 Jest 能够正确地编译和运行测试用例。下面是一个最基本的配置:
40
-
41
- ```ts title="jest.config.ts"
42
- import type { Config } from 'jest';
43
-
44
- const config: Config = {
45
- coverageProvider: 'babel',
46
- setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
47
- testEnvironment: 'jsdom',
48
- transform: {
49
- '^.+\\.(js|jsx|ts|tsx)$': 'babel-jest',
50
- },
51
- transformIgnorePatterns: [],
52
- };
53
-
54
- export default config;
55
- ```
56
-
57
- 配置中,将 `transformIgnorePatterns` 设置为了空数组,意味着所有的文件都会经过编译,如果你希望提升测试运行的速度,可以按需配置。
58
-
59
- `setupFilesAfterEnv` 会在启动时执行,在 `jest.setup.ts` 中,可以引入 `@testing-library/jest-dom`。它包含了一组便捷的自定义匹配器,例如 `.toBeInTheDocument()`,使编写测试变得更容易:
60
-
61
- ```ts title="jest.setup.ts"
62
- import '@testing-library/jest-dom';
63
- ```
64
-
65
- ### 配置 Babel
66
-
67
- 你需要配置 Babel 让 Jest 能够自动编译 JSX 等语法,下面是一个基本的配置:
68
-
69
- ```js title="babel.config.js"
70
- module.exports = {
71
- presets: [
72
- ['@babel/preset-env', { targets: { node: 'current' } }],
73
- ['@babel/preset-react', { runtime: 'automatic' }],
74
- '@babel/preset-typescript',
75
- ],
76
- };
77
- ```
78
-
79
- ## 编写测试用例
80
-
81
- 现在,你可以开始编写测试用例了,首先在 `package.json` 中添加一个 `test` 命令:
82
-
83
- ```json title="package.json"
84
- {
85
- "scripts": {
86
- "test": "jest"
87
- }
88
- }
89
- ```
90
-
91
- 创建一个简单的页面用于测试:
92
-
93
- ```tsx title="routes/page.tsx"
94
- import { Link } from '@modern-js/runtime/router';
95
-
96
- const Index = () => (
97
- <div>
98
- <h1>Home</h1>
99
- <Link to="/about">About</Link>
100
- </div>
101
- );
102
-
103
- export default Index;
104
- ```
105
-
106
- 添加测试用例,检测页面中是否有预期的文本:
107
-
108
- ```tsx title="__tests__/page.test.tsx"
109
- import '@testing-library/jest-dom';
110
- import { render, screen } from '@testing-library/react';
111
- import { BrowserRouter as Router } from '@modern-js/runtime/router';
112
- import Page from '../routes/page';
113
-
114
- describe('Page', () => {
115
- it('renders a heading', () => {
116
- render(
117
- <Router>
118
- <Page />
119
- </Router>,
120
- );
121
-
122
- const heading = screen.getByRole('heading', { level: 1 });
123
-
124
- expect(heading).toBeInTheDocument();
125
- });
126
- });
127
- ```
128
-
129
- 上述用例中,我们从 `@modern-js/runtime/router` 引入了 `<Router>` 组件,这是因为 React Router 在渲染部分路由相关组件时,必须要有对应的上下文。
130
-
131
- :::note
132
- 直接在 Modern.js 应用中运行时,`<Router>` 组件会自动注入。
133
- :::
134
-
135
- ## 运行测试用例
136
-
137
- 执行上述 `test` 命令,运行测试用例:
138
-
139
- ```bash
140
- PASS src/__tests__/page.test.tsx
141
- Page
142
- ✓ renders a heading (31 ms)
143
-
144
- Test Suites: 1 passed, 1 total
145
- Tests: 1 passed, 1 total
146
- Snapshots: 0 total
147
- Time: 0.959 s, estimated 1 s
148
- ```
@@ -1,100 +0,0 @@
1
- # Vitest
2
-
3
- Vitest 是由 Vite 驱动的测试框架,和 React Testing Library 配合可以用于单元测试。
4
-
5
- 在 Modern.js 中使用 Vitest 需要先安装依赖,可以执行以下命令:
6
-
7
- import { PackageManagerTabs } from '@theme';
8
-
9
- <PackageManagerTabs command={{
10
- npm: "npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom",
11
- yarn: "yarn add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom",
12
- pnpm: "pnpm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom",
13
- bun: "bun add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom"
14
- }} />
15
-
16
- 接下来,你需要创建一个 Vitest 配置文件 `vitest.config.ts`,内容如下:
17
-
18
- ```ts title="vitest.config.ts"
19
- import { defineConfig } from 'vitest/config'
20
- import react from '@vitejs/plugin-react'
21
-
22
- export default defineConfig({
23
- plugins: [react()],
24
- test: {
25
- environment: 'jsdom',
26
- },
27
- })
28
- ```
29
-
30
- 更多关于 Vitest 配置的信息,可以参考 [Vitest 配置文档](https://vitest.dev/config/#configuration)。
31
-
32
- 你可以选择性的将 `vitest` 命令添加到 `package.json` 中:
33
-
34
- ```json title="package.json"
35
- {
36
- "scripts": {
37
- "test": "vitest"
38
- }
39
- }
40
- ```
41
-
42
- 运行该命令后,Vitest 会自动监听你的文件变化,并重新运行用例。
43
-
44
- ## 创建单元测试
45
-
46
- 首先,创建一个简单的页面用于测试:
47
-
48
- ```tsx title="routes/page.tsx"
49
- import { Link } from '@modern-js/runtime/router';
50
-
51
- const Index = () => (
52
- <div>
53
- <h1>Home</h1>
54
- <Link to="/about">About</Link>
55
- </div>
56
- );
57
-
58
- export default Index;
59
- ```
60
-
61
- 添加测试用例,检测页面中是否有预期的文本:
62
-
63
- ```tsx title="__tests__/page.test.tsx"
64
- import { expect, test } from 'vitest';
65
- import { render, screen } from '@testing-library/react';
66
- import { BrowserRouter as Router } from '@modern-js/runtime/router';
67
- import Page from '../routes/page';
68
-
69
- test('Page', () => {
70
- render(
71
- <Router>
72
- <Page />
73
- </Router>,
74
- );
75
- expect(screen.getByRole('heading', { level: 1, name: 'Home' })).toBeDefined();
76
- });
77
- ```
78
-
79
- 上述用例中,我们从 `@modern-js/runtime/router` 引入了 `<Router>` 组件,这是因为 React Router 在渲染部分路由相关组件时,必须要有对应的上下文。
80
-
81
- :::note
82
- 直接在 Modern.js 应用中运行时,`<Router>` 组件会自动注入。
83
- :::
84
-
85
- ## 运行测试用例
86
-
87
- 执行上述 `test` 命令,运行测试用例:
88
-
89
- ```bash
90
- ✓ src/__tests__/page.test.tsx (1)
91
- ✓ Page
92
-
93
- Test Files 1 passed (1)
94
- Tests 1 passed (1)
95
- Start at 15:37:12
96
- Duration 999ms (transform 119ms, setup 0ms, collect 365ms, tests 33ms, environment 421ms, prepare 44ms)
97
-
98
-
99
- PASS Waiting for file changes...
100
- ```