简体   繁体   English

Jest 测试在本地通过,但在 azure devops 中失败

[英]Jest test passes locally but fails in azure devops

I have a react application using react-scripts 3.2.0我有一个使用 react-scripts 3.2.0 的反应应用程序

This is my test :这是我的测试:

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');
    });
  });

And this is my function:这是我的功能:

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);
};

The test works perfectly locally but fails in Azure DevOps.该测试在本地完美运行,但在 Azure DevOps 中失败。 Although I'm installing the same dependencies in CI using npm ci .尽管我使用npm ci在 CI 中安装了相同的依赖项。

my package.json:我的 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"
  ]
}

here's the log of the failing test in CI:这是 CI 中失败测试的日志:

getContractsUsing › should perform getRequestWithDispatchUsing getContractsUsing › 应该执行 getRequestWithDispatchUsing

expect(received).toEqual(expected) // deep equality expect(received).toEqual(expected) // 深度相等

Expected: "getRequestWithDispatchUsing is success" Received: {}预期:“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)

Edit Here's my node version编辑这是我的节点版本

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'
}

For some reasons in CI the jest.mock doesn't work.由于 CI 中的某些原因, jest.mock不起作用。 I had to change the path of the package to make it work.我必须更改包的路径才能使其工作。 It's tricky cause you need to try multiple combination before making it work in CI, although all of them work locally.这很棘手,因为您需要尝试多种组合才能使其在 CI 中工作,尽管它们都在本地工作。 In my case I used:就我而言,我使用了:

jest.mock('./../shared/utils', () => ({
  __esModule: true,
  default: () => () => () => () => 'getRequestWithDispatchUsing is success',
}));

PS: I'm using Azure Devops with a windowsBuild PS:我在 windowsBuild 中使用 Azure Devops

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM