如何用自定义消息替换 Cypress 错误消息

ali*_* ak 3 chai cypress

我正在尝试使用 cypress 解析一个大文件并断言该文件中不包含字符串。我想替换断言消息以保持日志干净。我已经尝试过:我将内容包装如下:

cy.task('parsePdf', filePath)
  .its('text')
  .then(($txt) => {
    cy.wrap($txt).as('content');
  });
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用 Chai 来断言这一点expect,并以两种方式插入自定义消息,这两种方式都会产生相同的结果:

第一的:

cy.get('@content').should(($txt) => {
  expect($txt).to.not.include(
    'text that should not be in the content',
    'Custom error message'
  );
});
Run Code Online (Sandbox Code Playgroud)

第二:

cy.get('@content').should(($txt) => {
  expect($txt, 'Custom error message').to.not.include(
    'text that should not be in the content');
});
Run Code Online (Sandbox Code Playgroud)

结果:

Custom error message预计Full report content不包括text that should not be in the content.

我寻找的只是登录Custom error message失败。

预期的:

Custom error message

Zai*_*sta 5

也许是这样的:

cy.get('@content').should(($txt) => {
  const flag = $txt.includes('text that should be in the content');
  assert.isTrue(flag, 'Custom message');
});
Run Code Online (Sandbox Code Playgroud)