Skip to content

Commit af598b7

Browse files
committed
Make sure commit()/discard() only called once
1 parent 9b0db72 commit af598b7

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

lib/test.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ function tryTest(fn) {
6565

6666
const commitFn = (ret, toCommit) => {
6767
if (commitCalled) {
68-
this.saveFirstError(new Error('Either commit() or discard() was already called'));
68+
if (discarded) {
69+
this.saveFirstError(new Error('The discard() was already called'));
70+
} else {
71+
this.saveFirstError(new Error('The commit() was already called'));
72+
}
6973
return;
7074
}
7175
commitCalled = true;
@@ -101,11 +105,22 @@ function tryTest(fn) {
101105
finished = true;
102106
return Object.assign({}, ret, {
103107
commit: () => commitFn(ret, true),
104-
discard: () => commitFn(ret, false)
108+
discard: () => {
109+
discarded = true;
110+
return commitFn(ret, false);
111+
}
105112
});
106113
});
107114

108115
finishedRunning.discard = () => {
116+
if (finished) {
117+
this.saveFirstError(new Error('Attempt is already resolved'));
118+
return;
119+
}
120+
if (discarded) {
121+
this.saveFirstError(new Error('The discard() was already called'));
122+
return;
123+
}
109124
discarded = true;
110125
this.decrementAttemptAssertion();
111126
};

test/test.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,54 @@ test('try-commit fails when calling commit twice', t => {
983983
}).run().then(result => {
984984
t.false(result.passed);
985985
t.ok(result.error);
986-
t.match(result.error.message, /was already called/);
986+
t.match(result.error.message, /The commit\(\) was already called/);
987+
t.is(result.error.name, 'Error');
988+
});
989+
});
990+
991+
test('try-commit fails when calling discard twice', t => {
992+
return ava(a => {
993+
return a.try(b => b.pass()).then(res => {
994+
res.discard();
995+
res.discard();
996+
});
997+
}).run().then(result => {
998+
t.false(result.passed);
999+
t.ok(result.error);
1000+
t.match(result.error.message, /The discard\(\) was already called/);
1001+
t.is(result.error.name, 'Error');
1002+
});
1003+
});
1004+
1005+
test('try-commit fails when calling discard on promise twice', t => {
1006+
return ava(a => {
1007+
const pr = a.try(b => b.pass());
1008+
pr.discard();
1009+
pr.discard();
1010+
1011+
return pr.then(res => {
1012+
t.is(res, null);
1013+
});
1014+
}).run().then(result => {
1015+
t.false(result.passed);
1016+
t.ok(result.error);
1017+
t.match(result.error.message, /The discard\(\) was already called/);
1018+
t.is(result.error.name, 'Error');
1019+
});
1020+
});
1021+
1022+
test('try-commit fails when calling discard on promise after attempt resolved', t => {
1023+
return ava(a => {
1024+
const attemptPromise = a.try(b => b.pass());
1025+
return attemptPromise.then(res => {
1026+
t.true(res.passed);
1027+
res.commit();
1028+
attemptPromise.discard();
1029+
});
1030+
}).run().then(result => {
1031+
t.false(result.passed);
1032+
t.ok(result.error);
1033+
t.match(result.error.message, /Attempt is already resolved/);
9871034
t.is(result.error.name, 'Error');
9881035
});
9891036
});

0 commit comments

Comments
 (0)