我Jest在我的环境中工作,但我相当确定它的运行速度比它应该的慢.
目前,我正在运行从Jest intro教程中复制的2个测试.一个是简单的求和函数,另一个是使用的简单复选框示例Enzyme.它们平均需要大约10秒直到它们开始运行(显示Determining test suites to run...该时间),然后从该点开始大约另外10秒(一旦计时器启动).对于这样简单的组件来说,这似乎太荒谬了......
我尝试设置我testEnvironment要node加快速度,但它并没有取得多大的差别.
因此:
1)这是正常的吗?
2)如果不是 - 这会导致什么样的Jest减速?
一些潜在因素?
1)我正在运行一个较大的范围内这些测试React包含〜20左右反应的组分,但它们都没有被通过任何测试的引用的项目-所以我不认为他们应该影响到这一点.= /
2)我正在使用fileName.test.js命名系统来Jest了解测试文件的位置,而不是__tests__按照教程将所有内容放在文件夹中.
我在尝试运行测试用例代码时遇到错误.我正在使用本机与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)
什么是我没有得到的问题.
鉴于我的组件和测试如下,为什么我的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) 我正在尝试使用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配置文件?
每次我开玩笑时,它什么也不会运行。我让柜台任意高。我用--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) 我按照你推荐我的一切,让Babel 7与Jest一起工作.我安装了@babel/core和babel-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) 自从升级到新的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) 我遇到了错误,需要您的帮助。感谢您收看这个问题。
我的情况:我正在为我的项目配置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)
我尝试过的
<rootDir>/node_modules/。但是我又犯了一个错误Module vue-jest in the transform option was not found.。所以我认为这不是正确的解决方案我仍然在尝试使用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:意外的令牌导出 …
babel-jest ×10
jestjs ×10
reactjs ×4
javascript ×3
react-native ×3
babel ×2
unit-testing ×2
enzyme ×1
node.js ×1
npm ×1
testing ×1
typescript ×1
vue.js ×1
vuejs2 ×1
webpack ×1
yarnpkg ×1