Jest测试-生成随机数的打字错误

daolsyd0  于 8个月前  发布在  Jest
关注(0)|答案(1)|浏览(104)

这就是错误。不确定我如何预测一个十进制值,以及是什么改变了测试失败的错误,我所做的唯一更改是package.json中的版本

  • 添加“eslint-plugin-jsdoc”:“^46.2.6”,
  • 更改“@emotion/styled”:“^11.9.3”和“@emotion/styled-base”:“^10.0.27”,
clients › instrumentPromise › should log on promise rejecting

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    - Expected
    + Received

      Object {
        "groupNamespace": "global",
    -   "milliseconds": 0,
    +   "milliseconds": 0.05922000000009575,
        "operationName": "name",
        "success": false,
      },

    Number of calls: 1

      64 |                 void instrumentPromise(promise, "name").catch(() => {
      65 |                     // eslint-disable-next-line jest/no-conditional-expect
    > 66 |                     expect(spy).toHaveBeenCalledWith({
         |                                 ^
      67 |                         groupNamespace: "global",
      68 |                         milliseconds: 0,
      69 |                         operationName: "name",

      at src/utils/clients/clients.test.ts:66:33

字符串
我的版本- package.json文件

"dependencies": {
        "@amzn/brazil": "*",
        "@amzn/meridian": "*",
        "@amzn/meridian-tokens": "*",
        "@amzn/santos-bluering-clickstream-client": "*",
        "@amzn/santos-bluering-webapp-context": "*",
        "@amzn/santos-clara-ruby-utils": "*",
        "@amzn/santos-meridian-tokens": "*",
        "@amzn/santos-profile-service": "*",
        "@amzn/santos-team-management-type-script-client": "*",
        "@emotion/core": "^10.3.1",
        "@emotion/styled": "^10.3.0",
        "@storybook/addon-actions": "^6.4.19",
        "@storybook/addon-essentials": "^6.4.19",
        "@storybook/addon-links": "^6.4.19",
        "@storybook/manager-webpack5": "^6.4.19",
        "@storybook/node-logger": "^6.4.19",
        "@storybook/react": "^6.4.19",
        "aws-amplify": "4.3.17",
        "aws-sdk": "^2.1096.0",
        "babel-loader": "8.2.3",
        "cognito-jwt-token-validator": "1.0.0",
        "date-fns": "^2.28.0",
        "emotion": "^10.0.27",
        "i18next": "^21.6.14",
        "qrcode.react": "^1.0.0",
        "query-string": "^7.1.1",
        "react": "^17.0.2",
        "react-dom": "^17.0.2",
        "react-helmet": "^6.1.0",
        "react-i18next": "^11.16.1",
        "react-query": "^3.39.0",
        "react-router-dom": "^5.3.0",
        "use-copy": "^0.0.2"
    },
    "devDependencies": {
        "@amzn/brazil": "*",
        "@amzn/browserslist-config-santos-bluering": "*",
        "@amzn/eslint-plugin-santos-bluering": "*",
        "@amzn/santos-bluering-common-config": "*",
        "@amzn/santos-ui-components": "^1.0.0",
        "@amzn/string-localization-validator": "*",
        "@babel/core": "^7.17.8",
        "@babel/eslint-parser": "^7.17.0",
        "@babel/preset-env": "^7.16.11",
        "@babel/preset-react": "^7.16.7",
        "@babel/preset-typescript": "^7.16.7",
        "@emotion/babel-plugin": "^11.10.5",
        "@emotion/styled": "^11.9.3",
        "@emotion/styled-base": "^10.0.27",
        "@pmmmwh/react-refresh-webpack-plugin": "^0.5.4",
        "@storybook/addon-a11y": "^6.4.19",
        "@storybook/addon-essentials": "^6.2.8",
        "@storybook/addon-links": "^6.2.8",
        "@storybook/builder-webpack5": "^6.4.19",
        "@storybook/react": "^6.2.8",
        "@testing-library/jest-dom": "^5.16.2",
        "@testing-library/react": "^12.1.4",
        "@testing-library/react-hooks": "^7.0.2",
        "@testing-library/user-event": "^13.5.0",
        "@types/jest": "^27.4.1",
        "@types/qrcode.react": "^1.0.2",
        "@types/react": "^17.0.3",
        "@types/react-dom": "^17.0.13",
        "@types/react-helmet": "^6.1.5",
        "@types/react-router-dom": "^5.3.2",
        "@typescript-eslint/eslint-plugin": "^5.15.0",
        "@typescript-eslint/parser": "^5.15.0",
        "babel-loader": "^8.2.2",
        "babel-plugin-emotion": "^10.2.2",
        "cookie-parser": "^1.4.6",
        "core-js": "^3.21.1",
        "cspell": "^5.19.2",
        "css-loader": "^6.7.1",
        "css-minimizer-webpack-plugin": "^3.4.1",
        "deepmerge": "^4.2.2",
        "dotenv": "^16.0.0",
        "dotenv-webpack": "^7.1.0",
        "eslint": "^8.11.0",
        "eslint-plugin-import": "^2.25.4",
        "eslint-plugin-jest": "^26.1.1",
        "eslint-plugin-jsdoc": "^46.2.6",
        "eslint-plugin-jsx-a11y": "^6.5.1",
        "eslint-plugin-react": "^7.29.4",
        "eslint-plugin-react-hooks": "^4.3.0",
        "eslint-plugin-testing-library": "^5.1.0",
        "file-loader": "^6.2.0",
        "fork-ts-checker-webpack-plugin": "^7.2.1",
        "html-webpack-plugin": "^5.5.0",
        "husky": "^7.0.4",
        "identity-obj-proxy": "^3.0.0",
        "jest": "^27.5.1",
        "lint-staged": "^12.3.7",
        "mini-css-extract-plugin": "^2.6.0",
        "mini-svg-data-uri": "^1.4.4",
        "node-polyfill-webpack-plugin": "^1.1.4",
        "npm-check-updates": "^12.5.4",
        "postcss": "^8.4.12",
        "postcss-loader": "^6.2.1",
        "postcss-preset-env": "^7.4.2",
        "prettier": "^2.5.1",
        "react-hooks-testing-library": "^0.6.0",
        "react-refresh": "^0.11.0",
        "sass": "^1.49.9",
        "sass-loader": "^12.6.0",
        "style-loader": "^3.3.1",
        "terser-webpack-plugin": "^5.3.1",
        "typescript": "^4.6.2",
        "typescript-plugin-css-modules": "^3.4.0",
        "url-loader": "^4.1.1",
        "webpack": "^5.70.0",
        "webpack-assets-manifest": "^5.1.0",
        "webpack-bundle-analyzer": "^4.5.0",
        "webpack-cli": "^4.9.2",
        "webpack-dev-server": "4.x",
        "webpack-merge": "^5.8.0"
    }

bpzcxfmw

bpzcxfmw1#

如果我理解正确的话,问题是你得到了一个你无法预测的毫秒值,而且之前是0?(不知道为什么你认为这是一个TS问题)。
解决这个问题最简单的方法是期望任何毫秒值:

64 |                 void instrumentPromise(promise, "name").catch(() => {
      65 |                     // eslint-disable-next-line jest/no-conditional-expect
      66 |                     expect(spy).toHaveBeenCalledWith({
         |                                 
      67 |                         groupNamespace: "global",
      68 |                         milliseconds: expect.any(Number), // Just expect any number, you cannot predict random decimals. Or round down, and expect 0, if the result matters for what you are trying to test. but I assume it is something like elapsed time?
      69 |                         operationName: "name",

字符串
当然,最好是弄清楚为什么毫秒是0,而现在是一个数字。但是没有上下文很难帮助你。你可以查看你更新的库中发生了什么变化。逐个更新它们,看看是什么导致了行为的变化,然后检查他们的git中的变化,看看是什么导致了问题。

相关问题