reactjs - Jest 无法加载时刻

标签 reactjs jestjs

我有一个非常简单的 React 组件,它利用了 moment 库:

import React from 'react';
import moment from 'moment';

let { Component } = React;

export class SimpleApp extends Component {
    constructor (props) {
        super(props);
        var currentDate = this.props.data[this.props.current].date;

        this.state = {
            currentDate: currentDate,
            currentFromatted: moment(currentDate).format('dddd, MMMM Do YYYY')
        };
    }

    render() {
        return (
            <div className="simple">{this.state.currentDate}</div>
        );
    }
} 

module.exports = SimpleApp;

我有这个测试来测试这个组件:

import React from 'react/addons';
import SimpleApp from '../js/components/pages/Simple.react';

var TestUtils = React.addons.TestUtils;


jest.dontMock('../js/components/pages/Simple.react');
jest.dontMock('moment');

describe('SimpleApp', () => {
    it('should not do anything', () => {
        var data = {
            "date": "2015-07-01",
            "total": 2,
            "results":[
                {
                    "startTime": "2015-07-01T00:00:00.000",
                    "endTime": "2015-07-01T00:59:59.999",
                    "total": 2,
                    "results":[
                        {
                            "type":"motion",
                            "count":2
                        },
                        {
                            "type":"transaction",
                            "count":0
                        }
                    ]
                }
            ]
        };

        var simple = TestUtils.renderIntoDocument(
            <SimpleApp data={[data]} current="0" />
        );

        var div = TestUtils.findRenderedDOMComponentWithTag(simple, 'div');
        expect(div.getDOMNode().textContent).toEqual('2015-07-01');
    })
})

当运行npm test时,我收到错误消息,提示无法调用未定义的方法“格式”:

● SimpleApp › it should not do anything
  - TypeError: Cannot call method 'format' of undefined
        at new SimpleApp (/Users/xiaofanyang/workspace/solink/histogram/flux2/js/components/pages/Simple.react.js:13:69)

似乎库被 mock 了,但是,我确实指定不 mock 它......但显然没有帮助......

最佳答案

我设法解决这个问题的方法是这样做

jest.autoMockOff(); 

然后手动模拟项目中我需要的任何其他内容。

关于reactjs - Jest 无法加载时刻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31410949/

相关文章:

javascript - 如何使用 supertest 和 jest 测试图像上传(流)?

javascript - 开 Jest : test that an array contains none of these values?

typescript - 将 jest 的 expect.extend() 与 TypeScript 的范围限定为单个测试文件

javascript - 使用 React 和 webpack 4 拆分项目; html 标签是意外的标记

javascript - React 和 Flex 布局如何使用它们

javascript - 如何使 react-chartjs-2 在移动设备上响应?

javascript - 在 enzyme 中模拟 history.back()

jestjs - 无法模拟 createQueryBuilder 函数 typeorm Nestjs Jest

css - 在 reactjs 中悬停显示组件

javascript - NEXT JS req.body undefined