reactjs - cy.tick() 在赛普拉斯测试中无法正常工作
问题描述
我正在测试 5 秒后出现超时屏幕:
describe('UI aggregates error/status', () => {
beforeEach(() => {
cy.clock();
cy.visitHome();
});
describe('Detect timeout of backend status and trigger modal dialog.', () => {
const TIMEOUT = 5000;
it('reports a timeout if no status is received after 5 seconds', () => {
sendStatus({ status: HEALTHY });
cy.tick(TIMEOUT + 1);
expectErrorScreen();
});
});
});
实际的应用程序代码可以正常工作。
export const handleStatusMessage = (message: MessageEvent): ThunkAction =>
(dispatch: ThunkDispatch<{}, {}, any>, getState: () => AppState) => {
const { status }: StatusMessage = JSON.parse(message.data);
// clear the timer, that we set last time
const oldTimer = getState().meta.statusTimer;
if (oldTimer) clearTimeout(oldTimer);
// start a new timer. if we don't receive a status within the timeout period,
// this timer will finish and report a timeout.
const newTimer = setTimeout(() => {
dispatch(reportError('Timeout'));
}, STATUS_TIMEOUT_MS);
// report that we've received a status, replacing the old timer.
dispatch(reportStatusReceived(newTimer));
// handle actual status...
但是 cypress timer-mocking ( cy.tick()
) 不起作用——测试失败,并且在他们超时后没有找到他们正在寻找的元素,我的超时屏幕出现了(因为应用程序超时时间比我猜的 cypress 自己的超时时间长)。
我已经cy.tick()
在其他测试中成功使用过,所以我知道我用对了:
describe('successful request', () => {
beforeEach(() => {
cy.clock();
// ... other setup ...
});
describe('request timed out (no WS response after 5 secs)', () => {
it('shows a timeout error when the HTTP request succeeds but a WS verification is not received', () => {
cy.tick(5000);
cy.contains('Timed out');
});
});
});
笔记:
我今天写了有问题的测试。赛普拉斯今天也表现得非常非常烦人,在测试运行后异常被锁定(在赛普拉斯旋转中加载指标但测试被冻结,或不重新加载,和/或我无法与赛普拉斯 UI 交互) ,这包括一个问题,当我运行大量测试时,它会在运行中间冻结。所以我不知道这是否与它有关。虽然我只能运行那些有效的滴答测试(我上面的第三个片段)describe.only
并且有效,但我再次可以确认它cy.tick
有时有效。
解决方案
推荐阅读
- amazon-web-services - 如何在 AWS S3 中托管的应用程序中实现单点登录?
- python - 根据不同列中的条件修改 2 列
- php - woocommerce oauth 的 Rest 身份验证
- angular - Angular8 / NG-ZORRO 8:在 nzAction 的 ng-template 中绑定
- ionic-framework - 为什么键盘 go 按钮在 ionic 4 应用程序中不执行任何操作?
- django - “DeferredAttribute”对象不可调用
- python - 如何在 HTML 集成 DRF 和 django-filters 中显示表单
- jekyll - Jekyll,将 html 嵌入为字符串
- python-3.x - IndexError:尝试在python中为扫雷器制作网格时列表索引超出范围
- javascript - 反应 setState 的深度嵌套值