标签: babel-jest

什么可以影响测试的速度与Jest?

Jest在我的环境中工作,但我相当确定它的运行速度比它应该的慢.

目前,我正在运行从Jest intro教程中复制的2个测试.一个是简单的求和函数,另一个是使用的简单复选框示例Enzyme.它们平均需要大约10秒直到它们开始运行(显示Determining test suites to run...该时间),然后从该点开始大约另外10秒(一旦计时器启动).对于这样简单的组件来说,这似乎太荒谬了......

我尝试设置我testEnvironmentnode加快速度,但它并没有取得多大的差别.

因此:

1)这是正常的吗?

2)如果不是 - 这会导致什么样的Jest减速?

一些潜在因素?

1)我正在运行一个较大的范围内这些测试React包含〜20左右反应的组分,但它们都没有被通过任何测试的引用的项目-所以我不认为他们应该影响到这一点.= /

2)我正在使用fileName.test.js命名系统来Jest了解测试文件的位置,而不是__tests__按照教程将所有内容放在文件夹中.

unit-testing reactjs jestjs enzyme babel-jest

6
推荐指数
0
解决办法
748
查看次数

jest:测试套件无法运行,TypeError:无法读取undefined的属性'bind'

我们使用反应原生的单元测试中的jest.它运作良好.我们刚刚删除了npm并再次安装它,然后当我们尝试运行单元测试(npm测试)时,我们遇到了以下错误,我们无法运行测试用例.

在此输入图像描述

可能出了什么问题.

jestjs react-native babel-jest

6
推荐指数
1
解决办法
4126
查看次数

用React Native来解决问题

我在尝试运行测试用例代码时遇到错误.我正在使用本机与jest进行反应.在升级0.40之前一切正常.现在是0.42我的所有测试用例都停止工作并且出现错误跟踪错误.

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React, { Component, Children, PropTypes } from 'react';
                                                                                             ^^^^^^
    SyntaxError: Unexpected token import

      at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
      at Object.<anonymous> (node_modules/react-native-root-siblings/lib/AppRegistryInjection.js:3:22)
      at Object.<anonymous> (node_modules/react-native-root-siblings/lib/SiblingsManager.js:3:27)
Run Code Online (Sandbox Code Playgroud)

这是我的.babelrc代码

{
    "presets": [
        "react-native"
    ],
    "plugins": [
        "transform-decorators-legacy"
    ]
}
Run Code Online (Sandbox Code Playgroud)

什么是我没有得到的问题.

javascript reactjs jestjs react-native babel-jest

6
推荐指数
1
解决办法
1400
查看次数

Jest - React组件中的模拟胖箭头函数

鉴于我的组件和测试如下,为什么我的confirmClickHandler方法在我运行测试时仍然被调用?

注意:我注意到当我将方法从胖箭头函数更改为常规函数时,它会被正确地模拟出来.我在这里错过了什么?

class CalendarConfirmation extends React.Component {
  ...

  confirmClickHandler = (e) =>  {
  ...
  }
}
Run Code Online (Sandbox Code Playgroud)

和我的测试:

import React from 'react';
import {mount} from 'enzyme';
import CalendarConfirmation from '../components/CalendarConfirmation';

describe('Test CalendarConfirmation', () => {
  let calendarConfirmation;
  calendarConfirmation = mount (<CalendarConfirmation />);
  calendarConfirmation.instance().confirmClickHandler = jest.fn();
  ...
}
Run Code Online (Sandbox Code Playgroud)

javascript reactjs jestjs babel-jest

6
推荐指数
1
解决办法
2403
查看次数

如何使用特定的webpack配置文件来运行Jest?

我正在尝试使用Jest编写测试并导入es6模块.我目前正在使用webpack3和jest 21.0.2.我的测试代码以

import Const from 'Util/Const';
Run Code Online (Sandbox Code Playgroud)

这会引发错误"SyntaxError:Unexpected token import".我有用于开发和生产构建的development.webpack.config.js和production.webpack.config.js文件.webpack.config.js文件基本上读取env变量并确定要引用的文件.

下面的代码是development.webpack.config.js的一部分,用于处理此错误:

env: {
    test: {
        presets:['react',["es2015"]],
        plugins: ["transform-es2015-modules-commonjs", "dynamic-import-node"]
    }
}
Run Code Online (Sandbox Code Playgroud)

看起来当我运行npm test时,Jest并没有引用development.webpack.config.js.有没有办法在运行Jest时设置特定的webpack配置文件?

webpack jestjs babel-jest

6
推荐指数
0
解决办法
252
查看次数

玩笑无限期挂起,不进行任何测试

每次我开玩笑时,它什么也不会运行。我让柜台任意高。我用--no-cache开玩笑

jest --debug输出如下:

{
  "configs": [
    {
      "automock": false,
      "browser": false,
      "cache": true,
      "cacheDirectory": "/var/folders/7v/64n1tsk11zs2pbwf5bm_c9kc0000gn/T/jest_dx",
      "clearMocks": false,
      "coveragePathIgnorePatterns": [
        "/node_modules/"
      ],
      "detectLeaks": false,
      "forceCoverageMatch": [],
      "globals": {},
      "haste": {
        "defaultPlatform": "ios",
        "platforms": [
          "android",
          "ios",
          "native"
        ],
        "providesModuleNodeModules": [
          "react-native"
        ]
      },
      "moduleDirectories": [
        "node_modules"
      ],
      "moduleFileExtensions": [
        "js",
        "json",
        "jsx",
        "node"
      ],
      "moduleNameMapper": [
        [
          "^React$",
          "/Users/skilurus/github/flock-react-app/node_modules/react"
        ]
      ],
      "modulePathIgnorePatterns": [
        "/Users/skilurus/github/flock-react-app/node_modules/react-native/Libraries/react-native/"
      ],
      "name": "b29a126b130a0be47202d3bc7b00f1b4",
      "resetMocks": false,
      "resetModules": false,
      "restoreMocks": false,
      "rootDir": "/Users/skilurus/github/flock-react-app",
      "roots": [
        "/Users/skilurus/github/flock-react-app"
      ],
      "runner": "jest-runner", …
Run Code Online (Sandbox Code Playgroud)

testing node.js npm jestjs babel-jest

6
推荐指数
6
解决办法
2927
查看次数

Jet with Babel ^ 7.0.0-beta.51问题

我按照你推荐我的一切,让Babel 7与Jest一起工作.我安装了@babel/corebabel-core.仍然没有用.

{
  "devDependencies": {
    "@babel/cli": "^7.0.0-beta.51",
    "@babel/core": "^7.0.0-beta.51",
    "@babel/node": "^7.0.0-beta.51",
    "@babel/plugin-transform-modules-commonjs": "^7.0.0-beta.51",
    "@babel/preset-flow": "^7.0.0-beta.51",
    "babel-core": "^7.0.0-beta.51",
    "jest-cli": "^23.1.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

yarn jest

yarn run v1.9.0-20180612.1255
$ /Users/m/Desktop/k/node_modules/.bin/jest
 FAIL  packages/services/src/Client/__tests__/Client.js
  - Test suite failed to run

    Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this …
Run Code Online (Sandbox Code Playgroud)

babel jestjs babel-jest yarnpkg

6
推荐指数
1
解决办法
1491
查看次数

React Native - Jest:自更新到0.56后损坏.怎么解决?

自从升级到新的React Native版本后,我的测试就被打破了.

环境

这是我的环境:

React Native Environment Info:
System:
  OS: macOS High Sierra 10.13.4
  CPU: x64 Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz
  Memory: 486.81 MB / 16.00 GB

  Shell: 3.2.57 - /bin/bash
Binaries:
  Node: 9.4.0 - /usr/local/bin/node
  npm: 6.1.0 - /usr/local/bin/npm
  Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
  iOS SDK:
    Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3
IDEs:
  Android Studio: 3.1 AI-173.4697961
  Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild
npmPackages:
  react: 16.4.1 => 16.4.1
  react-native: 0.56.0 => 0.56.0
npmGlobalPackages:
  create-react-native-app: 1.0.0
  react-native-cli: 2.0.1 …
Run Code Online (Sandbox Code Playgroud)

babel reactjs jestjs react-native babel-jest

6
推荐指数
2
解决办法
3468
查看次数

找不到转换选项中的模块&lt;rootDir&gt; / node_modules / vue-jest

我遇到了错误,需要您的帮助。感谢您收看这个问题。

我的情况:我正在为我的项目配置Drone CI工具,当我在drone.yml上运行单元测试时得到了这个。

Validation Error:

  Module <rootDir>/node_modules/vue-jest in the transform option was not found.

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html
Run Code Online (Sandbox Code Playgroud)

这是我的 jest.conf.js

transform: {
    "^.+\\.js$": "babel-jest",
    ".*\\.vue$": "<rootDir>/node_modules/vue-jest"
  },
Run Code Online (Sandbox Code Playgroud)

我尝试过的

  1. 删除<rootDir>/node_modules/。但是我又犯了一个错误Module vue-jest in the transform option was not found.。所以我认为这不是正确的解决方案

unit-testing vue.js jestjs babel-jest vuejs2

6
推荐指数
2
解决办法
829
查看次数

玩笑+打字稿+ es6模块(再次,2019)-SyntaxError:意外的令牌导出

我仍然在尝试使用jest,typescript和es6模块时遇到问题。我的测试是用Typescript编写的,我正在尝试从使用es6模块的js文件中导入对象。为此,我遵循了jest文档,并在位于项目根目录下的babel.config.js中包含以下内容:

module.exports = {
presets: [
    [
        '@babel/preset-env',
        {
            targets: {
                node: 'current'
            }
        }
    ]
]
Run Code Online (Sandbox Code Playgroud)

};

我安装了以下依赖项:

"devDependencies": {
  "@babel/core": "^7.2.2",
  "@babel/preset-env": "^7.3.1",
  "@types/jest": "^23.3.13",
  "@types/node": "^10.12.18",
  "babel-jest": "^24.1.0",
  "jest": "^23.6.0",
  "rollup": "^1.1.2",
  "rollup-plugin-terser": "^4.0.4",
  "rollup-plugin-typescript2": "^0.19.2",
  "ts-jest": "^23.10.5",
  "ts-node": "^8.0.1",
  "typescript": "^3.2.4"
}
Run Code Online (Sandbox Code Playgroud)

在jest.config.js中,我有以下内容:

module.exports = {
verbose: true,
transform: {
    "^.+\\.jsx?$": "babel-jest",
    '^.+\\.ts?$': 'ts-jest'
},
testEnvironment: 'node',
testRegex: '/test/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};
Run Code Online (Sandbox Code Playgroud)

最后,我想在测试中像这样导入:

import {Dag, Directions} from "../dist/dag";
Run Code Online (Sandbox Code Playgroud)

它说:SyntaxError:意外的令牌导出 …

javascript typescript jestjs babel-jest

6
推荐指数
1
解决办法
2216
查看次数