2021.12.27 星期一 :
Taro文档-进阶指南-第三方工具-Jest: https://nervjs.github.io/taro-docs/docs/external-libraries#jest
1 | module.exports = { |
该方法不适用路由跳转和部分生命周期测试。
PS: 没跑通。直接mock了整个taro.js。
注意配置snapshotSerializers
,否则快照内容为空。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33/* test/setup.js */
import { configure } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
configure({ adapter: new Adapter() });
/* test/jest.config.js */
module.exports = {
// verbose: true,
testEnvironment: "jsdom",
rootDir: path.resolve(__dirname, "../"),
setupFilesAfterEnv: ["<rootDir>/test/setup"],
moduleNameMapper: {
'@tarojs/components': '@tarojs/components/dist-h5/react',
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"<rootDir>/test/mocks/emptyMock.js",
"\\.(css|less|scss)$": "<rootDir>/test/mocks/emptyMock.js",
'^@/(.*)$': '<rootDir>/src/$1',
},
transform: {
"^.+\\.[t|j]sx?$": "babel-jest",
},
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!((@tarojs)|(@base))/)',
],
snapshotSerializers: ["enzyme-to-json/serializer"],
setupFilesAfterEnv: ["<rootDir>/test/setup"],
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
testPathIgnorePatterns: [
"/node_modules",
// '^.+\\.(css|sass|scss|less)$',
],
};
通过moduleNameMapper
mock了整个taro.js。
并不关心taro的内部实现;还可以加速。
Taro-H5 中使用到的 API 实际上并不在 @tarojs/taro 的入口文件之下。
使用 Jest 测试也是类似,需要添加配置如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 import handleApiMock from './apiMock';
export const getApp = jest.fn(() => ({}));
export const getCurrentInstance = jest.fn(() => ({
page: {},
router: {
params: {
dealId: '245777499999', // follow
dealState: '18', // follow
id: '', // remind
phone: '' // remind
}
}
}));
export const getCurrentPages = jest.fn(() => ({}));
export const setNavigationBarTitle = jest.fn();
export const getStorageSync = jest.fn();
export const setStorageSync = jest.fn();
export const getSystemInfoSync = jest.fn(() => ({}));
export const getSystemInfo = jest.fn();
export const useDidShow = jest.fn();
export const useDidHide = jest.fn();
export const useReachBottom = jest.fn();
export const useShareAppMessage = jest.fn();
export const getEnv = jest.fn();
// export const request = jest.fn();
export const request = jest.fn().mockImplementation(handleApiMock);
export const addInterceptor = jest.fn();
export const getNetworkType = jest.fn();
export const usePageScroll = jest.fn();
export const commonGetData = jest.fn();
export const showToast = jest.fn();
export const showModal = jest.fn(({success, fail}) => {
success && success();
fail && fail();
});
export const eventCenter = {
on: jest.fn(() => eventCenter),
off: jest.fn(() => eventCenter),
}
const Taro = {
getApp,
getCurrentInstance,
getCurrentPages,
setNavigationBarTitle,
getStorageSync,
setStorageSync,
getSystemInfoSync,
getSystemInfo,
useDidShow,
useDidHide,
useShareAppMessage,
useReachBottom,
getEnv,
ENV_TYPE: {},
request,
addInterceptor,
getNetworkType,
usePageScroll,
commonGetData,
showModal,
showToast,
eventCenter,
}
export default Taro
process.env.TARO_ENV
package.json "test": "cross-env JEST_REPORT_FILE='./test/report/test-report.json' TARO_ENV=h5 jest --config test/jest.conf.js",
1 | globals: { |
另一种方式:在jest配置文件中直接赋值process.env = Object.assign(process.env, {TARO_ENV: 'h5'});
问题:ReferenceError: $RefreshSig$ is not defined
, ReferenceError: $RefreshReg$ is not defined
setup.js1
2global.$RefreshReg$ = () => {};
global.$RefreshSig$ = () => () => {};
taro项目中cookie是多端文件,会报错。RangeError: Maximum call stack size exceeded
解决:用mockModule的方式,指向index.h5.js。
原因:可能是在小程序的cookie utils中发生了什么事情。
引入tarojs/components 出错。已经指向了h5的components了。
1 | SyntaxError: Cannot use import statement outside a module |
默认忽略了node_modules 文件夹。
所以要进行配置。包括其他node_modules需要忽略的依赖。1
2
3transformIgnorePatterns: [
'<rootDir>/node_modules/(?!((@tarojs)|(@base/datetime)|(@base/logger))/)',
],
1 | ReferenceError: ENABLE_INNER_HTML is not defined |
看出是tarojs的api 引入出错。
所以需要mock。可以在每个文件中单独mock,或者通过映射(moduleNameMapper)统一处理。
Could not locate module @/utils/Cookie mapped as: /Users/myfolder/shop/order/src/$1.
1 | moduleNameMapper: { |
可以看到还有一些css和tarojs 的映射。