我有一个'notifications.js'模块,看起来像这样:
import { Notifications, Permissions } from 'expo'
export function setLocalNotification(storage = AsyncStorage) {
return storage
.getItem(NOTIFICATION_KEY)
.then(JSON.parse)
.then(data => {
if (data === null) {
return Permissions.askAsync(
Permissions.NOTIFICATIONS
).then(({ status }) => {
if (status === 'granted') {
Notifications.cancelAllScheduledNotificationsAsync()
...etc.
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我想模拟“权限”和“通知”,以便可以在notifications.spec.js中执行以下操作:
import { setLocalNotification } from './notifications'
import mockAsyncStorage from '../mock/AsyncStorage'
it('correctly cancels pending notifications', done => {
setLocalNotification(mockAsyncStorage).then(done())
expect(Permissions.askAsync).toBeCalled()
expect(Notifications.cancelAllScheduledNotificationsAsync)
.toBeCalled()
})
Run Code Online (Sandbox Code Playgroud)
我已经尝试过使用各种方法jest.mock
,jest.setMock
但是似乎无法正常工作。如何以所需的方式模拟这些命名的导入?例如,我已经尝试过了:
jest.setMock('Permissions', () => ({
askAsync: jest
.fn()
.mockImplementationOnce(() => ({ status: …
Run Code Online (Sandbox Code Playgroud) 我有一个配置文件导入到我将要测试的文件中,我需要能够模拟配置文件,但我似乎做错了什么 - 我有
应用程序.js
import config from './config';
export default class App{
static get(){
//refers to config
}
}
Run Code Online (Sandbox Code Playgroud)
测试文件
import App from './app';
it('should do something', () => {
jest.mock(./config, () => {
return {
//mocked config
}
})
})
Run Code Online (Sandbox Code Playgroud)
但是在运行测试时,我得到的是真实的配置而不是模拟的配置。有任何想法吗?
我正在处理一个 React 项目,并且正在使用 jest 为我的代码编写测试。
这是我要测试的代码。
const handleSubmit = (handleSuccess, handleErrors) => {
signupAPI(user)
.then(handleSuccess)
.catch(handleErrors);
};
Run Code Online (Sandbox Code Playgroud)
这是测试代码:
test('should call handleSuccess', () => {
signupAPI.mockImplementation((user) => Promise.resolve(user));
const handleSuccess = jest.fn();
const handleErrors = jest.fn();
handleSubmit(handleSuccess, handleErrors);
expect(signupAPI).toHaveBeenCalled(); // test passes
expect(handleSuccess).toHaveBeenCalled(); // test fails
});
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,它永远不会在承诺之后移动到“then”部分。如何测试 then 部分中的函数是否被实际调用?
当我在Constant部分注意到带注释的注释时,我正在阅读http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml:
/**
* The number of seconds in each of the given units.
* @type {Object.<number>}
* @const
*/
Run Code Online (Sandbox Code Playgroud)
然后指南继续"这允许编译器强制执行常量".
这是v8的事吗?这记录在哪里?
我的头脑很可能有可能,也许,我可以提供类型信息的v8(或其他)!
是否可以在javascript中使用字符串拆分来获取生成的数组中的最后一个元素.
例如,我确定在php中你可以做同样的事情并使用负限制值来获得最后一项,即-1或最后两项-2等.
我在li元素里面有div:
<ul id="tabs">
<li id="tab-1"> <div id="xxx"></div> </li>
....
</ul>
Run Code Online (Sandbox Code Playgroud)
如果在CSS中我将悬停效果应用于li元素
#tabs li:hover {....}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,效果是应用于li和child div,
但是如果我在李和冒号之间留一个空格
#tabs li :hover {....}
Run Code Online (Sandbox Code Playgroud)
效果仅适用于儿童div
这是正常的吗?我是一个脚本初学者,通过阅读不同的教程,我得到的印象是,在执行脚本时,空白区域没有区别.我在Chrome,Safari和Firefox中对此进行了测试
我尝试使用grunt-contrib-coffee插件使用grunt编译我的coffeescript文件.但是跑步grunt coffee
,我总是得到这个错误:
RangeError: Maximum call stack size exceeded
Run Code Online (Sandbox Code Playgroud)
这是我的grunt.js文件:
/*global module:false*/
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
meta: {
version: '0.1.0',
banner: '/*! PROJECT_NAME - v<%= meta.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'* http://PROJECT_WEBSITE/\n' +
'* Copyright (c) <%= grunt.template.today("yyyy") %> ' +
'YOUR_NAME; Licensed MIT */'
},
lint: {
files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
},
qunit: {
files: ['test/**/*.html']
},
concat: {
dist: {
src: ['<banner:meta.banner>', '<file_strip_banner:lib/FILE_NAME.js>'],
dest: 'dist/FILE_NAME.js' …
Run Code Online (Sandbox Code Playgroud) 我有一个反应组件,它接受一组对象并将它们映射到视图中的另一个反应组件中。我在测试它时运气不好,因为它给出了这个错误:
类型错误:data.map 不是函数
这是我写的测试。请注意我传递了 data 属性,我认为这应该使它起作用?
内容.test.js
import React from 'react';
import { shallow, mount } from 'enzyme';
import { expect } from 'chai';
import Content from '../components/Content.jsx';
describe('<Content />', () => {
const data = {
created: '2017-02-21T09:50:21.441815Z',
duration: 1575,
final_script: 'some script',
language: 'en-GB',
rating: 2,
url: 'some url',
};
it('renders without exploding', () => {
mount(<Content data={ data } />);
});
});
Run Code Online (Sandbox Code Playgroud)
这是组件本身
内容.jsx
function Content(props) {
const { data } = props;
return (
<div className='content_container'> …
Run Code Online (Sandbox Code Playgroud) 我正在用酶运行开玩笑,但我特别从我试图测试的类中的react-router依赖获得上述警告.我的问题是为什么我收到这个警告,因为我已经将我的反应版本降级为15.1.1.
Package.json
"dependencies": {
"axios": "^0.16.1",
"babel": "^6.23.0",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"bootstrap": "^3.3.5",
"boron": "^0.2.3",
"browserify": "^13.0.1",
"connect-history-api-fallback": "^1.2.0",
"debounce": "^1.0.0",
"envify": "^3.4.0",
"es6-promise": "^3.2.1",
"font-awesome": "^4.6.3",
"jquery": "^2.2.4",
"jwt-decode": "^2.2.0",
"minimist": "^1.2.0",
"node-fetch": "1.6.3",
"node-notifier": "^4.6.0",
"object-assign": "^4.1.0",
"react": "^15.1.0",
"react-autosuggest": "^3.8.0",
"react-dnd": "^2.1.4",
"react-dnd-html5-backend": "^2.1.2",
"react-document-title": "^2.0.3",
"react-dom": "^15.1.0",
"react-dropdown": "^1.0.4",
"react-quill": "^0.4.1",
"react-router": "^2.4.1",
"react-select": "^1.0.0-beta13",
"react-select2-wrapper": "^0.6.1",
"react-tag-input": "^3.0.3",
"reflux": "0.4.1",
"request": "^2.81.0",
"request-promise": "^4.2.0",
"toastr": "^2.1.0",
"underscore": "^1.8.3",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.7.0", …
Run Code Online (Sandbox Code Playgroud) 现在我正在学习开玩笑来编写单元测试。我已经阅读了 jest 的部分文档和 github 上关于 jest 配置的一些代码,我发现可以像这样定义选项 moduleNameMapper:
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif)$": "<rootDir>/__mocks__/fileMock.js",
".*\\.(css|less|scss)$": "<rootDir>/__mocks__/styleMock.js"
}
Run Code Online (Sandbox Code Playgroud)
但我不明白\\
配置中的“ ”是什么意思?
正则表达式中的两个反斜杠似乎无法匹配任何文件或目录。
我只是想知道这样的配置可以匹配像"aaa.jpg"
,"src/images/bbb.jpg"
,"src/less/style.config.less"
,等不?
jestjs ×6
javascript ×4
reactjs ×3
enzyme ×2
arrays ×1
chai ×1
coffeescript ×1
css ×1
es6-modules ×1
expo ×1
gruntjs ×1
mocking ×1
npm ×1
react-native ×1
react-router ×1
sinon ×1
testing ×1