From b899d21980a4071420a07c6c9454a287e3689b19 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Sat, 10 May 2025 11:33:22 +0800 Subject: [PATCH] add jest --- admin-ui/jest.config.js | 8 ++++++++ admin-ui/jest.config.ts | 41 ------------------------------------- admin-ui/jest.setup.ts | 15 ++++++++++++++ admin-ui/jest/map.test.ts | 13 ++++++++++++ admin-ui/package.json | 4 ++-- mobile-ui/jest.config.js | 8 ++++++++ mobile-ui/jest.setup.ts | 15 ++++++++++++++ mobile-ui/jest/map.test.ts | 13 ++++++++++++ mobile-ui/package.json | 15 ++++++++++++-- mobile-ui/webpack.common.js | 2 -- 10 files changed, 87 insertions(+), 47 deletions(-) create mode 100644 admin-ui/jest.config.js delete mode 100644 admin-ui/jest.config.ts create mode 100644 admin-ui/jest.setup.ts create mode 100644 admin-ui/jest/map.test.ts create mode 100644 mobile-ui/jest.config.js create mode 100644 mobile-ui/jest.setup.ts create mode 100644 mobile-ui/jest/map.test.ts diff --git a/admin-ui/jest.config.js b/admin-ui/jest.config.js new file mode 100644 index 00000000..098c2987 --- /dev/null +++ b/admin-ui/jest.config.js @@ -0,0 +1,8 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'jsdom', + moduleNameMapper: { + '\\.(css|less|scss|sass)$': 'identity-obj-proxy', + }, + setupFilesAfterEnv: ['/jest.setup.ts'], +}; \ No newline at end of file diff --git a/admin-ui/jest.config.ts b/admin-ui/jest.config.ts deleted file mode 100644 index ad3ff041..00000000 --- a/admin-ui/jest.config.ts +++ /dev/null @@ -1,41 +0,0 @@ -import type { Config } from 'jest'; - -const config: Config = { - preset: 'ts-jest', - testEnvironment: 'jsdom', - transform: { - '^.+\\.(ts|tsx)$': ['ts-jest', { - useESM: true, - }], - '^.+\\.(js|jsx|mjs)$': ['babel-jest', { - presets: [ - ['@babel/preset-env', { - targets: { - node: 'current', - }, - }], - '@babel/preset-react', - '@babel/preset-typescript' - ], - }], - }, - moduleNameMapper: { - '^monaco-editor$': '/__mocks__/monaco-editor.js', - "@logicflow": "/node_modules/@logicflow/core/dist/index.min.js", - '\\.(css|less|scss|sass)$': 'identity-obj-proxy', - '\\.(jpg|jpeg|png|gif|webp|svg)$': '/__mocks__/fileMock.js', - '^@/(.*)$': '/src/$1' - }, - setupFilesAfterEnv: ['/src/jest.setup.ts'], - testMatch: [ - "**/__test__/**/*.[jt]s?(x)", - "**/__tests__/**/*.[jt]s?(x)", - "**/?(*.)+(spec|test).[jt]s?(x)" - ], - moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], - transformIgnorePatterns: [ - 'node_modules/(?!(lodash-es|@ant-design|@logicflow|other-esm-modules)/)' - ], -}; - -export default config; diff --git a/admin-ui/jest.setup.ts b/admin-ui/jest.setup.ts new file mode 100644 index 00000000..0732ca30 --- /dev/null +++ b/admin-ui/jest.setup.ts @@ -0,0 +1,15 @@ +import '@testing-library/jest-dom'; + +Object.defineProperty(window, 'matchMedia', { + writable: true, + value: jest.fn().mockImplementation(query => ({ + matches: false, + media: query, + onchange: null, + addListener: jest.fn(), // 旧版 API + removeListener: jest.fn(), // 旧版 API + addEventListener: jest.fn(), // 新版 API + removeEventListener: jest.fn(), // 新版 API + dispatchEvent: jest.fn(), + })), +}); \ No newline at end of file diff --git a/admin-ui/jest/map.test.ts b/admin-ui/jest/map.test.ts new file mode 100644 index 00000000..0b391f71 --- /dev/null +++ b/admin-ui/jest/map.test.ts @@ -0,0 +1,13 @@ +import '@testing-library/jest-dom'; + + +test("map", () => { + const map = new Map(); + map.set('key', 'value'); + expect(map.get('key')).toBe('value'); + expect(map.has('key')).toBe(true); + expect(map.size).toBe(1); + map.delete('key'); + expect(map.has('key')).toBe(false); + expect(map.size).toBe(0); +}) \ No newline at end of file diff --git a/admin-ui/package.json b/admin-ui/package.json index 88ade8f3..960d70f2 100644 --- a/admin-ui/package.json +++ b/admin-ui/package.json @@ -66,7 +66,7 @@ "@babel/preset-typescript": "^7.26.0", "@testing-library/dom": "^10.4.0", "@testing-library/jest-dom": "^6.6.3", - "@testing-library/react": "^16.1.0", + "@testing-library/react": "^16.3.0", "@types/jest": "^29.5.14", "@types/lodash": "^4.17.7", "@types/lodash-es": "^4.17.12", @@ -85,7 +85,7 @@ "sass": "^1.78.0", "sass-loader": "^16.0.1", "style-loader": "^4.0.0", - "ts-jest": "^29.2.5", + "ts-jest": "^29.3.2", "ts-loader": "^9.5.1", "ts-node": "^10.9.2", "webpack": "^5.94.0", diff --git a/mobile-ui/jest.config.js b/mobile-ui/jest.config.js new file mode 100644 index 00000000..098c2987 --- /dev/null +++ b/mobile-ui/jest.config.js @@ -0,0 +1,8 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'jsdom', + moduleNameMapper: { + '\\.(css|less|scss|sass)$': 'identity-obj-proxy', + }, + setupFilesAfterEnv: ['/jest.setup.ts'], +}; \ No newline at end of file diff --git a/mobile-ui/jest.setup.ts b/mobile-ui/jest.setup.ts new file mode 100644 index 00000000..0732ca30 --- /dev/null +++ b/mobile-ui/jest.setup.ts @@ -0,0 +1,15 @@ +import '@testing-library/jest-dom'; + +Object.defineProperty(window, 'matchMedia', { + writable: true, + value: jest.fn().mockImplementation(query => ({ + matches: false, + media: query, + onchange: null, + addListener: jest.fn(), // 旧版 API + removeListener: jest.fn(), // 旧版 API + addEventListener: jest.fn(), // 新版 API + removeEventListener: jest.fn(), // 新版 API + dispatchEvent: jest.fn(), + })), +}); \ No newline at end of file diff --git a/mobile-ui/jest/map.test.ts b/mobile-ui/jest/map.test.ts new file mode 100644 index 00000000..0b391f71 --- /dev/null +++ b/mobile-ui/jest/map.test.ts @@ -0,0 +1,13 @@ +import '@testing-library/jest-dom'; + + +test("map", () => { + const map = new Map(); + map.set('key', 'value'); + expect(map.get('key')).toBe('value'); + expect(map.has('key')).toBe(true); + expect(map.size).toBe(1); + map.delete('key'); + expect(map.has('key')).toBe(false); + expect(map.size).toBe(0); +}) \ No newline at end of file diff --git a/mobile-ui/package.json b/mobile-ui/package.json index 8155759c..524cd4f3 100644 --- a/mobile-ui/package.json +++ b/mobile-ui/package.json @@ -36,7 +36,9 @@ "scripts": { "start": "webpack serve --config webpack.config.mock.js --open", "dev": "webpack serve --config webpack.config.dev.js --open", - "build": "webpack --mode production --config webpack.config.prod.js" + "build": "webpack --mode production --config webpack.config.prod.js", + "test": "jest", + "test:watch": "jest --watchAll" }, "eslintConfig": { "extends": [ @@ -59,16 +61,25 @@ "@types/lodash": "^4.17.7", "@types/lodash-es": "^4.17.12", "@types/react-resizable": "^3.0.8", + "@testing-library/dom": "^10.4.0", + "@testing-library/jest-dom": "^6.6.3", + "@testing-library/react": "^16.3.0", + "@types/jest": "^29.5.14", + "@types/mockjs": "^1.0.10", + "babel-jest": "^29.7.0", "clean-webpack-plugin": "^4.0.0", "copy-webpack-plugin": "^12.0.2", "css-loader": "^7.1.2", "express": "^4.21.0", "html-webpack-plugin": "^5.6.0", + "identity-obj-proxy": "^3.0.0", + "jest": "^29.7.0", + "jest-environment-jsdom": "^29.7.0", "mockjs": "^1.1.0", - "monaco-editor-webpack-plugin": "^7.1.0", "sass": "^1.78.0", "sass-loader": "^16.0.1", "style-loader": "^4.0.0", + "ts-jest": "^29.3.2", "ts-loader": "^9.5.1", "webpack": "^5.94.0", "webpack-cli": "^5.1.4", diff --git a/mobile-ui/webpack.common.js b/mobile-ui/webpack.common.js index 8aeb1056..24defe87 100644 --- a/mobile-ui/webpack.common.js +++ b/mobile-ui/webpack.common.js @@ -2,7 +2,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const CopyWebpackPlugin = require('copy-webpack-plugin'); const {CleanWebpackPlugin} = require('clean-webpack-plugin'); -const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin'); const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin"); const {dependencies} = require("./package.json"); @@ -56,7 +55,6 @@ module.exports = { }, plugins: [ new CleanWebpackPlugin(), - new MonacoWebpackPlugin(), new HtmlWebpackPlugin({ template: './public/index.html', }),