Skip to content

Commit dc4483f

Browse files
committed
Teardowns run in reverse order
Fixes #2516.
1 parent bcb750c commit dc4483f

File tree

4 files changed

+10
-31
lines changed

4 files changed

+10
-31
lines changed

docs/02-execution-context.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ Plan how many assertions there are in the test. The test will fail if the actual
4040

4141
## `t.teardown(fn)`
4242

43-
Registers the `fn` function to be run after the test has finished. You can register multiple functions and they'll run in order<sup>†</sup>. You can use asynchronous functions: only one will run at a time.
43+
Registers the `fn` function to be run after the test has finished. You can register multiple functions. In AVA 3 the functions are called in order, but in AVA 4 they'll run in _reverse_ order.<sup>†</sup>. You can use asynchronous functions: only one will run at a time.
4444

4545
You cannot perform assertions using the `t` object or register additional functions from inside `fn`.
4646

4747
You cannot use `t.teardown()` in hooks either.
4848

49-
<sup>†</sup> In the next major release we'll change this so teardown functions run in reverse order. The last registered function will be called first. You can opt in to this behavior now by enabling the `reverseTeardowns` experiment.
49+
<sup>†</sup> You can opt in to this behavior in AVA 3 by enabling the `reverseTeardowns` experiment.
5050

5151
**`package.json`**:
5252

lib/load-config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const NO_SUCH_FILE = Symbol('no ava.config.js file');
1010
const MISSING_DEFAULT_EXPORT = Symbol('missing default export');
1111
const EXPERIMENTS = new Set([
1212
'nextGenConfig',
13-
'reverseTeardowns',
1413
'sharedWorkers'
1514
]);
1615

lib/test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,7 @@ class Test {
509509
}
510510

511511
async runTeardowns() {
512-
const teardowns = [...this.teardowns];
513-
514-
if (this.experiments.reverseTeardowns) {
515-
teardowns.reverse();
516-
}
512+
const teardowns = [...this.teardowns].reverse();
517513

518514
for (const teardown of teardowns) {
519515
try {

test-tap/test.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const sinon = require('sinon');
88
const delay = require('delay');
99
const snapshotManager = require('../lib/snapshot-manager');
1010
const Test = require('../lib/test');
11-
const {ava, withExperiments} = require('./helper/ava-test');
11+
const {ava} = require('./helper/ava-test');
1212

1313
const failingTestHint = 'Test was expected to fail, but succeeded, you should stop marking the test as failing';
1414

@@ -815,30 +815,14 @@ test('teardown awaits promise', t => {
815815
});
816816
});
817817

818-
test('teardowns run sequentially in order', t => {
819-
const teardownA = sinon.stub().resolves(delay(200));
820-
let resolveB;
821-
const teardownB = sinon.stub().returns(new Promise(resolve => {
822-
resolveB = resolve;
823-
}));
824-
return ava(a => {
825-
a.teardown(() => teardownA().then(resolveB));
826-
a.teardown(teardownB);
827-
a.pass();
828-
}).run().then(result => {
829-
t.is(result.passed, true);
830-
t.ok(teardownA.calledBefore(teardownB));
831-
});
832-
});
833-
834-
test('teardowns run in reverse order when the `reverseTeardowns` experimental feature is enabled', t => {
818+
test('teardowns run in reverse order', t => {
835819
let resolveA;
836820
const teardownA = sinon.stub().returns(new Promise(resolve => {
837821
resolveA = resolve;
838822
}));
839823
const teardownB = sinon.stub().resolves(delay(200));
840824

841-
return withExperiments({reverseTeardowns: true})(a => {
825+
return ava(a => {
842826
a.teardown(teardownA);
843827
a.teardown(() => teardownB().then(resolveA));
844828
a.pass();
@@ -896,9 +880,9 @@ test('teardown errors are hidden behind assertion errors', t => {
896880
});
897881
});
898882

899-
test('teardowns errors do not stop next teardown from running', t => {
900-
const teardownA = sinon.stub().throws('TeardownError');
901-
const teardownB = sinon.spy();
883+
test('teardown errors do not stop next teardown from running', t => {
884+
const teardownA = sinon.spy();
885+
const teardownB = sinon.stub().throws('TeardownError');
902886
return ava(a => {
903887
a.teardown(teardownA);
904888
a.teardown(teardownB);
@@ -908,7 +892,7 @@ test('teardowns errors do not stop next teardown from running', t => {
908892
t.is(result.error.name, 'TeardownError');
909893
t.ok(teardownA.calledOnce);
910894
t.ok(teardownB.calledOnce);
911-
t.ok(teardownA.calledBefore(teardownB));
895+
t.ok(teardownB.calledBefore(teardownA));
912896
});
913897
});
914898

0 commit comments

Comments
 (0)