Skip to content

Commit 952a017

Browse files
Marcnovemberborn
Marc
andauthored
Experimentally reverse teardown order
Co-authored-by: Mark Wubben <[email protected]>
1 parent b3866b6 commit 952a017

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

docs/02-execution-context.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,36 @@ Plan how many assertion 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. 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 and they'll run in 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.
50+
51+
**`package.json`**:
52+
53+
```json
54+
{
55+
"ava": {
56+
"nonSemVerExperiments": {
57+
"reverseTeardowns": true
58+
}
59+
}
60+
}
61+
```
62+
63+
**`ava.config.js`**:
64+
65+
```js
66+
export default {
67+
nonSemVerExperiments: {
68+
reverseTeardowns: true
69+
}
70+
}
71+
```
72+
4973
## `t.timeout(ms)`
5074

5175
Set a timeout for the test, in milliseconds. The test will fail if this timeout is exceeded. The timeout is reset each time an assertion is made.

lib/load-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const pkgConf = require('pkg-conf');
77

88
const NO_SUCH_FILE = Symbol('no ava.config.js file');
99
const MISSING_DEFAULT_EXPORT = Symbol('missing default export');
10-
const EXPERIMENTS = new Set();
10+
const EXPERIMENTS = new Set(['reverseTeardowns']);
1111

1212
// *Very* rudimentary support for loading ava.config.js files containing an `export default` statement.
1313
const evaluateJsConfig = configFile => {

lib/test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,13 @@ class Test {
482482
}
483483

484484
async runTeardowns() {
485-
for (const teardown of this.teardowns) {
485+
const teardowns = [...this.teardowns];
486+
487+
if (this.experiments.reverseTeardowns) {
488+
teardowns.reverse();
489+
}
490+
491+
for (const teardown of teardowns) {
486492
try {
487493
await teardown(); // eslint-disable-line no-await-in-loop
488494
} catch (error) {

test-tap/test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const delay = require('delay');
1010
const snapshotManager = require('../lib/snapshot-manager');
1111
const Test = require('../lib/test');
1212
const HelloMessage = require('./fixture/hello-message');
13-
const {ava} = require('./helper/ava-test');
13+
const {ava, withExperiments} = require('./helper/ava-test');
1414

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

@@ -799,6 +799,23 @@ test('teardowns run sequentially in order', t => {
799799
});
800800
});
801801

802+
test('teardowns run in reverse order when the `reverseTeardowns` experimental feature is enabled', t => {
803+
let resolveA;
804+
const teardownA = sinon.stub().returns(new Promise(resolve => {
805+
resolveA = resolve;
806+
}));
807+
const teardownB = sinon.stub().resolves(delay(200));
808+
809+
return withExperiments({reverseTeardowns: true})(a => {
810+
a.teardown(teardownA);
811+
a.teardown(() => teardownB().then(resolveA));
812+
a.pass();
813+
}).run().then(result => {
814+
t.is(result.passed, true);
815+
t.ok(teardownB.calledBefore(teardownA));
816+
});
817+
});
818+
802819
test('teardown with cb', t => {
803820
const teardown = sinon.spy();
804821
return ava.cb(a => {

0 commit comments

Comments
 (0)