question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Specify setup/teardown for each test individually

See original GitHub issue

Is your feature request related to a problem or a nice-to-have?? Please describe.

Currently, we can:

  1. Define a setup/teardown for a describe block by using before/after.
  2. Define a setup/teardown for all it blocks by using beforeEach/afterEach.

There is no way to define a setup/teardown for one it block.

As a workaround, I’m putting the setup code inside the it block. Downside:

  1. The setup code is mixed with the calculation.
  2. The setup time is also counted in the timing.

Describe the solution you’d like

Some proposals:

  1. Make it accept more arguments to define setup/teardown:
    it(
      "my test",
      () => {
        // setup
      },
      () => {
        // test
      },
      () => {
        // teardown
      }
    );
    // look better with an option object
    it({
      name: "my test",
      setup() {
        // ...
      },
      test() {
        // ...
      },
      teardown() {
        // ...
      }
    });
    
  2. Add new block (or reuse before, after) inside it:
    it("my test", () => {
      itBefore(() => {
        // ...
      });
      // test
      itAfter(() => {
        // ...
      });
    });
    
  3. Allows the test to emit an event to indicate the current phase:
    it("my test", function () {
      this.section("setup");
      // ...
      
      this.section("test");
      // ...
      
      this.section("teardown");
      // ...
    });
    

Describe alternatives you’ve considered

Additional context

This should be handy for dynamically-generated tests since they often have to read some data from the disk before running the actual test.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
eight04commented, Jun 2, 2019

That is another workaround. It prints an additional line for each test as the downside: image

1reaction
plroebuckcommented, Jun 2, 2019

Or perhaps more simply, just put a describe around the test (it) that you want to add hooks for setup/teardown. No changes required and backwards/forwards-compatible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set Up and Tear Down State in Your Tests - Apple Developer
When you run a test case, XCTest calls the XCTestCase setUp() class method first. Use this method to set up state common to...
Read more >
Setup and Teardown - Jest
Jest executes all describe handlers in a test file before it executes any of the actual tests. This is another reason to do...
Read more >
Unittest setUp/tearDown for several tests - Stack Overflow
It appears that pydev doesn't use the test suite, but finds all individual test cases and runs them all separately. My solution for...
Read more >
All About setUp() And tearDown() - Medium
When Xcode runs tests, it invokes each test method independently. Therefore each method must prepare and cleanup all the allocations.
Read more >
One-Time Set Up and Tear Down - Java Extreme ... - O'Reilly
As outlined in Recipe 4.6, JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found