期望测试失败


🌐 Expecting tests to fail

这会翻转特定测试或测试套件的通过/失败报告:被标记的测试用例必须抛出异常才能通过,而未抛出异常的被标记测试用例则失败。

🌐 This flips the pass/fail reporting for a specific test or suite: a flagged test case must throw in order to pass, and a flagged test case that does not throw fails.

在以下每一种情况下,doTheThing() 未能返回 true,但由于测试被标记为 expectFailure,它们通过了。

🌐 In each of the following, doTheThing() fails to return true, but since the tests are flagged expectFailure, they pass.

it.expectFailure('should do the thing', () => {
  assert.strictEqual(doTheThing(), true);
});

it('should do the thing', { expectFailure: true }, () => {
  assert.strictEqual(doTheThing(), true);
});

it('should do the thing', { expectFailure: 'feature not implemented' }, () => {
  assert.strictEqual(doTheThing(), true);
}); 

如果 expectFailure 的值是一个 <RegExp> | <Function> | <Object> | <Error>,测试将仅在抛出匹配的值时通过。请参阅 assert.throws 了解每种值类型的处理方式。

🌐 If the value of expectFailure is a <RegExp> | <Function> | <Object> | <Error> the tests will pass only if they throw a matching value. See assert.throws for how each value type is handled.

以下每个测试都会失败,尽管被标记为 expectFailure,因为失败与特定的预期失败不匹配。

🌐 Each of the following tests fails despite being flagged expectFailure because the failure does not match the specific expected failure.

it('fails because regex does not match', {
  expectFailure: /expected message/,
}, () => {
  throw new Error('different message');
});

it('fails because object matcher does not match', {
  expectFailure: { code: 'ERR_EXPECTED' },
}, () => {
  const err = new Error('boom');
  err.code = 'ERR_ACTUAL';
  throw err;
}); 

要为 expectFailure 提供原因和具体错误,请使用 { label, match }

🌐 To supply both a reason and specific error for expectFailure, use { label, match }.

it('should fail with specific error and reason', {
  expectFailure: {
    label: 'reason for failure',
    match: /error message/,
  },
}, () => {
  assert.strictEqual(doTheThing(), true);
}); 

skip 和/或 todoexpectFailure 互斥,而 skiptodo 在同时应用时会“胜出”(skip 对两者都胜出,而 todo 胜过 expectFailure)。

这些测试将被跳过(不会运行):

🌐 These tests will be skipped (and not run):

it.expectFailure('should do the thing', { skip: true }, () => {
  assert.strictEqual(doTheThing(), true);
});

it.skip('should do the thing', { expectFailure: true }, () => {
  assert.strictEqual(doTheThing(), true);
}); 

这些测试将被标记为“待办”(忽略错误):

🌐 These tests will be marked "todo" (silencing errors):

it.expectFailure('should do the thing', { todo: true }, () => {
  assert.strictEqual(doTheThing(), true);
});

it.todo('should do the thing', { expectFailure: true }, () => {
  assert.strictEqual(doTheThing(), true);
});