我只想模拟模块中的单个函数(名为导出),但保持模块的其余功能完好无损。
使用jest.mock('package-name')
使所有导出的函数模拟,这是我不想要的。
我尝试将命名导出传播回模拟对象...
import * as utils from './utilities.js';
jest.mock(utils, () => ({
...utils
speak: jest.fn(),
}));
Run Code Online (Sandbox Code Playgroud)
但得到这个错误:
的模块工厂
jest.mock()
不允许引用任何范围外的变量。
我的所有jest
typescript
测试都在travis
管道中失败,并引发以下错误:
TypeError: Cannot assign to read only property 'Symbol(Symbol.toStringTag)' of object '#<process>'
Run Code Online (Sandbox Code Playgroud)
突然发生了,我没有更改代码中的任何特定内容。在本地,一切正常。
任何想法会发生什么?
我正在学习使用 Jest/Enzyme 进行单元测试 - 特别是针对 React/Redux。我正在尝试编写一个测试来从用户的角度检查(这是正确的方法吗?...)单击某个链接后是否显示模式。接下来我想测试单击shade
包装器是否modal
关闭modal
。
模态状态(打开/关闭)由 Redux 控制。
根据 Redux 的当前状态 - 模态获取适当的样式:modal
或invisibleModal
。
我尝试通过模拟 Redux store 来做到这一点,但我觉得没有它就应该完成 - 我想测试未连接的组件会更好,但如果我错了,请纠正我。
到目前为止测试:
const mockFn = jest.fn();
describe('Contact components', ()=>{
it('<ContactModal /> renders correctly', ()=>{
shallow(<ContactModal />)
})
it('<ContactForm /> renders correctly', ()=>{
shallow(<ContactForm toggleContactModal={mockFn} />)
})
});
describe('In contact form link',()=>{
const link = shallow(<ContactForm toggleContactModal={mockFn}/>)
const modalWrapper = shallow(<ContactModal />);
it('toggleContactModal function is called upon clicking ContactForm',()=>{
expect(modalWrapper.find('#modal').hasClass('modal')).toBe(false);
link.simulate('click');
expect(mockFn.mock.calls.length).toBe(1); …
Run Code Online (Sandbox Code Playgroud) 我需要从服务器代码路由器/控制器上的任何地方从服务器发出套接字.我检查了一些线程和谷歌但没有按预期工作.
app.js
var app = require('express').createServer();
var io = require('socket.io')(app);
require(./router.js)(app)
require(./socket.js)(io)
app.listen(80);
Run Code Online (Sandbox Code Playgroud)
router.js
module.exports = function (app) {
app.use('/test', require(./controller.js));
return app;
};
Run Code Online (Sandbox Code Playgroud)
socket.js
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Run Code Online (Sandbox Code Playgroud)
controller.js
var express = require('express');
var router = express.Router();
router.get('/about', function(req, res) {
// I need to emit here
});
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
这不是我正在使用的提取代码.这是我使用的结构以及我需要调用的位置.
比如说,我下面有一个对象
const obj1 = {名称:expect.any(String)}
后端返回响应如下,对象键“age”作为可选键
const 响应 = {名称:'bbb',年龄:10}
那么,如何断言 obj1 将age作为可选键,这意味着如果它存在,它必须是数字类型,如果它不存在,我们可以省略检查?
期望(响应).toMatchObject(obj1);
当我从命令行对 Angular-Libs 运行 Jest-Unit 测试时,它们工作正常。当我在单个测试中从 IntelliJ(版本 2019.1)运行它们时,它们仅在我不使用 async() 时才起作用。
这很奇怪,因为我的 angular.json 包含以下内容:
"auth": {
"root": "libs/auth",
"sourceRoot": "libs/auth/src",
"projectType": "library",
"prefix": "auth",
"architect": {
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"libs/auth/tsconfig.lib.json",
"libs/auth/tsconfig.spec.json"
],
"exclude": ["**/node_modules/**"]
}
},
"test": {
"builder": "@nrwl/builders:jest",
"options": {
"jestConfig": "libs/auth/jest.config.js",
"tsConfig": "libs/auth/tsconfig.spec.json",
"setupFile": "libs/auth/src/test-setup.ts"
}
}
},
"schematics": {
"@nrwl/schematics:component": {
"styleext": "scss"
}
}
},
Run Code Online (Sandbox Code Playgroud)
并且在test-setup.ts
(在 lib 内部)中import 'jest-preset-angular';
包含,它执行:
require('core-js/es6/reflect');
require('core-js/es7/reflect');
require('zone.js/dist/zone.js');
require('zone.js/dist/proxy.js');
require('zone.js/dist/sync-test');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
require('./zone-patch');
Run Code Online (Sandbox Code Playgroud)
在设置过程中。 …
我正在将 reduxsauce 库用于 redux 存储,并且我想对其中的单个 redux 存储进行单元测试。Redux 文件:
import { createReducer, createActions } from 'reduxsauce'
import Immutable from 'seamless-immutable'
/* ------------- Types and Action Creators ------------- */
const { Types, Creators } = createActions({
getLanguage: [],
setLanguage: ['language']
})
export const LanguageTypes = Types
export default Creators
/* ------------- Initial State ------------- */
export const INITIAL_STATE = Immutable({
language: "en"
})
/* ------------- Reducers ------------- */
export const getLanguage = (state: Object, {}: Object) => {
return state.merge({})
}
export …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个带有react-native和typescript的应用程序,并使用Jest进行测试,但是当我使用作用域包(@assets)时遇到问题,Jest无法找到路径并给出错误。
目录结构如下所示:
project/
assets/
img/
foo.png
package.json
src/
Foo.ts
build/
Foo.js
// assets/package.json
{
"name": "@assets" // My @assets scope
}
// build/Foo.js
const image = require('@assets/img/foo.png'); // <- error in Jest
Run Code Online (Sandbox Code Playgroud)
因此,当我开玩笑时:
npm run jest build /
它找不到'@ assets / img / foo.png'并抛出错误:
从'Foo.js'中找不到模块'@ assets / img / logo.png'
如何在Jest中使用范围包?
开玩笑的版本:20.0.4
谢谢
我有一个包含步骤的向导,每个步骤都有自己的验证(同步/异步)。
例如:
<Wizard>
<Form1 />
<Form2 />
<Form3 />
</ Wizard>
Run Code Online (Sandbox Code Playgroud)
每个表单都包含onContinue
验证表单输入的方法。
onContinue = async () => {
let step = this.steps[this.state.step];
let validation = await step.validate();
// check for error and change step number.
this.changeStep(this.state.step + 1, validation);
};
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试测试向导的行为,方法是确保单击“继续”时步骤编号增加1。
it('should set the state property "step" to 1 after successfully clicking the continue button', () => {
const props = {
id: 'testId',
children: noErrorChildren
};
const wizard= mount(<Wizard {...props} />);
tree.find('onContinue-button').simulate('click');
expect(wizard.state().step).toEqual(1);
});
Run Code Online (Sandbox Code Playgroud)
运行测试后,出现如下错误: …
我正在使用travis-ci部署我的开源项目。自从我的代码没有更改以来,自昨天以来,我所有的构建都无法运行单元测试,并出现下一个错误:
~/w/some-package ??? yarn test
yarn run v1.13.0
$ jest
FAIL test/unit/lib/sometest.js
? Test suite failed to run
TypeError: Cannot assign to read only property 'Symbol(Symbol.toStringTag)' of object '#<process>'
at _default (node_modules/jest-util/build/createProcessObject.js:85:34)
Run Code Online (Sandbox Code Playgroud)
我的构建配置使用最新的节点并仅运行测试
language: node_js
node_js:
- node
script:
- yarn test
Run Code Online (Sandbox Code Playgroud)
travis-ci机器的依存关系是否有变化?node和jest的版本之间有兼容性吗?
我经常将函数导入到 React 组件中,并想使用 jest/enzyme 进行测试。
通常,我可以通过wrapper.instance().functionName访问组件中定义的函数,然后测试该函数是否已被调用。同样,在测试中安装组件时,我可以将模拟函数作为 props 传递,然后检查该组件是否已被调用。但是,我没有测试导入到组件中的函数的方法(未在内部定义或作为 props 定义)。
有没有办法使用 jest/enzyme 定义将在组件测试中使用的全局模拟函数,该函数将覆盖已导入到我正在测试的组件中的同名函数的实现?
jestjs ×10
reactjs ×5
enzyme ×3
javascript ×3
node.js ×2
redux ×2
travis-ci ×2
typescript ×2
angular ×1
mocking ×1
package ×1
react-redux ×1
scope ×1
socket.io ×1
unit-testing ×1
webstorm ×1