簡體   English   中英

Jest 測試在本地通過,但在 azure devops 中失敗

[英]Jest test passes locally but fails in 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 中失敗。 盡管我使用npm ci在 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM