You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
更新所有失败的snapshot test:jest --updateSnapshot或简写 jest -u; 可以通过jest --testNamePattern=<regex>;指定匹配模块; 如jest -t=anchor.*page, anchor.*page可以匹配到'renders as an anchor when no page is set'这个模块;
交互模式: 在jest —watch模式下,输入 i,可以进入交互模式;
动态的数据,比如 new Date(),由于每次的值都不一样,所以需要进行修正 ; 可以如下修复:
it('will check the matchers and pass',()=>{constuser={createdAt: newDate(),id: Math.floor(Math.random()*20),name: 'LeBron James',};expect(user).toMatchSnapshot({createdAt: expect.any(Date),id: expect.any(Number),});});
hello world
在线 demo: https://github.com/facebook/jest.git
jest/examples/snapshot里的目录结构:
实例
运行下面的测试代码:
将生成如下的snapshot文件:
jest --updateSnapshot
或简写jest -u
; 可以通过jest --testNamePattern=<regex>;
指定匹配模块; 如jest -t=anchor.*page
, anchor.*page可以匹配到'renders as an anchor when no page is set'
这个模块;最佳实践
Date.now = jest.fn(() => 1482363367071);
后续参考资料
基础:Snapshot Testing · Jest
由来:Jest 14.0: React Tree Snapshot Testing · Jest
初印象:https://benmccormick.org/2016/09/19/testing-with-jest-snapshots-first-impressions/
视频推荐:Use Jest’s Snapshot Testing Feature from @kentcdodds on @eggheadio
The text was updated successfully, but these errors were encountered: