快照时间戳不匹配

时间:2019-09-17 08:27:37

标签: javascript reactjs unit-testing jestjs enzyme

我要运行快照测试。当我运行测试时,唯一的问题就是时间戳。我该如何解决?

这是一个通过所有测试的组件,以与失败的组件相同的方式进行测试。

传递组件

return (
  <div className={`accordion-section ${className}`}>
    <button tabIndex={0} className={'accordion-btn'} onClick={toggleAccordion}>
      <p className={'accordion-title'}>
        <Text isRtl={rtl}>{title}</Text>
      </p>
      <ArrowTemplate
        direction={isAccordionExpanded ? 'up' : 'down'}
        onClick={toggleAccordion}
        rtl={rtl}
        color={color}
      />
    </button>
    <AccordionContent
      tabIndex={0}
      className={'accordion-content'}
      height={height}
      isAccordionExpanded={isAccordionExpanded}
      ref={element}
      aria-expanded={isAccordionExpanded}
    >
      <div className={'accordion-text'}>
        <Text isRtl={rtl}>{content}</Text>
      </div>
    </AccordionContent>
  </div>
);

通过测试:

let wrapper;
beforeEach(() => {
  wrapper = mount(<StyledAccordion rtl={false} content='content' color='black' title='title'/>);
});
it('should match the snapshot', () => {
  expect(wrapper).toMatchSnapshot();
});

失败组件

<StyledButtonTemplate
  {...{ className, label }}
  classes={isTaskCompleted ? 'check-button-gray' : 'check-button-blue'}
  disabled={isTaskCompleted}
  onClick={handleClick}
/>

失败测试:

const checkAnswerMock = jest.fn();
const SolveButton = <StyledSolveButton
  handleClick={checkAnswerMock}
  isTaskCompleted={false}
  text={'Check'}
/>;

it('should match the snapshot', () => {
    expect(shallow(SolveButton)).toMatchSnapshot();
});

精确错误:

expect(received).toMatchSnapshot()

Snapshot name: `<SolveButton/> match the snapshot 1`

- Snapshot
+ Received

@@ -1029287,11 +1029287,11 @@
  aria-disabled="false"
class="solve-button-blue"
  >
  <span />
  </button>,
  -             "timeStamp": 1568711150135,
  +             "timeStamp": 1568711193266,
  "type": "click",
},
],
],
"results": Array [

51 |
52 |   it('match the snapshot', () => {
> 53 |     expect(shallow(SolveButton)).toMatchSnapshot();
|                                  ^
  54 |   });
55 | });
56 |

at Object.toMatchSnapshot (src/components/SolveCheckButton/test/SolveButton.test.js:53:34)

enter image description here

2 个答案:

答案 0 :(得分:0)

某事正在调用日期。我们可以使用

覆盖它
export const getByIds = async ids => {
    let results = await Customers.find({ _id: { $in: ids }, deletedAt: null }).exec();

    let rows = ids.map(id => {
        let found = results.find(item => {
            return item._id.equals(id);
        });

        return found ? found : null;
    });

    return rows;
};

来自here

答案 1 :(得分:0)

确定添加以下内容:https://github.com/facebook/jest/issues/2234

Date.now = jest.fn();

在描述开始时就对其进行了修复。

describe('<SolveButton/>', () => {
  Date.now = jest.fn();