Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] add .teardown() on t instances #546

Merged
merged 2 commits into from Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/workflows/node-zero.yml
Expand Up @@ -12,6 +12,7 @@ jobs:
- uses: ljharb/actions/node/matrix@main
id: set-matrix
with:
versionsAsRoot: true
preset: '0.x'

stable:
Expand All @@ -20,7 +21,8 @@ jobs:
runs-on: ubuntu-latest

strategy:
matrix: ${{ fromJson(needs.matrix.outputs.stable) }}
matrix:
node-version: ${{ fromJson(needs.matrix.outputs.stable) }}

steps:
- uses: actions/checkout@v2
Expand All @@ -39,7 +41,10 @@ jobs:
runs-on: ubuntu-latest

strategy:
matrix: ${{ fromJson(needs.matrix.outputs.unstable) }}
matrix:
node-version: ${{ fromJson(needs.matrix.outputs.unstable) }}
exclude:
- node-version: '0.6'

steps:
- uses: actions/checkout@v2
Expand Down
59 changes: 50 additions & 9 deletions lib/test.js
Expand Up @@ -69,6 +69,7 @@ function Test(name_, opts_, cb_) {
this._plan = undefined;
this._cb = args.cb;
this._progeny = [];
this._teardown = [];
this._ok = true;
var depthEnvVar = process.env.NODE_TAPE_OBJECT_PRINT_DEPTH;
if (args.opts.objectPrintDepth) {
Expand Down Expand Up @@ -190,6 +191,14 @@ Test.prototype.end = function (err) {
this._end();
};

Test.prototype.teardown = function (fn) {
if (typeof fn !== 'function') {
this.fail('teardown: ' + inspect(fn) + ' is not a function');
} else {
this._teardown.push(fn);
}
};

Test.prototype._end = function (err) {
var self = this;

Expand All @@ -202,16 +211,48 @@ Test.prototype._end = function (err) {
return;
}

if (!this.ended) this.emit('end');
var pendingAsserts = this._pendingAsserts();
if (!this._planError && this._plan !== undefined && pendingAsserts) {
this._planError = true;
this.fail('plan != count', {
expected: this._plan,
actual: this.assertCount
});

function next(i) {
if (i === self._teardown.length) {
completeEnd();
return;
}
var fn = self._teardown[i];
var res;
try {
res = fn();
} catch (e) {
self.fail(e);
}
if (res && typeof res.then === 'function') {
res.then(function () {
next(++i);
}, function (_err) {
err = err || _err;
});
} else {
next(++i);
}
}

if (this._teardown.length > 0) {
next(0);
} else {
completeEnd();
}

function completeEnd() {
if (!self.ended) self.emit('end');
var pendingAsserts = self._pendingAsserts();
if (!self._planError && self._plan !== undefined && pendingAsserts) {
self._planError = true;
self.fail('plan != count', {
expected: self._plan,
actual: self.assertCount
});
}
self.ended = true;
}
this.ended = true;
};

Test.prototype._exit = function () {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -41,10 +41,12 @@
"through": "^2.3.8"
},
"devDependencies": {
"array.prototype.flatmap": "^1.2.4",
"aud": "^1.1.4",
"concat-stream": "^1.6.2",
"eclint": "^2.8.1",
"ecstatic": "^4.1.4",
"es-value-fixtures": "^1.2.1",
"eslint": "^7.20.0",
"falafel": "^2.2.4",
"js-yaml": "^3.14.0",
Expand Down
4 changes: 4 additions & 0 deletions readme.markdown
Expand Up @@ -170,6 +170,10 @@ If `cb` returns a Promise, it will be implicitly awaited. If that promise reject

Generate a new test that will be skipped over.

## test.teardown(cb)

Register a callback to run after the individual test has completed. Multiple registered teardown callbacks will run in order. Useful for undoing side effects, closing network connections, etc.

## test.onFinish(fn)

The onFinish hook will get invoked when ALL `tape` tests have finished right before `tape` is about to print the test summary.
Expand Down
8 changes: 8 additions & 0 deletions test/common.js
Expand Up @@ -69,6 +69,14 @@ module.exports.stripFullStack = function (output) {
// Handle stack trace variation in Node v0.8
/at(:?) Test\.(?:module\.exports|tap\.test\.err\.code)/g,
'at$1 Test.<anonymous>'
).replace(
// Handle stack trace variation in Node v0.8
/at(:?) (Test\.)?tap\.test\.test\.skip/g,
'at$1 $2<anonymous>'
).replace(
// Handle stack trace variation in Node v0.8
/(\[\.\.\. stack stripped \.\.\.\]\n *at) <anonymous> \(([^)]+)\)/g,
'$1 $2'
).split('\n');
};

Expand Down