Tag: jestjs

用Jest重置单个模块

jest.resetModules()清除所有模块的requirecaching,但有没有一种方法来清除它只是一个单一的模块? 因为Jest似乎绕过它,所以不能使用require.cache 。 我正在testing一个有状态的Node模块(即它依赖于多个调用require返回相同实例的事实)。 对于我的testing,我需要重置模块的状态来testing不同的场景。 jest.resetModules()作品,但我需要重新require一些其他嘲笑的模块,不需要重置。

使用Jest CLI时如何获得承诺?

我试图用Jest CLI来testing一个承诺,这个代码在浏览器中运行时应该执行。 不过,我想开始为它编写testing。 class ListCollection { constructor() { this.items = new Array(); } addItem(string) { const addItemPromise = new Promise( function (resolve, reject) { // set up async getting like a XMLHttpRequest setTimeout( () => { this.items.push(string); resolve(string); }.bind(this), 2000); }.bind(this) ); return addItemPromise; } } 目前我正试图让这个非常基本的testing工作。 我按照与茉莉花坑相关的文件进行testing。 jest.dontMock('../collections'); import Collection from '../collections'; describe("Collection", () […]

Jest:运营商和运营商之间的差异1

什么时候使用每个–runInBand或 – –maxWorkers 1选项是合适的? 如果我的意图是按顺序依次运行所有testing(按顺序一个一个地进行),那么哪个才是正确的select? 额外的细节: 我正在使用Jesttesting一个NodeJs express应用程序,集成testing通过supertest敲击HTTP端点。 这可能对答案没有任何影响,只是在相关的情况下提及。 这里是Jest CLI参考: https://facebook.github.io/jest/docs/cli.html 相关部分: –maxWorkers=<num> 别名:-w。 指定工作池为运行testing而产生的最大工作者数量。 这默认为您的机器上可用的内核数量。 在CI等资源有限的环境中进行调整可能是有用的,但是对于大多数使用情况,默认值应该是足够的。 –runInBand 别名:-i。 在当前进程中连续运行所有testing,而不是创build运行testing的subprocess的工作池。 这对debugging很有用。

React组件在浏览器中正确渲染,但Jesttesting时出现错误:“只有ReactOwner可以引用”

我在React中有两个组件,它们在浏览器中呈现得很好,并产生预期的行为,但是在通过Jest运行testing时似乎无法呈现。 descriptions.js var React = require('react/addons'); var $ = require('jquery'); var Description = require('./description.js'); var Descriptions = React.createClass({ getInitialState: function () { //container always starts with at least one description field that is empty, or whatever is contained in props var descriptions = []; if (this.props.info == null) { descriptions.push({num: 0, data: ''}); } else […]

使用Jesttesting失败,错误:观察文件更改时出错:EMFILE

我试图编写一个React.js应用程序的testing。 一切都很顺利,但使目录跟踪Git(做了一个Git回购)。 testing失败,下面的错误 2017-01-15 05:05 node[1278] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22) 2017-01-15 05:05 node[1278] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22) events.js:160 throw er; // Unhandled 'error' event ^ Error: Error watching file for changes: EMFILE at exports._errnoException (util.js:1022:11) at FSEvent.FSWatcher._handle.onchange (fs.js:1406:11) 我相信这是因为.git目录,因为当我删除.git目录时,它正在运行没有错误。 似乎在观看文件时发生exception。 我的开发环境是MacOS 10.12.2和Node 6.9.4。 我该如何解决这个问题?

为什么在使用笑话testing节点应用程序时看不到控制台日志输出

我是新玩笑testing,我似乎无法看到模块我想testing的控制台输出。 我-module.js: var _ = require('underscore'); exports.filter = function(data) { if(_.isArray(data)) { console.log("Data is: " + data); data = data[0]; } return data; } 我的模块,test.js: jest.dontMock('../my-module'); var testdata = [{label: "test"}, {id: 5}]; describe('test my module', function(){ it('changes some data' , function(){ var transformedData = require('../my-module').filter(testdata); expect(transformedData).toBe(testdata[0]); }); }); 为什么在“my-module.js”中input我的console.log输出?

NodeJS – 在process.oncallback中testingunit testingsetTimeout

我正在尝试使用Jest在我的process.on('SIGTERM')callback中testing一个计时器,但是它似乎永远不会被调用。 我正在使用jest.useFakeTimers() ,虽然它似乎将setTimeout调用模拟到一个范围,但它不会在检查setTimeout.mock对象时结束。 我的index.js文件: process.on('SIGTERM', () => { console.log('Got SIGTERM'); setTimeout(() => { console.log('Timer was run'); }, 300); }); setTimeout(() => { console.log('Timer 2 was run'); }, 30000); 和testing文件: describe('Test process SIGTERM handler', () => { test.only('runs timeout', () => { jest.useFakeTimers(); process.exit = jest.fn(); require('./index.js'); process.kill(process.pid, 'SIGTERM'); jest.runAllTimers(); expect(setTimeout.mock.calls.length).toBe(2); }); }); 并且testing失败: 预期值为(使用===):2收到:1,控制台日志输出为: console.log […]

Jest,Typescript,ts-jest:覆盖率稍微不正确

我正在使用TypeScript,Jest和ts-jest NPM模块来编写项目。 当我运行我的testing,我得到了一些覆盖面,但HTML报告是不正确的: 此外,一些function即使被称为是未经testing也被标记为未经testing。 我的package.json设置如下: { "jest": { "transform": { ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js" }, "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$", "moduleFileExtensions": [ "ts", "tsx", "js" ], "testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js", "collectCoverage": true, "collectCoverageFrom": [ "src/**/*.{ts,tsx}" ], "coverageReporters": [ "html", "json" ] } } 我的configuration有问题吗?

所有的testing都运行完毕之后再进行清理

是否有可能在Jest运行完成所有其他testing后运行的清理或拆卸任务? 与setupFiles允许在运行任何testing之前设置任务类似。 如果这也可以运行,不pipetesting是否有任何错误,奖励点。 将afterAll(() => {})置于文件顶层(在任何describe函数之外)时,只会在完成特定文件的testing后才运行。 这个用例是我有许多testing文件将在一个开发数据库中创build用户,我不想让每个testing文件负责清理和删除用户。 编写testing时也可能发生错误,所以如果清理发生而不pipe错误是否会更好。

Jest:找不到模块内部需要testing的模块(相对path)

我有这个组件: import React from 'react'; import VideoTag from './VideoTag'; import JWPlayer from './JWPlayer'; class VideoWrapper extends React.Component { //… component code } 基于一些逻辑呈现另一个组件(VideoTag或JWPlayer),但当我试图在一个笑话文件中testing它,我得到的错误: 找不到模块'./VideoTag' 这三个组件都在同一个目录下,这就是为什么当我在浏览器中运行它并在运行时看到它实际工作的原因,但看起来Jest在解决这些相对path时遇到了问题,这是一个笑话文件: jest.dontMock('../src/shared/components/Video/VideoWrapper.jsx'); import React from 'react'; import ReactDOM from 'react-dom'; TestUtils from 'react-addons-test-utils'; import VideoWrapper from '../src/shared/components/Video/VideoWrapper.jsx'; describe('VideoWrapper tests', () => { it('Render JWPlayer', () => { let vWrapper = TestUtils.renderIntoDocument( […]