小编And*_*rle的帖子

如何用jest模拟回调函数

我正试图用开玩笑模拟一个自定义函数,但我遇到了问题.

这是我的功能:

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)

javascript unit-testing callback jestjs

3
推荐指数
1
解决办法
1万
查看次数

将Jest覆盖率阈值添加到create-react-app项目

这是最初使用生成"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吗?

谢谢!

javascript reactjs jestjs package.json create-react-app

3
推荐指数
2
解决办法
2314
查看次数

Jest 错误:测试套件无法运行,如何解决?

我正在为 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)

unit-testing reactjs jestjs

3
推荐指数
1
解决办法
9170
查看次数

如何使用Jest模拟仅一个测试文件的模块?

我正在使用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 …

unit-testing jestjs

3
推荐指数
1
解决办法
2016
查看次数

如何用Jest测试数字范围?

我有:

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不会返回任何值xy大于4

javascript testing reactjs jestjs redux

3
推荐指数
2
解决办法
2069
查看次数

在相对路径上嘲笑

我正试图开玩笑地嘲笑相对路径.相同的代码,但有嘲弄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()从未被调用过,尽管我调试这个时我可以看到它已经达到了这个功能......

我错过了什么?

javascript testing mocking ecmascript-6 jestjs

3
推荐指数
1
解决办法
3536
查看次数

如何使用Jest检查对象的对象属性是否匹配?

我有以下代码,当它被调用时返回一个对象.我想编写一个测试用例,检查对象是否具有相应命名的树属性,它们的值是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)

javascript jestjs

3
推荐指数
1
解决办法
2877
查看次数

jest:模拟函数中没有调用Mock函数

我想测试我的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)

javascript unit-testing promise reactjs jestjs

3
推荐指数
1
解决办法
4453
查看次数

如何使用Javascript与Objective-c代码进行通信?

在原生iPhone应用程序中,我使用a UIWebView来加载网页.

我想在网页中使用JavaScript与Objective-C代码进行通信,例如调用Objective-C代码中的函数.

有没有办法实现这个?

javascript objective-c uiwebview

2
推荐指数
1
解决办法
1208
查看次数

!==运算符在javascript中做了什么?

if (description !== undefined)
Run Code Online (Sandbox Code Playgroud)

我在书呆子晚餐教程中找到了这个.

javascript

2
推荐指数
1
解决办法
148
查看次数