javascript - Jest 测试在本地通过,但在 azure devops 中失败
问题描述
我有一个使用 react-scripts 3.2.0 的反应应用程序
这是我的测试:
jest.mock('./../shared/utils/getRequestWithDispatchUsing', () => ({
__esModule: true,
default: () => () => () => () => 'getRequestWithDispatchUsing is success',
}));
describe('getContractsUsing', () => {
const dispatch = jest.fn(action => action);
const fetch = jest.fn();
const clientNumber = 'clientNumber';
beforeEach(() => {
jest.clearAllMocks();
});
it('should perform getRequestWithDispatchUsing', () => {
const actualContract = getContractsUsing(fetch, clientNumber)(dispatch);
expect(actualContract).toEqual('getRequestWithDispatchUsing is success');
});
});
这是我的功能:
export const getContractsUsing = (fetch, clientNumber) => dispatch => {
const apiContractsUrl = getContractsApiUrl();
const body = {
customerNumber: clientNumber,
};
return getRequestWithDispatchUsing(fetch)({
url: apiContractsUrl,
body,
featureName: SELECT_CONTRACTS_NAVIGATION,
})(dispatch)(contractsRequested, contractsReceived, contractsRequestFailed);
};
该测试在本地完美运行,但在 Azure DevOps 中失败。尽管我在 CI 中使用npm ci
.
我的 package.json:
{
"name": "app",
"version": "2.3.1",
"private": true,
"jest": {
"collectCoverageFrom": [
"**/*.{js,jsx}",
"!**/index.{js,jsx}",
"!**/*.{stories.js,demo.js}",
"!**/build/**",
"!**/mocks/**",
"!**/coverage/**",
"!**/node_modules/**",
"!src/setupTests.js"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
},
"dependencies": {
"analytics-js": "^0.2.0",
"classnames": "^2.2.5",
"currency-formatter": "^1.4.2",
"lodash": "^4.17.15",
"moment": "^2.22.1",
"mw.validation": "^1.0.9",
"prop-types": "^15.6.1",
"raf": "^3.4.0",
"react": "^16.12.0",
"react-scripts": "~3.2.0",
"react-app-polyfill": "^1.0.5",
"react-cookies": "^0.1.0",
"react-datepicker": "~1.4.0",
"react-dom": "^16.12.0",
"react-moment-proptypes": "^1.5.0",
"react-redux": "^5.0.5",
"react-router": "^4.2.0",
"react-router-dom": "^4.1.1",
"react-scroll": "^1.7.9",
"react-transition-group": "^2.2.1",
"recompose": "^0.26.0",
"redux": "^3.6.0",
"redux-persist": "^5.9.1",
"redux-thunk": "^2.2.0",
"url-search-params-polyfill": "^4.0.0"
},
"devDependencies": {
"@storybook/addon-actions": "^5.0.0",
"@storybook/addon-links": "^5.0.0",
"@storybook/addons": "^5.0.0",
"@storybook/react": "^5.0.0",
"bluebird": "^3.5.2",
"concurrently": "^3.5.0",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.15.1",
"enzyme-to-json": "^3.4.3",
"eslint-config-airbnb": "^15.1.0",
"eslint-config-prettier": "2.6.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-prettier": "^2.3.1",
"eslint-plugin-react": "^7.5.1",
"husky": "^0.14.3",
"istanbul": "^0.4.5",
"jest-fetch-mock": "^2.1.2",
"lint-staged": "^4.3.0",
"node-sass": "^4.9.2",
"prettier": "^1.8.2",
"redux-mock-store": "^1.5.4",
"webpack-bundle-analyzer": "^3.3.2"
},
"scripts": {
"test": "react-scripts test --env=jsdom",
"test-coverage": "react-scripts test --env=jsdom --coverage --watchAll=false",
},
"eslintConfig": {
"extends": "react-app"
},
"engines": {
"node": ">=12.0.0",
"npm": ">=6.0.0"
},
"browserslist": [
">0.2%",
"ie 11",
"not dead",
"not op_mini all"
]
}
这是 CI 中测试失败的日志:
getContractsUsing › 应该执行 getRequestWithDispatchUsing
expect(received).toEqual(expected) // 深度相等
预期:“getRequestWithDispatchUsing 成功”收到:{}
183 | it('should perform getRequestWithDispatchUsing', () => { 184 | const actualContract = getContractsUsing(fetch, clientNumber)(dispatch); 185 | expect(actualContract).toEqual('getRequestWithDispatchUsing is success'); | ^ 186 | }); 187 | }); 188 | }); at Object.<anonymous> (src/SelectContracts/SelectContracts.actions.spec.js:185:30)
编辑这是我的节点版本
node -p process.versions
{
node: '12.14.0',
v8: '7.7.299.13-node.16',
uv: '1.33.1',
zlib: '1.2.11',
brotli: '1.0.7',
ares: '1.15.0',
modules: '72',
nghttp2: '1.39.2',
napi: '5',
llhttp: '1.1.4',
http_parser: '2.8.0',
openssl: '1.1.1d',
cldr: '35.1',
icu: '64.2',
tz: '2019c',
unicode: '12.1'
}
解决方案
由于 CI 中的某些原因,jest.mock
它不起作用。我必须更改包的路径才能使其正常工作。这很棘手,因为您需要尝试多种组合才能使其在 CI 中工作,尽管它们都在本地工作。就我而言,我使用了:
jest.mock('./../shared/utils', () => ({
__esModule: true,
default: () => () => () => () => 'getRequestWithDispatchUsing is success',
}));
PS:我正在使用带有 windowsBuild 的 Azure Devops
推荐阅读
- python - 如何使用 Python 在 Google Drive 上下载目录?
- c# - 我如何对 Fulfillment Messages 进行交互以将消息逐个返回给客户端
- python - 如何直接从云存储桶加载 tf.keras 模型?
- c++ - 为什么我不能在主函数之外定义一个类的对象(它继承了另一个类)?
- machine-learning - PyTorch 神经网络不学习
- sql - 如何从 SQL Server 中的同一组中获取第一条和最后一条记录?
- python - 用于在日期时间上执行基本算术的 python 脚本
- php - 合并多个 POST 数组
- swift - 如何从 Firebase 存储中检索配置文件图像
- python - Spyder 4:自动完成从错误的自定义模块中建议功能