Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
setup and teardown in tape
var test = require('tape');
test('setup', function (t) {
// ...
t.end();
});
// tests go here
test('teardown', function (t) {
// ...
t.end();
});
@seadb

This comment has been minimized.

Copy link

commented Apr 22, 2016

is there a specific reason you make them tests instead of just regular functions?

@bySabi

This comment has been minimized.

Copy link

commented Jun 13, 2016

@seadb Probably cause tape test run serially. This gist ensure that it will run in the correct order:

  • first setup
  • tests ...
  • last teardown

tape test are callbacks, run async. If you make setup a function you can ensure it called first but teardown probably run before tests if you do something like this:

...
setup();

// tests go here

teardown();
...
@u84six

This comment has been minimized.

Copy link

commented Sep 8, 2017

But the problem is that the output includes them as tests. So if you have only one test that needs to pass, the output will say "tests 3, passed 3". Kind of nit-picky, but would be nice to have a way to flag the functions to not be included in the test results.

@ThompsonJonM

This comment has been minimized.

Copy link

commented Nov 15, 2017

What I have done is declare variables of "beforeEach" and "afterEach," then use the variables in place of "setup" and "teardown."

`const beforeEach = require('tape');
const afterEach = require('tape');

beforeEach('this should happen before testing, function(t) {
doTheThingBeforeEachTest()
t.end()
}

TEST

afterEach('this should happen after testing, function(t) {
doTheThingAfterEachTest()
t.end()
}`

When you run the test, the TAP will return 1 test despite there being a beforeEach, a test, and an afterEach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.