我正试图用开玩笑模拟一个自定义函数,但我遇到了问题.
这是我的功能:
export const resizeImage = (file, fileName, callback) => {
const MAX_WIDTH = avatarImage.maxWidth;
const MAX_HEIGHT = avatarImage.maxHeight;
const img = document.createElement('img');
img.src = window.URL.createObjectURL(file);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
img.onload = () => {
const sizes = internalResizeImage(img, MAX_WIDTH, MAX_HEIGHT);
canvas.width = sizes.width;
canvas.height = sizes.height;
ctx.drawImage(img, 0, 0, sizes.width, sizes.height);
return callback(dataURItoFile(canvas.toDataURL(), fileName));
};
};
Run Code Online (Sandbox Code Playgroud)
我打电话是这样的:
resizeImage(acceptedFiles[0], this.props.user.id, (res) => {
//dostuff
});
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我嘲笑它:
let mockResizeImage = jest.fn();
jest.mock('../../utilities/imageUtils', () => ({
resizeImage: () …
Run Code Online (Sandbox Code Playgroud) 这是最初使用生成"scripts"
的package.json
文件部分create-react-app
:
"scripts": {
"start": "concurrently \"react-scripts start\" \"node server.js\"",
"build": "react-scripts build",
"eject": "react-scripts eject",
"test": "react-scripts test --env=jsdom --coverage --watchAll",
"start:server": "node server"
},
Run Code Online (Sandbox Code Playgroud)
我想将Jest配置为具有以下覆盖率阈值:
"jest": {
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行yarn test
它时,该"jest"
部分看起来好像没有被执行。我还需要添加一些其他东西来建立这个项目create-react-app
吗?
谢谢!
我正在为 reactjs 测试 Jest,尤其是快照测试。这是我的测试:
import React, { Component } from 'react';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json';
import Hello from 'hello';
test('if am then return goodmorning', () => {
const wrapper = shallow(
<Hello timeofday="am">
<strong>Hello World!</strong>
</Hello>
);
expect(toJson(wrapper)).toMatchSnapshot();
});
Run Code Online (Sandbox Code Playgroud)
我正在测试的组件:
import React from 'react';
export default class Hello extends React.Component {
constructor(props) {
super();
this.props = props;
//todo remove
console.log('testing props', props);
}
render() {
let greet ;
if (this.props) {
greet= this.props.timeofday ==='am' …
Run Code Online (Sandbox Code Playgroud) 我正在使用Jest,并且想从另一个模块中模拟一些功能(我们称之为dependency
)。
我能够进行dependency
全局模拟,将其放入__mocks__
文件__tests__
夹中的文件夹中。
Unfortunately, I need the actual dependecy
for all the other tests. So, how can I specify that I want to require the mocked dependecy
only when required in my file1.js
, but not in all other files?
PS: I could create a __mocks__
folder inside my file1.js
folder, but this file is in the root, so when dependency
is required by any file it will be picked …
我有:
const BOARD = {
size: {
columns: 5,
rows: 5,
},
}
Run Code Online (Sandbox Code Playgroud)
以及一个Redux动作创建者,该动作产生者在董事会规模之内:
const generateInitialPlayerPosition = (
{
x = random(0, BOARD_SIZE.size.rows - 1),
y = random(0, BOARD_SIZE.size.columns - 1)
} = {}) => ({
type: GENERATE_INITIAL_PLAYER_POSITION,
payload: { x, y },
}
)
Run Code Online (Sandbox Code Playgroud)
我需要测试在这种情况下generateInitialPlayerPosition
不会返回任何值x
或y
大于4
我正试图开玩笑地嘲笑相对路径.相同的代码,但有嘲弄fs
工作很好,所以我不知道为什么试图模拟我自己的模块不起作用
// myFile.js
const { cacheFile } = require('./cacheHandler.js')
const myFunc = () => {
cacheFile(file_name)
}
// myFile.spec.js
const myFile = require('./myFile.js')
const cacheHandler = require('./cacheHandler.js')
jest.mock('./cacheHandler.js')
describe("my failing test :( ", () =>{
it("should be able to spy on the function", () => {
cacheHandler.cacheFile = jest.fn()
myFile.myFunc()
expect(cacheHandler.cacheFile).toHaveBeenCalledTimes(1)
}
}
Run Code Online (Sandbox Code Playgroud)
jest声称cacheFile()从未被调用过,尽管我调试这个时我可以看到它已经达到了这个功能......
我错过了什么?
我有以下代码,当它被调用时返回一个对象.我想编写一个测试用例,检查对象是否具有相应命名的树属性,它们的值是number,array和bool.
能否请您使用Jest库提供示例?
const location = () => {
return {
locationId: 5128581, // nyc usa
geo: [-74.006, 40.7143],
isFetching: false
}
}
export default location
Run Code Online (Sandbox Code Playgroud)
我想测试我的react login组件,但是还没有调用mock函数:
登录:
export default class LoginPage extends PureComponent {
static propTypes = {
login: PropTypes.func.isRequired,
history: PropTypes.shape({
replace: PropTypes.func.isRequired,
}).isRequired,
};
onSubmit = (e) => {
this.props.login(this.state.username, this.state.password)
.then(() => {
this.props.history.replace('/');
});
};
render() {
return (
<form onSubmit={this.onSubmit}>
...
</form>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我用jest
+ enzyme
来测试这个:
const props = {
login: jest.fn(() => Promise.resolve('success')),
history: {
replace: jest.fn()
},
};
const wrapper = mount(<LoginPage {...props}/>);
const form = wrapper.find(Form);
const inputs = form.find('input');
const username …
Run Code Online (Sandbox Code Playgroud) 在原生iPhone应用程序中,我使用a UIWebView
来加载网页.
我想在网页中使用JavaScript与Objective-C代码进行通信,例如调用Objective-C代码中的函数.
有没有办法实现这个?
javascript ×8
jestjs ×8
reactjs ×4
unit-testing ×4
testing ×2
callback ×1
ecmascript-6 ×1
mocking ×1
objective-c ×1
package.json ×1
promise ×1
redux ×1
uiwebview ×1