告诉玩笑不要嘲笑/时刻(-timezone)?/不工作

时间:2018-02-15 15:04:00

标签: unit-testing vue.js jestjs

在调用任何时刻/ moment.tz()的情况下使用jest获取错误,抛出TypeError: (0 , _moment2.default)(...).tz is not a function。下面是我的jest配置,其中包含绝对路径编辑。

{
  "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来模拟整个计算属性,同时解决这个问题。

1 个答案:

答案 0 :(得分:2)

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

import moment from "moment-timezone"
相关问题