0
0
Node.jsframework~10 mins

Test lifecycle hooks (before, after) in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run a setup function before all tests.

Node.js
beforeAll(() => [1]);
Drag options to blanks, or click blank then click option'
AinitializeDatabase()
BrunTests()
Ccleanup()
DlogResults()
Attempts:
3 left
💡 Hint
Common Mistakes
Putting test-running code inside beforeAll instead of setup code.
Using cleanup functions in beforeAll instead of afterAll.
2fill in blank
medium

Complete the code to run a cleanup function after each test.

Node.js
afterEach(() => [1]);
Drag options to blanks, or click blank then click option'
AresetMocks()
BstartServer()
CinitializeDatabase()
DrunTests()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to start servers after each test instead of resetting state.
Using afterEach for setup tasks.
3fill in blank
hard

Fix the error in the test lifecycle hook to properly close the server after all tests.

Node.js
afterAll(() => [1]);
Drag options to blanks, or click blank then click option'
AinitializeDatabase()
Bserver.start()
Cserver.close()
DrunTests()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling server.start() in afterAll instead of closing it.
Not cleaning up resources after tests.
4fill in blank
hard

Fill both blanks to run setup before each test and cleanup after each test.

Node.js
beforeEach(() => [1]);
afterEach(() => [2]);
Drag options to blanks, or click blank then click option'
AresetDatabase()
BinitializeMocks()
CclearMocks()
DstartServer()
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up initialization and cleanup functions.
Not resetting mocks properly between tests.
5fill in blank
hard

Fill all three blanks to run setup once before all tests, setup before each test, and cleanup once after all tests.

Node.js
beforeAll(() => [1]);
beforeEach(() => [2]);
afterAll(() => [3]);
Drag options to blanks, or click blank then click option'
AconnectDatabase()
BresetMocks()
CdisconnectDatabase()
DinitializeMocks()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to connect or disconnect the database before or after each test instead of once.
Not initializing mocks before each test.