09 control methods - ranhs/soda-test GitHub Wiki

Global Control Methods

If you define a method in the test-class with one of the names of the control-mehtods, it will be executing during the test execution according to the follwoing. Note that the global control method do not need decorators.
Types of control-methods:

  • before - this method will be executed during test-run once, before running any of the test-step methods
  • beforeEach - this method will be executed during test-run, before running each of the test-step methods
  • afterEach - this method will be executed during test-run, after running each of the test-step methods
  • after - this method will be exeucted during test-run once, after runnig all of the test-step methods
Any of the above method, may define as async. In this case the next execution (test-step, other control method, or end of test execution) will start only after the async execution of the control method has finished.
Note that those method when defined by their name, are global to the test-class, whether defined in the global context area, or inside of a context.
import { describe, it, TR } from 'soda-test'

@describe('demo global control methods')
class Demo {
    before(): void {
        console.log('***before')
    }
    beforeEach(): void {
        console.log('***beforeEach')
    }
    afterEach(): void {
        console.log('***afterEach')
    }
    after(): void {
        console.log('***after')
    }

    @it()
    test1(): TR {
        console.log('*** test-step 1')
    }

    @it()
    test2(): TR {
        console.log('*** test-step 2')
    }
}

The output of the above is as follow:

  demo global control methods
***before
***beforeEach
*** test-step 1
    √ test1
***afterEach
***beforeEach
*** test-step 2
    √ test2
***afterEach
***after

Context Control Methods

You may also define control method of a specific context. Define a method inside the control (the name is not importent, but don't call it as the name of the control method, or it will be used as global control method). Put decorator with the name of the control method before the method. The effect is accordig to the decorator used as follow:

  • before - a method in a context with this decorator will be executed during test-run once, before running any of the test-step methods of the context
  • beforeEach - a method in a context with this decorator will be executed during test-run, before running each of the test-step methods of the context
  • afterEach - a method in a context with this decorator will be executed during test-run, after running each of the test-step methods of the context
  • after - a method in a context with this decorator will be exeucted during test-run once, after runnig all of the test-step methods of the context
import { expect, describe, context, it, TR, beforeEach, afterEach, before, after } from 'soda-test'

@describe("demo context control methods")
class Demo {
    before(): void {
        console.log('***before-global')
    }

    beforeEach(): void {
        console.log('***beforeEach-global')
    }

    afterEach(): void {
        console.log('***afterEach-global')
    }

    after(): void {
        console.log('***after-global')
    }

    @it()
    test1(): TR {
        console.log('***test1-global')
    }
   
@context('context1')

    @beforeEach()
    beforeEach1(): void {
        console.log('***beforeEach-context1')
    }

    @afterEach()
    afterEach1(): void {
        console.log('***afterEach-context1')
    }

    @before()
    before1(): void {
        console.log('***before-context1')
    }

    @after()
    after1(): void {
        console.log('***after-context1')
    }

    @it()
    test2(): TR {
        console.log('***test2-conext1')
    }

    @it()
    test3(): TR {
        console.log('***test3-context1')
    }

@context('context2')

    @beforeEach()
    beforeEach2(): void {
        console.log('***beforeEach-context2')
    }

    @afterEach()
    afterEach2(): void {
        console.log('***afterEach-context2')
    }

    @before()
    before2(): void {
        console.log('***before-context2')
    }

    @after()
    after2(): void {
        console.log('***after-context2')
    }

    @it()
    test4(): TR {
        console.log('***test4-conext2')
    }

    @it()
    test5(): TR {
        console.log('***test5-context2')
    }

}
  demo context control methods/
***before-global
***beforeEach-global
***test1-global
    √ test1
***afterEach-global
    context1
***before-context1
***beforeEach-global
***beforeEach-context1
***test2-conext1
      √ test2
***afterEach-context1
***afterEach-global
***beforeEach-global
***beforeEach-context1
***test3-context1
      √ test3
***afterEach-context1
***afterEach-global
***after-context1
    context2
***before-context2
***beforeEach-global
***beforeEach-context2
***test4-conext2
      √ test4
***afterEach-context2
***afterEach-global
***beforeEach-global
***beforeEach-context2
***test5-context2
      √ test5
***afterEach-context2
***afterEach-global
***after-context2
***after-global

⚠️ **GitHub.com Fallback** ⚠️