【问题标题】:Telling Jest to not mock /moment(-timezone)?/ not working告诉 Jest 不要模拟 /moment(-timezone)?/ 不工作
【发布时间】:2018-02-15 15:03:59
【问题描述】:

在任何时候调用 moment/moment.tz() 时遇到错误,TypeError: (0 , _moment2.default)(...).tz is not a function 被抛出。下面是我的玩笑配置,绝对路径已编辑。

{
  "configs": [
    {
      "automock": false,
      "browser": false,
      "cache": true,
      "cacheDirectory": "<absolute path>",
      "clearMocks": true,
      "coveragePathIgnorePatterns": [
        "/node_modules/"
      ],
      "globals": {
        "window": true
      },
      "haste": {
        "providesModuleNodeModules": []
      },
      "moduleDirectories": [
        "node_modules",
        "src"
      ],
      "moduleFileExtensions": [
        "js",
        "vue"
      ],
      "moduleNameMapper": [
        [
          "^@/(.*)$",
          "/<absolute path>/src/$1"
        ],
        [
          "\\.(css|less)$",
          "/<absolute path>/test/__mocks__/styleMock.js"
        ]
      ],
      "modulePathIgnorePatterns": [],
      "name": "79986797be68f3008e950f2cefe0bbd1",
      "resetMocks": false,
      "resetModules": false,
      "rootDir": "<absolute path>",
      "roots": [
        "<absolute path>/src",
        "<absolute path>/test"
      ],
      "runner": "jest-runner",
      "setupFiles": [],
      "snapshotSerializers": [
        "/<absolute path>/node_modules/jest-serializer-html/index.js"
      ],
      "testEnvironment": "jest-environment-jsdom",
      "testMatch": [
        "**/__tests__/**/*.js?(x)",
        "**/?(*.)(spec|test).js?(x)"
      ],
      "testPathIgnorePatterns": [
        "node_modules/",
        "test/e2e/"
      ],
      "testRegex": "",
      "testRunner": "/<absolute path>/node_modules/jest-jasmine2/build/index.js",
      "testURL": "about:blank",
      "timers": "real",
      "transform": [
        [
          ".*\\.(vue)$",
          "/<absolute path>/node_modules/vue-jest/vue-jest.js"
        ],
        [
          ".*\\.js$",
          "/opt/cm/cm_frontend/cm/node_modules/babel-jest/build/index.js"
        ]
      ],
      "transformIgnorePatterns": [
        "/node_modules/"
      ],
      "unmockedModulePathPatterns": [
        "moment",
        "moment-timezone"
      ],
      "watchPathIgnorePatterns": []
    }
  ],
  "globalConfig": {
    "bail": false,
    "changedFilesWithAncestor": false,
    "collectCoverage": true,
    "collectCoverageFrom": [
      "src/components/**/*.{js,vue}",
      "src/mixins/**/*.js",
    ],
    "coverageDirectory": "<ABSOLUTE PATH>",
    "coverageReporters": [
      "json",
      "text",
      "lcov",
      "clover"
    ],
    "expand": false,
    "listTests": false,
    "mapCoverage": true,
    "maxWorkers": 7,
    "noStackTrace": false,
    "nonFlagArgs": [],
    "notify": false,
    "rootDir": "<ROOT PATH HERE>",
    "runTestsByPath": false,
    "testFailureExitCode": 1,
    "testPathPattern": "",
    "testResultsProcessor": null,
    "updateSnapshot": "new",
    "useStderr": false,
    "verbose": false,
    "watch": false,
    "watchman": true
  },
  "version": "21.2.1"
}

我试过把玩笑告诉jest.dontMock()jest.unmock(),但无济于事。

我也试过require.requireActual()

我只想模拟使用jest.mock() 指定每个测试(每个文件)的时刻。

什么可能导致这些问题?我不是在我的测试文件中的任何地方嘲笑或取消嘲笑时刻。我只是简单地使用 @vue/test-utils 来模拟整个计算属性,同时使用 moment 来解决这个问题。

【问题讨论】:

  • 无法检查本地 rn,但是您如何在测试中导入时刻?
  • @LuisOrduz 我很确定我已经尝试过了,但不,我目前没有导入它。这是这样做的标准方法吗?
  • 你为什么不尝试导入它,看看会发生什么?
  • @LuisOrduz 我会的。但这可能是由于缺少一些基本的东西。我想我不需要在我的测试中导入它,因为我的源导入了它,而且我没有在测试中使用时刻,当我到达我的电脑时我会尝试。谢谢
  • @LuisOrduz 您可以正式回答,以便我奖励赏金。傻我。

标签: unit-testing vue.js jestjs


【解决方案1】:

您可以尝试直接在测试中导入它们。另外,我检查了问题和其他问题,似乎导入timezone实际上并没有将tz函数添加到moment,所以解决它的方法可能是:

import moment from "moment-timezone"

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-04-20
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 2021-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多