Jest: 使用 --coverage 标志时,一些通过的测试将失败

创建于 2016-12-08  ·  3评论  ·  资料来源: facebook/jest

您要请求功能还是报告错误

漏洞

目前的行为是什么?

执行npm test已通过所有测试; 然而,执行npm test -- --coverage会导致一些测试失败。

如果当前的行为是一个错误,请提供重现的步骤,如果可能的话,我们可以在 GitHub 上提供一个最小的存储库,我们可以npm installnpm test

我有一个项目,其中包含许多我正在使用 jest 和酶测试的反应组件。 我最近将我们从[email protected]提升到[email protected][email protected][email protected] 。 在安装较新版本之前,我清理了node_modules目录。

我运行npm test并且所有测试都通过了。 当我运行npm test -- --coverage ,一些测试失败并生成覆盖率报告。

似乎所有的失败都围绕着一个组件被酶浅渲染,然后使用find(selector)在渲染树中查找节点。 在jest期间成功找到节点,但在jest --coverage期间失败。

在升级之前, jestjest --coverage所有测试都通过了。

回购: https : //github.com/nsand/jest-coverage

什么是预期行为?

我希望两次执行的所有测试都通过。

使用--debug再次运行 Jest 并提供它打印的完整配置。

node @ v6.9.1
npm @3.10.8
OS macOS Sierra

jest version = 17.0.3
test framework = jasmine2
config = {
  "coverageDirectory": "/Users/dev/workspaces/component-project/.gh-pages/coverage",
  "moduleNameMapper": [
    [
      "^.+\\.(scss)$",
      "/Users/dev/workspaces/component-project/lib/styleMock.js"
    ]
  ],
  "rootDir": "/Users/dev/workspaces/component-project",
  "name": "-Users-dev-workspaces-component-project",
  "setupFiles": [
    "/Users/dev/workspaces/component-project/node_modules/babel-polyfill/lib/index.js"
  ],
  "testRunner": "/Users/dev/workspaces/component-project/node_modules/jest-jasmine2/build/index.js",
  "transform": [
    [
      "^.+\\.jsx?$",
      "/Users/dev/workspaces/component-project/node_modules/babel-jest/build/index.js"
    ]
  ],
  "usesBabelJest": true,
  "automock": false,
  "bail": false,
  "browser": false,
  "cacheDirectory": "/var/folders/46/446113_55dgdfk3jsptqtd2c0000gn/T/jest",
  "coveragePathIgnorePatterns": [
    "/node_modules/"
  ],
  "coverageReporters": [
    "json",
    "text",
    "lcov",
    "clover"
  ],
  "expand": false,
  "globals": {},
  "haste": {
    "providesModuleNodeModules": []
  },
  "mocksPattern": "__mocks__",
  "moduleDirectories": [
    "node_modules"
  ],
  "moduleFileExtensions": [
    "js",
    "json",
    "jsx",
    "node"
  ],
  "modulePathIgnorePatterns": [],
  "noStackTrace": false,
  "notify": false,
  "preset": null,
  "resetMocks": false,
  "resetModules": false,
  "snapshotSerializers": [],
  "testEnvironment": "jest-environment-jsdom",
  "testPathDirs": [
    "/Users/dev/workspaces/component-project"
  ],
  "testPathIgnorePatterns": [
    "/node_modules/"
  ],
  "testRegex": "(/__tests__/.*|\\.(test|spec))\\.jsx?$",
  "testURL": "about:blank",
  "timers": "real",
  "transformIgnorePatterns": [
    "/node_modules/"
  ],
  "useStderr": false,
  "verbose": null,
  "watch": false,
  "collectCoverage": true,
  "cache": false,
  "watchman": true
}
Confirmed

最有用的评论

displayName到您的组件中应该可以修复它😄

伊斯坦布尔用其他匿名函数包装函数,我们采用节点为渲染组件提供的函数名称:( -- @cpojer https://github.com/facebook/jest/issues/1824#issuecomment -250376673

@nsand :例如,您的Icon组件现在将如下所示:

import React from 'react';

const Icon = ({ name }) => (
  <i className={`fa-icon fa-icon--${name}`}></i>
);

Icon.displayName = 'Icon';

export default Icon;

所有3条评论

@dmitriiabramov关于为什么会发生这种情况的任何想法?

displayName到您的组件中应该可以修复它😄

伊斯坦布尔用其他匿名函数包装函数,我们采用节点为渲染组件提供的函数名称:( -- @cpojer https://github.com/facebook/jest/issues/1824#issuecomment -250376673

@nsand :例如,您的Icon组件现在将如下所示:

import React from 'react';

const Icon = ({ name }) => (
  <i className={`fa-icon fa-icon--${name}`}></i>
);

Icon.displayName = 'Icon';

export default Icon;

@rogeliog太棒了! 谢谢你。 之前我正在做一些调试,并注意到如果我在包装器上调用children() ,就会发现有明显的差异。

npm 测试

 [ { '$$typeof': Symbol(react.element),
           type: [Function: Icon],
           key: null,
           ref: null,
           props: [Object],
           _owner: null,
           _store: {} } ]

对比
npm 测试 -- --coverage

[ { '$$typeof': Symbol(react.element),
           type: [Function],
           key: null,
           ref: null,
           props: [Object],
           _owner: null,
           _store: {} } ]

不同之处在于 Function 以npm test命名。 所以这似乎与您发布的内容密切相关,您的提案确实解决了问题。 谢谢@thymikee和@rogeliog!

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

stephenlautier picture stephenlautier  ·  3评论

kgowru picture kgowru  ·  3评论

Antho2407 picture Antho2407  ·  3评论

Secretmapper picture Secretmapper  ·  3评论

samzhang111 picture samzhang111  ·  3评论