Test Assertions in Handler
When would I want to do this?
typically when using [mocq]
to build out a long lived test, something like an integration test, you'll want the handler to
run some kind of create function with the generated data then query and assert
this is useful when building out a workflow where data in the data store will change during the
[mocq]
execution, allowing for assertions on intermediate values (something like acreate
>edit
>delete
>recreate
)
- JavaScript
- TypeScript
import { expect, test } from 'bun:test';
import { mocq } from 'mocq';
test('asserting in mocq handler', async () => {
const count = 1;
const { execute } = mocq({
nodes: {
generator: (i) => ({ index: i }),
count,
handler: (nodes) => {
expect(true).toEqual(true);
expect(nodes.length).toEqual(count);
expect(nodes[0]).toEqual({ index: 0 });
},
};
});
const { data: { nodes } } = await execute();
});
import { expect, test } from 'bun:test';
import { mocq } from 'mocq';
test('asserting in mocq handler', async () => {
const count = 1;
const { execute } = mocq({
nodes: {
generator: (i: number) => ({ index: i }),
count,
handler: (data: {index: number}[]) => {
expect(true).toEqual(true);
expect(data.length).toEqual(count);
expect(data[0]).toEqual({ index: 0 });
},
};
});
const { data: { nodes } } = await execute();
});
Test Output
bun test v1.0.13 (f5bf67bd)
assert.test.ts:
✓ asserting in mocq handler [1.67ms]
1 pass
0 fail
3 expect() calls
Ran 1 tests across 1 files. [24.00ms]