Skip to content

Commit c9c21a0

Browse files
add jest & upgrade
1 parent a855d61 commit c9c21a0

10 files changed

Lines changed: 2390 additions & 773 deletions

File tree

.eslintrc.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@ module.exports = {
1515
"plugin:react/jsx-runtime", // new JSX runtime for react
1616
"prettier",
1717
],
18-
ignorePatterns: ["node_modules/*", "script/*", "dist/*", "dev/*", "webpack/*", "!.prettierrc", ".eslintrc.js", "babel.config.js", "postcss.config.js"], // We don't want to lint generated files nor node_modules, but we want to lint .prettierrc.js (ignored by default by eslint)
18+
ignorePatterns: [
19+
"node_modules/*",
20+
"script/*",
21+
"dist/*",
22+
"dev/*",
23+
"src/__mocks__/*",
24+
"webpack/*",
25+
"!.prettierrc",
26+
".eslintrc.js",
27+
"babel.config.js",
28+
"postcss.config.js",
29+
"jest.config.js",
30+
], // We don't want to lint generated files nor node_modules, but we want to lint .prettierrc.js (ignored by default by eslint)
1931
parser: "@typescript-eslint/parser",
2032
root: true,
2133
parserOptions: {

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"singleQuote": false,
55
"tabWidth": 2,
66
"useTabs": false,
7-
"printWidth": 160
7+
"printWidth": 160,
8+
"parser": "typescript"
89
}

jest.config.js

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* For a detailed explanation regarding each configuration property, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
module.exports = {
7+
// All imported modules in your tests should be mocked automatically
8+
// automock: false,
9+
10+
// Stop running tests after `n` failures
11+
// bail: 0,
12+
13+
// The directory where Jest should store its cached dependency information
14+
// cacheDirectory: "/tmp/jest_rs",
15+
16+
// Automatically clear mock calls, instances and results before every test
17+
clearMocks: true,
18+
19+
// Indicates whether the coverage information should be collected while executing the test
20+
collectCoverage: true,
21+
22+
// An array of glob patterns indicating a set of files for which coverage information should be collected
23+
// collectCoverageFrom: undefined,
24+
25+
// The directory where Jest should output its coverage files
26+
coverageDirectory: "coverage",
27+
28+
// An array of regexp pattern strings used to skip coverage collection
29+
coveragePathIgnorePatterns: ["/node_modules/"],
30+
31+
// Indicates which provider should be used to instrument code for coverage
32+
coverageProvider: "babel",
33+
34+
// A list of reporter names that Jest uses when writing coverage reports
35+
// coverageReporters: [
36+
// "json",
37+
// "text",
38+
// "lcov",
39+
// "clover"
40+
// ],
41+
42+
// An object that configures minimum threshold enforcement for coverage results
43+
// coverageThreshold: undefined,
44+
45+
// A path to a custom dependency extractor
46+
// dependencyExtractor: undefined,
47+
48+
// Make calling deprecated APIs throw helpful error messages
49+
// errorOnDeprecated: false,
50+
51+
// Force coverage collection from ignored files using an array of glob patterns
52+
// forceCoverageMatch: [],
53+
54+
// A path to a module which exports an async function that is triggered once before all test suites
55+
// globalSetup: undefined,
56+
57+
// A path to a module which exports an async function that is triggered once after all test suites
58+
// globalTeardown: undefined,
59+
60+
// A set of global variables that need to be available in all test environments
61+
// globals: {},
62+
63+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
64+
// maxWorkers: "50%",
65+
66+
// An array of directory names to be searched recursively up from the requiring module's location
67+
// moduleDirectories: [
68+
// "node_modules"
69+
// ],
70+
71+
// An array of file extensions your modules use
72+
moduleFileExtensions: [
73+
"js",
74+
"jsx",
75+
"ts",
76+
"tsx",
77+
// "json",
78+
// "node"
79+
],
80+
81+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
82+
moduleNameMapper: {
83+
// Handle CSS imports (with CSS modules)
84+
// https://jestjs.io/docs/webpack#mocking-css-modules
85+
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy",
86+
87+
// Handle CSS imports (without CSS modules)
88+
"^.+\\.(css|sass|scss)$": "<rootDir>/__mocks__/styleMock.js",
89+
90+
// Handle image imports
91+
// https://jestjs.io/docs/webpack#handling-static-assets
92+
"^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$": `<rootDir>/__mocks__/fileMock.js`,
93+
},
94+
95+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
96+
// modulePathIgnorePatterns: [],
97+
98+
// Activates notifications for test results
99+
// notify: false,
100+
101+
// An enum that specifies notification mode. Requires { notify: true }
102+
// notifyMode: "failure-change",
103+
104+
// A preset that is used as a base for Jest's configuration
105+
// preset: undefined,
106+
107+
// Run tests from one or more projects
108+
// projects: undefined,
109+
110+
// Use this configuration option to add custom reporters to Jest
111+
// reporters: undefined,
112+
113+
// Automatically reset mock state before every test
114+
// resetMocks: false,
115+
116+
// Reset the module registry before running each individual test
117+
// resetModules: false,
118+
119+
// A path to a custom resolver
120+
// resolver: undefined,
121+
122+
// Automatically restore mock state and implementation before every test
123+
// restoreMocks: false,
124+
125+
// The root directory that Jest should scan for tests and modules within
126+
// rootDir: undefined,
127+
128+
// A list of paths to directories that Jest should use to search for files in
129+
roots: ["<rootDir>/src"],
130+
131+
// Allows you to use a custom runner instead of Jest's default test runner
132+
// runner: "jest-runner",
133+
134+
// The paths to modules that run some code to configure or set up the testing environment before each test
135+
// setupFiles: [],
136+
137+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
138+
// setupFilesAfterEnv: [],
139+
140+
// The number of seconds after which a test is considered as slow and reported as such in the results.
141+
// slowTestThreshold: 5,
142+
143+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
144+
// snapshotSerializers: [],
145+
146+
// The test environment that will be used for testing
147+
testEnvironment: "jsdom",
148+
149+
// Options that will be passed to the testEnvironment
150+
// testEnvironmentOptions: {},
151+
152+
// Adds a location field to test results
153+
// testLocationInResults: false,
154+
155+
// The glob patterns Jest uses to detect test files
156+
testMatch: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[tj]s?(x)"],
157+
158+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
159+
testPathIgnorePatterns: ["/node_modules/"],
160+
161+
// The regexp pattern or array of patterns that Jest uses to detect test files
162+
// testRegex: [],
163+
164+
// This option allows the use of a custom results processor
165+
// testResultsProcessor: undefined,
166+
167+
// This option allows use of a custom test runner
168+
// testRunner: "jest-circus/runner",
169+
170+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
171+
// testURL: "http://localhost",
172+
173+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
174+
// timers: "real",
175+
176+
// A map from regular expressions to paths to transformers
177+
transform: undefined,
178+
179+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
180+
transformIgnorePatterns: ["/node_modules/", "^.+\\.module\\.(css|sass|scss)$"],
181+
182+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
183+
// unmockedModulePathPatterns: undefined,
184+
185+
// Indicates whether each individual test should be reported during the run
186+
// verbose: undefined,
187+
188+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
189+
// watchPathIgnorePatterns: [],
190+
191+
// Whether to use watchman for file crawling
192+
// watchman: true,
193+
};

package.json

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,32 @@
66
"scripts": {
77
"dev": "./script/start-dev",
88
"dev:csr": "cross-env CSR=true ./script/start-dev",
9+
"dev:dynamic": "cross-env CSR=true MIDDLEWARE=true ./script/start-dev",
910
"build": "./script/start-prod",
1011
"lint": "eslint --cache --ext ts,tsx .",
1112
"lint:fix": "yarn run lint --fix",
1213
"build:csr": "cross-env CSR=true ./script/start-prod",
13-
"start": "cross-env NODE_ENV=production node ./dist/server/app"
14+
"start": "cross-env NODE_ENV=production node ./dist/server/app",
15+
"test": "jest"
1416
},
1517
"dependencies": {
16-
"@chakra-ui/react": "^1.8.3",
17-
"@emotion/react": "^11.7.1",
18+
"@chakra-ui/react": "^1.8.6",
19+
"@emotion/react": "^11.8.1",
1820
"@emotion/server": "^11.4.0",
19-
"@emotion/styled": "^11.6.0",
21+
"@emotion/styled": "^11.8.1",
2022
"@loadable/component": "^5.15.2",
2123
"@loadable/server": "^5.15.2",
22-
"@mui/material": "^5.4.2",
23-
"antd": "^4.18.7",
24+
"@mui/material": "^5.4.4",
25+
"antd": "^4.18.9",
2426
"axios": "^0.26.0",
2527
"chalk": "4",
2628
"compression": "^1.7.4",
2729
"cookie-parser": "^1.4.6",
2830
"cors": "^2.8.5",
2931
"dotenv": "^16.0.0",
3032
"express": "^4.17.3",
31-
"express-session": "^1.17.1",
32-
"framer-motion": "6.2.4",
33+
"express-session": "^1.17.2",
34+
"framer-motion": "6.2.8",
3335
"immer": "^9.0.12",
3436
"js-cookie": "^3.0.1",
3537
"lodash": "^4.17.21",
@@ -40,18 +42,18 @@
4042
"react-helmet-async": "^1.2.3",
4143
"react-intl": "^5.24.6",
4244
"react-redux": "^7.2.6",
43-
"react-router": "6.2.1",
44-
"react-router-dom": "6.2.1",
45+
"react-router": "6.2.2",
46+
"react-router-dom": "6.2.2",
4547
"redux": "^4.1.2",
4648
"redux-saga": "^1.1.3",
4749
"redux-thunk": "^2.4.1",
4850
"spark-md5": "^3.0.1",
4951
"webpack": "^5.69.1",
50-
"zustand": "^3.7.0"
52+
"zustand": "^3.7.1"
5153
},
5254
"devDependencies": {
53-
"@babel/cli": "^7.17.3",
54-
"@babel/core": "^7.17.4",
55+
"@babel/cli": "^7.17.6",
56+
"@babel/core": "^7.17.5",
5557
"@babel/plugin-proposal-class-properties": "^7.16.7",
5658
"@babel/plugin-proposal-decorators": "^7.17.2",
5759
"@babel/plugin-proposal-export-default-from": "^7.16.7",
@@ -67,63 +69,67 @@
6769
"@loadable/babel-plugin": "^5.13.2",
6870
"@loadable/webpack-plugin": "^5.15.2",
6971
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.4",
70-
"@types/compression": "^1.7.0",
72+
"@types/compression": "^1.7.2",
7173
"@types/cookie-parser": "^1.4.2",
72-
"@types/cors": "^2.8.10",
73-
"@types/express": "^4.17.11",
74-
"@types/express-session": "^1.17.3",
74+
"@types/cors": "^2.8.12",
75+
"@types/express": "^4.17.13",
76+
"@types/express-session": "^1.17.4",
77+
"@types/jest": "^27.4.1",
7578
"@types/js-cookie": "^3.0.1",
76-
"@types/loadable__component": "^5.13.3",
77-
"@types/loadable__server": "^5.12.3",
78-
"@types/lodash": "^4.14.178",
79+
"@types/loadable__component": "^5.13.4",
80+
"@types/loadable__server": "^5.12.6",
81+
"@types/lodash": "^4.14.179",
7982
"@types/multer": "^1.4.7",
80-
"@types/node": "^17.0.18",
83+
"@types/node": "^17.0.21",
8184
"@types/react": "^17.0.39",
82-
"@types/react-dom": "^17.0.11",
83-
"@types/react-redux": "^7.1.22",
85+
"@types/react-dom": "^17.0.13",
86+
"@types/react-redux": "^7.1.23",
8487
"@types/webpack": "^5.28.0",
8588
"@types/webpack-env": "^1.16.3",
8689
"@types/webpack-hot-middleware": "^2.25.6",
87-
"@typescript-eslint/eslint-plugin": "^5.12.0",
88-
"@typescript-eslint/parser": "^5.12.0",
90+
"@typescript-eslint/eslint-plugin": "^5.13.0",
91+
"@typescript-eslint/parser": "^5.13.0",
8992
"autoprefixer": "^10.4.2",
90-
"babel-loader": "^8.2.2",
93+
"babel-jest": "^27.5.1",
94+
"babel-loader": "^8.2.3",
9195
"babel-plugin-import": "^1.13.3",
9296
"clean-webpack-plugin": "^4.0.0",
9397
"core-js": "^3.21.1",
9498
"cross-env": "^7.0.3",
9599
"css-loader": "^6.6.0",
96100
"css-minimizer-webpack-plugin": "^3.4.1",
97-
"eslint": "^8.9.0",
98-
"eslint-config-prettier": "^8.3.0",
101+
"eslint": "^8.10.0",
102+
"eslint-config-prettier": "^8.5.0",
99103
"eslint-import-resolver-typescript": "^2.5.0",
100104
"eslint-plugin-import": "^2.25.4",
101105
"eslint-plugin-jsx-a11y": "^6.5.1",
102106
"eslint-plugin-prettier": "^4.0.0",
103-
"eslint-plugin-react": "^7.28.0",
107+
"eslint-plugin-react": "^7.29.2",
104108
"eslint-plugin-react-hooks": "^4.3.0",
105109
"eslint-webpack-plugin": "^3.1.1",
106110
"file-loader": "^6.2.0",
107111
"fork-ts-checker-webpack-plugin": "^7.2.1",
112+
"identity-obj-proxy": "^3.0.0",
113+
"jest": "^27.5.1",
108114
"mini-css-extract-plugin": "^2.5.3",
109115
"node-sass": "^7.0.1",
110116
"nodemon": "^2.0.15",
111-
"postcss": "^8.4.6",
117+
"postcss": "^8.4.7",
112118
"postcss-loader": "^6.2.1",
113119
"prettier": "^2.5.1",
114120
"react-refresh": "^0.11.0",
115-
"sass": "^1.49.7",
121+
"sass": "^1.49.9",
116122
"sass-loader": "^12.6.0",
117-
"style-loader": "^3.2.1",
123+
"style-loader": "^3.3.1",
118124
"thread-loader": "^3.0.4",
119125
"typescript": "^4.5.5",
120-
"webpack-bundle-analyzer": "^4.4.0",
126+
"webpack-bundle-analyzer": "^4.5.0",
121127
"webpack-cli": "^4.9.2",
122128
"webpack-dev-middleware": "^5.3.1",
123129
"webpack-dev-server": "^4.7.4",
124130
"webpack-hot-middleware": "^2.25.1",
125131
"webpack-manifest-plugin": "^4.1.1",
126-
"webpack-merge": "^5.7.3",
132+
"webpack-merge": "^5.8.0",
127133
"webpack-node-externals": "^3.0.0"
128134
}
129135
}

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,6 @@ yarn run start
7676

7777
## 开箱既支持多种 UI 组件库(CSR / SSR) 通过环境变量 UI 切换(antd,material,chakra-ui,...)
7878

79-
## TODO support swc-loader jest
79+
## jest
80+
81+
## TODO support swc-loader react18

src/__mocks__/fileMock.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "test-file-stub";

src/__mocks__/styleMock.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// https://jestjs.io/docs/webpack#handling-static-assets
2+
module.exports = {};

src/__tests__/env.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test("add", () => {
2+
expect(1 + 2).toBe(3);
3+
});
4+
5+
export {};

0 commit comments

Comments
 (0)