- Checkout
simple_setup
branch - yarn install
Run:
yarn tl
You should see some logs show up and this should give you some idea of the lifecycle.
🎉🎉 Congrats! You have a working setup!
Set of global
variables → made available in all test environments
- Checkout
explore_globals
branch - See
jest.config.js
module.exports = {
globals: {
__NODE_DEBUG__: process.env.NODE_DEBUG,
},
};
- Accessing
__NODE_DEBUG__
value
console.log(`Current __NODE_DEBUG__ value is ${global.__NODE_DEBUG__}`);
Try running:
yarn tl
yarn tlb
(this runs the tests one after another and uses--runInBand
)
Result:
- Any mutation won't reflect between tests (even in the same test runs) although the mutation will be reflected for that particular test file
- so any changes in
it
block is seen inafterEach
block
- so any changes in
- Values for global variables must be json-serializable.
- You can't create a
function
and try using thisfunction
in your test! - Use
setupFiles
config instead
- You can't create a
(globalSetup
Documentation)
(globalTeardown
Documentation)
- Checkout
global_setup_teardown
branch - See
jest.config.js
globalSetup: './configFiles/globalSetup.js',
globalTeardown: './configFiles/globalTeardown.js',
Try running:
yarn tl
yarn tlb
Results:
- Any
global
variable defined inglobalSetup
can be read inglobalTeardown
but it cannot be read by any of the tests or test-suite global
variable defined injest.config.js
cannot be read inglobalSetup
andglobalTeardown
- Execution Sequence:
globalSetup
→ ...[beforeAll
,beforeEach
,test
,afterEach
,afterAll
]... →globalTeardown
globalConfig
passed toglobalSetup
andglobalTeardown
functions cannot bo modified!
(setupFiles
Documentation)
(setupFilesAfterEnv
Documentation)
- Checkout
setupfiles
branch - See
jest.config.js
setupFiles: ['./configFiles/setupFile1.js', './configFiles/setupFile2.js'],
setupFilesAfterEnv: [
'./configFiles/setupFileAfterEnv1.js',
'./configFiles/setupFileAfterEnv2.js',
],
Try running:
yarn tlb
(--runInBand
to see the behavior)
Result:
setupFiles
andsetupFileAfterEnv
can access variables declared usingglobal
setupFiles
can be used to create and set functions onglobal
and these can be accessed insetupFilesAfterEnv
as well as in your Test files (ex:funnyHelloWorld
function created bysetupFile1.js
)setupFiles
andsetupFileAfterEnv
are executed before each test filesetupFiles
is expected to be used to run some code to setup your environment before every test is run- you don't have access to functions like
beforeAll
,beforeEach
,afterAll
,afterEach
- you don't have access to functions like
setupFilesAfterEnv
is expected to be used to configure the testing framework before each test file but after the the testing framework is installed in the environment. Think of it as the right place to run some common code that needs to happen for every test to customize the testing framework for your needs.- you have access to functions like
beforeAll
,beforeEach
,afterAll
,afterEach
- you have access to functions like
- Lifecycle looks like:
- globalSetup
- For each test file
- setupFile
- setupFileAfterEnv
- beforeAll from setupFileAfterEnv
- beforeAll from test file
- beforeEach from setupFileAfterEnv
- beforeEach from test file
- set of test from the file
- afterEach from test file
- afterEach from setupFileAfterEnv
- afterAll from test file
- afterAll from setupFileAfterEnv
- globalTeardown
(testEnvironment
Documentation)
(testEnvironmentOptions
Documentation)
- Checkout
explore_globals
branch - run
yarn install
- See
jest.config.js
testEnvironment: './configFiles/customTestEnvironment.js',
testEnvironmentOptions: {
teKey1: 'teValue1',
teKey2: 'teValue2',
},
testRunner: 'jest-circus/runner', // we are including this as well!
Try running:
yarn tl
Result:
- Lifecycle looks like:
- globalSetup
- For each test file
- customTestEnvironment is instantiated:
constructor
- setupFile
- customTestEnvironment
setup
- setupFileAfterEnv
- beforeAll from setupFileAfterEnv
- beforeAll from test file
- customTestEnvironment
handleTestEvent
- test_start - beforeEach from setupFileAfterEnv
- beforeEach from test file
- customTestEnvironment
handleTestEvent
- test_fn_start - set of tests from the file
- afterEach from test file
- afterEach from setupFileAfterEnv
- afterAll from test file
- afterAll from setupFileAfterEnv
- customTestEnvironment
teardown
- customTestEnvironment is instantiated:
- globalTeardown
runScripts
is not used ifgetVmContext
is defined in customTestEnvironmenthandleTestEvent
is used byjest-circus/runner
and if it is not set as your runner, this function is never calledsetup
can be used to set up more variables or functions on global (example, we set up__NEW_VALUE__
in our code) and they will be available in tests for consumption- values passed using
testEnvironmentOptions
injest.config.js
can be accessed incustomTestEnvironment
and in our tests as well
- Checkout
runner
branch - See
jest.config.js
runner: './configFiles/customRunner.js',
Try running:
yarn tl
Results:
- you can create your own custom Runner (default is
jest-runner
in previous example we usedjest-circus
) - Lifecycle looks like:
- globalSetup
customRunner
gets initializedcustomRunner
'srunTests
is called with with list of tests (responsible for running these tests)- For each test file (based on logic inside
runTests
these test files can run in parallel or sequentially)- customTestEnvironment is instantiated:
constructor
- setupFile
- customTestEnvironment
setup
- setupFileAfterEnv
- beforeAll from setupFileAfterEnv
- beforeAll from test file
- customTestEnvironment
handleTestEvent
- test_start - beforeEach from setupFileAfterEnv
- beforeEach from test file
- customTestEnvironment
handleTestEvent
- test_fn_start - set of tests from the file
- afterEach from test file
- afterEach from setupFileAfterEnv
- afterAll from test file
- afterAll from setupFileAfterEnv
- customTestEnvironment
teardown
- customTestEnvironment is instantiated:
- globalTeardown
- We rely on default
jest-runner
'srunTest
function in the code (I am sure we can build our own as well!)- this internally calls
jest-jasmine
orjest-circus/runner
(whenprocess.env.JEST_CIRCUS === '1'
)
- this internally calls
- We can also customize the execution a little bit by modifying the value returned by
runTest
We forced CLI to display ✕
even though the test passed by modifying the value to failed
- Checkout
testRunner
branch - See
jest.config.js
testRunner: './configFiles/customTestRunner.js',
Try running:
yarn tl
Results:
- You can create your own custom Test Runner (default is
jest-jasmine2
or we can even usejest-circus/runner
) - Lifecycle looks like:
- globalSetup
customRunner
gets initializedcustomRunner
'srunTests
is called with with list of tests (responsible for running these tests)- For each test file (based on logic inside
runTests
these test files can run in parallel or sequentially)customTestEnvironment
is instantiated:constructor
- setupFiles
customTestEnvironment
->setup()
customTestRunner
is invoked- setupFileAfterEnv
- beforeAll from setupFileAfterEnv
- beforeAll from test file
- customTestEnvironment
handleTestEvent
- test_start (only withjest-circus
) - beforeEach from setupFileAfterEnv
- beforeEach from test file
- customTestEnvironment
handleTestEvent
- test_fn_start (only withjest-circus
) - set of tests from the file
- afterEach from test file
- afterEach from setupFileAfterEnv
- afterAll from test file
- afterAll from setupFileAfterEnv
- customTestEnvironment
teardown
- globalTeardown
- This is responsible for running the actual test and knows what to do when we encounter test functions like
beforeAll
,beforeEach
,afterAll
,afterEach
, 'it
ordescribe
In the code example, the customTestRunner
randomly picks one of jest-jasmine2
or jest-circus/runner
and runs the tests through them.