Assert call order
This guide adds expect.order to your tests with the
@repobuddy/vitest/setup/order setup file.
-
Add the setup file to
test.setupFilesin each config that needs it:import { defineConfig } from 'vitest/config'import { nodeTestPreset } from '@repobuddy/vitest/config/node'export default defineConfig({plugins: [nodeTestPreset()],test: { setupFiles: ['@repobuddy/vitest/setup/order'] },}) -
In a test, plan the number of steps with
expect.order.plan(n). -
Call
o.once(step)at each step, numbering from1. -
Call
o.end()after the code under test finishes. It throwsInvalidOrderif a planned step was not reached.
expect.order is typed once the setup file is in the config. You do not import anything in the test.
Finished test
Section titled “Finished test”it('calls the callbacks in order', () => { const o = expect.order.plan(2)
subject.on('start', () => o.once(1)) subject.on('end', () => o.once(2))
subject.run()
o.end()})Verify
Section titled “Verify”- Run the test. It passes when
startfires beforeend. - Swap the two step numbers and run it again.
once()throwsInvalidOrder.
Related
Section titled “Related”@repobuddy/vitest/setup/order@repobuddy/test: useinstallOrder()with another test runner