Skip to content

Commit 9b0db72

Browse files
committed
Refactor tryTest: pull toCommit() defn from promise
1 parent 6b72886 commit 9b0db72

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

lib/test.js

+27-25
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,34 @@ function tryTest(fn) {
5959
args.push(arguments[i]);
6060
}
6161

62+
let commitCalled = false;
63+
let discarded = false;
64+
let finished = false;
65+
66+
const commitFn = (ret, toCommit) => {
67+
if (commitCalled) {
68+
this.saveFirstError(new Error('Either commit() or discard() was already called'));
69+
return;
70+
}
71+
commitCalled = true;
72+
73+
if (ret.passed) {
74+
this.countFinishedAttemptAssertion(toCommit);
75+
} else {
76+
this.countFailedAttemptAssertion(ret.error, toCommit);
77+
}
78+
79+
if (toCommit) {
80+
for (const log of ret.logs) {
81+
this.addLog(log);
82+
}
83+
}
84+
};
85+
6286
const running = this
6387
.forAttempt(t => fn.apply(null, [t].concat(args)))
6488
.run();
6589

66-
let commitCalled = false;
67-
let discarded = false;
68-
6990
this.addPendingAttemptAssertion(running.then(ret => {
7091
if (!discarded && !ret.passed) {
7192
throw ret.error;
@@ -77,29 +98,10 @@ function tryTest(fn) {
7798
return null;
7899
}
79100

80-
const commitFn = toCommit => {
81-
if (commitCalled) {
82-
this.saveFirstError(new Error('Either commit() or discard() was already called'));
83-
return;
84-
}
85-
commitCalled = true;
86-
87-
if (ret.passed) {
88-
this.countFinishedAttemptAssertion(toCommit);
89-
} else {
90-
this.countFailedAttemptAssertion(ret.error, toCommit);
91-
}
92-
93-
if (toCommit) {
94-
for (const log of ret.logs) {
95-
this.addLog(log);
96-
}
97-
}
98-
};
99-
101+
finished = true;
100102
return Object.assign({}, ret, {
101-
commit: () => commitFn(true),
102-
discard: () => commitFn(false)
103+
commit: () => commitFn(ret, true),
104+
discard: () => commitFn(ret, false)
103105
});
104106
});
105107

0 commit comments

Comments
 (0)