Testing Fundamentals — Vitest and Jest — Concept Check
Questions on Testing Fundamentals — Vitest and Jest. Arrange, act, assert — the structure is the same as JUnit or pytest. What differs is how mocking works and how async assertions behave.
Questions in this quiz
8 questions on Testing Fundamentals — Vitest and Jest — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In a Vitest test, what happens if you forget to use 'await' with an async assertion?
- AThe test fails immediately with an error
- BThe test automatically retries the assertion
- CThe test passes silently even if the assertion fails
- DThe test hangs indefinitely
2.A developer uses 'vi.useFakeTimers()' in a test but forgets to call 'vi.useRealTimers()' afterwards. What is a potential consequence?
- ATimers will automatically reset after each test
- BThe test will not execute any timers
- CSubsequent tests may fail due to timer-related issues
- DThe test will run slower
3.Why is it recommended to mock at the network layer with MSW rather than mocking your own fetch code?
- AIt reduces the test execution time
- BIt avoids the need for async functions
- CIt allows testing of the actual fetch logic
- DIt simplifies the test setup
4.What is the risk of not using 'expect.assertions()' in an async test that expects a rejection?
- AThe test will throw an error immediately
- BThe test will always fail
- CThe test will take longer to execute
- DThe test may pass without executing any assertions
5.A test using 'vi.fn()' spies on a function but the spy is not reset between tests. What might occur?
- AState from one test may leak into another
- BThe spy will automatically reset
- CThe function will not be called
- DThe test will fail to execute
6.Which matcher should be used to verify that an object has a specific subset of properties?
- Aexpect(obj).toEqual({ id: 1 })
- Bexpect(obj).toStrictEqual({ id: 1 })
- Cexpect(obj).toMatchObject({ id: 1 })
- Dexpect(obj).toBe({ id: 1 })
7.When using 'it.each' in Vitest, what is the purpose of the parameters provided in the array?
- AThey define different scenarios for the same test logic
- BThey determine the number of times the test runs
- CThey set up different test environments
- DThey specify the order of test execution
8.A developer believes that 'expect(fn).toThrow()' will catch any thrown error in an async function without 'await'. What's the flaw in this belief?
- AIt will throw an error immediately
- B'expect(fn).toThrow()' only works with synchronous functions
- CThe test will automatically retry the assertion
- DWithout 'await', the promise may resolve before the error is caught