installOrder()
installOrder(expect) defines an order property on the expect you pass and returns the same object.
expect.order.plan(n) then creates an AssertOrder that expects n steps.
Signature
Section titled “Signature”function installOrder<E extends object>(expect: E): E & ExpectWithOrderParameters
Section titled “Parameters”| Name | Type | Default | Effect |
|---|---|---|---|
expect |
object |
required | The runner’s expect. installOrder adds order to it in place. |
Returns
Section titled “Returns”The same expect object, typed as E & ExpectWithOrder.
Behavior
Section titled “Behavior”- It sets
orderwithObject.defineProperty: not enumerable, writable, and configurable.Object.keys(expect)does not list it. - The value is the shared
orderobject. Everyexpectyou install onto gets the same object. - Calling it twice on one
expectreplaces the property with the same value. - It does not change the runner’s types. Augment them yourself (see
ExpectWithOrder) or, with Vitest, use the@repobuddy/vitest/setup/ordersetup file.
const order: OrderApiThe object that installOrder attaches. Use it directly when you do not want to change expect.
| Method | Returns | Effect |
|---|---|---|
plan(steps?: number) |
AssertOrder |
new AssertOrder(steps). With steps, end() throws InvalidOrder unless every planned step ran. Without it, the instance is unplanned and end() returns a number. |
Steps start at 1.
Examples
Section titled “Examples”These examples come from the package’s tests.
Steps that run in order pass:
import { installOrder } from '@repobuddy/test'import { expect, it } from 'vitest'
installOrder(expect)
it('asserts steps run in order', () => { const o = expect.order.plan(2) o.once(1) o.once(2) o.end()})A step out of order throws InvalidOrder at once:
import { InvalidOrder, order } from '@repobuddy/test'import { expect, it } from 'vitest'
it('throws when a step runs out of order', () => { const o = order.plan(2) o.once(1) expect(() => o.once(1)).toThrow(InvalidOrder)})A plan that is not fulfilled throws at end():
it('throws at end() when the plan is not fulfilled', () => { const o = order.plan(2) o.once(1) expect(() => o.end()).toThrow(InvalidOrder)})The message for that case is Planned for 2 step but expecting step 2 when 'end()' is called.
Related
Section titled “Related”- Types:
AssertOrdermethods,InvalidOrder,OrderApi,ExpectWithOrder. - Use
expect.orderwith Jest @repobuddy/vitest: ships@repobuddy/vitest/setup/order.