Skip to content

Teardown functions should run in reverse order #695

@tobias-walle

Description

@tobias-walle

I have a test that looks like the following (Pseudocode):

const db = connectToDatabase();
t.teardown(() => db.drop()); // drop-database

const connection = db.connect();
t.teardown(() => connection.close()); // close-connection

If I run the test I get an error, because I can't drop the database with open connections.

This is because tap runs the teardown functions in the order in which there are defined:

  1. drop-database
  2. close-connection

I would expect that they are run in the reverse order:

  1. close-connection
  2. drop-database

This way all connections would be closed before the database is dropped. This would also match the behavior of other test runners:

  • ava (must be configured, default in the next major release) Add ability to reverse the order of teardown steps avajs/ava#2495
  • jest. I didn't find good documentation explaining this but if I run:
    beforeAll(() => console.log('Before each 1'))
    afterAll(() => console.log('After all 1'))
    beforeAll(() => console.log('Before each 2'))
    afterAll(() => console.log('After all 2'))
    it('should test', () => console.log('Test'))
    
    It logs:
    Before each 1
    Before each 2
    Test
    After all 2
    After all 1
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions