mokacoding

unit and acceptance testing, automation, productivity

Quick beforeEach and afterEach behaviour

I am sometimes unsure of what to expect from nested beforeEach and/or afterEach calls in Quick.

Luckily we can verify how they behave with a simple example spec, which you can find on GitHub:

import Quick

class Spec: QuickSpec {
  override func spec() {
    describe("beforeEach and afterEach behaviour") {
      beforeEach {
        print("⭐️ top before each")
      }

      context("some context") {
        beforeEach {
          print("👉 context before each")
        }

        it("example 1") { print("😊 example 1") }

        it("example 2") { print("😊 example 2") }

        it("example 3") { print("😊 example 3") }

        afterEach {
          print("👉 context after each")
        }
      }

      context("another context") {
        beforeEach {
          print("🍎 context before each")
        }

        it("example 1") { print("😜 example 1") }

        it("example 2") { print("😜 example 2") }

        afterEach {
          print("🍎 context after each")
        }
      }

      afterEach {
        print("⭐️ top after each")
      }
    }
  }
}

If we run this test the console output, cleared of all the test framework information, will be:

behaviour - some context - example 1
⭐️ top before each
👉 context before each
😊 example 1
👉 context after each
⭐️ top after each

behaviour - some context - example 2
⭐️ top before each
👉 context before each
😊 example 2
👉 context after each
⭐️ top after each

behaviour - some context - example 3
⭐️ top before each
👉 context before each
😊 example 3
👉 context after each
⭐️ top after each

behaviour - other context - example 1
⭐️ top before each
🍎 context before each
😜 example 1
🍎 context after each
⭐️ top after each

behaviour - other context - example 2
⭐️ top before each
🍎 context before each
😜 example 2
🍎 context after each
⭐️ top after each

This shows us that all and only the beforeEach and afterEach encountered in the path from the start of the spec are run before and after each it block is executed.

That's important to keep in mind and can open the door to some interesting simplifications of how the spec is written.


I set out to write this post to share what I thought was a gotcha with the behaviour of beforeEach and afterEach have in Quick, but the example project that I made to verify it revealed that my mental model was incorrect. That's for the best, as the actual behaviour is better than what I thought it was.

Get in touch on Twitter @mokagio or leave a comment below if you want to chat about testing with Quick.

Until next time:

👋 Leave the codebase better than you found it.

Vote on Hacker News