Skip to content

Commit bb458be

Browse files
committed
Assertions must not be run outside of an active attempt
Though you may start multiple attempts.
1 parent 16cc21e commit bb458be

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ class Test {
313313
this.saveFirstError(new Error('Assertion passed, but test has already finished'));
314314
}
315315

316+
if (this.pendingAttemptCount > 0) {
317+
this.saveFirstError(new Error('Assertion passed, but an attempt is pending. Use the attempt’s assertions instead'));
318+
}
319+
316320
this.assertCount++;
317321
this.refreshTimeout();
318322
}
@@ -326,6 +330,10 @@ class Test {
326330
this.saveFirstError(new Error('Assertion started, but test has already finished'));
327331
}
328332

333+
if (this.pendingAttemptCount > 0) {
334+
this.saveFirstError(new Error('Assertion started, but an attempt is pending. Use the attempt’s assertions instead'));
335+
}
336+
329337
this.assertCount++;
330338
this.pendingAssertionCount++;
331339
this.refreshTimeout();
@@ -343,6 +351,10 @@ class Test {
343351
this.saveFirstError(new Error('Assertion failed, but test has already finished'));
344352
}
345353

354+
if (this.pendingAttemptCount > 0) {
355+
this.saveFirstError(new Error('Assertion failed, but an attempt is pending. Use the attempt’s assertions instead'));
356+
}
357+
346358
this.assertCount++;
347359
this.refreshTimeout();
348360
this.saveFirstError(error);

test/test-try-commit.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@ test('try-commit fails when plan is not reached inside the try', async t => {
277277

278278
test('plan within try-commit is not affected by assertions outside', async t => {
279279
const result = await ava(async a => {
280+
a.is(1, 1);
281+
a.is(2, 2);
282+
280283
const attempt = a.try(b => {
281284
b.plan(3);
282285
});
283286

284-
a.is(1, 1);
285-
a.is(2, 2);
286-
287287
const res = await attempt;
288288
t.false(res.passed);
289289
res.commit();
@@ -559,3 +559,29 @@ test('assigning context in try-commit does not affect parent', async t => {
559559

560560
t.is(result.passed, true);
561561
});
562+
563+
test('do not run assertions outside of an active attempt', async t => {
564+
const passing = await ava(async a => {
565+
await a.try(() => {});
566+
a.pass();
567+
}).run();
568+
569+
t.false(passing.passed);
570+
t.match(passing.error.message, /Assertion passed, but an attempt is pending. Use the attempts assertions instead/);
571+
572+
const pending = await ava(async a => {
573+
await a.try(() => {});
574+
await a.throwsAsync(Promise.reject(new Error('')));
575+
}).run();
576+
577+
t.false(pending.passed);
578+
t.match(pending.error.message, /Assertion started, but an attempt is pending. Use the attempts assertions instead/);
579+
580+
const failing = await ava(async a => {
581+
await a.try(() => {});
582+
a.fail();
583+
}).run();
584+
585+
t.false(failing.passed);
586+
t.match(failing.error.message, /Assertion failed, but an attempt is pending. Use the attempts assertions instead/);
587+
});

0 commit comments

Comments
 (0)