Jest matchers for Dynamic Api testing.
npm install @universal-packages/dynamic-api-jest
npm install @universal-packages/dynamic-api
Add the following to your jest.config.js
or where you configure Jest:
module.exports = {
setupFilesAfterEnv: ['@universal-packages/dynamic-api-jest']
}
import { MyDynamic } from './MyDynamic'
it('should have performed MyDynamic', async () => {
await dynamicApi.perform('my-dynamic')
expect(MyDynamic).toHaveBeenPerformed()
})
import { MyDynamic } from './MyDynamic'
it('should have performed MyDynamic with payload', async () => {
await dynamicApi.perform('my-dynamic', { foo: 'bar' })
expect(MyDynamic).toHaveBeenPerformedWith({ foo: 'bar' })
})
import { MyDynamic } from './MyDynamic'
import { MyHook } from './MyHook'
it('should have hooked MyHook before MyDynamic', async () => {
await dynamicApi.perform('my-dynamic')
expect(MyHook).toHaveBeenHookedBefore(MyDynamic)
})
import { MyDynamic } from './MyDynamic'
import { MyHook } from './MyHook'
it('should have hooked MyHook after MyDynamic', async () => {
await dynamicApi.perform('my-dynamic')
expect(MyHook).toHaveBeenHookedAfter(MyDynamic)
})
If you need to force a dynamic to return a specific value, you can use the mockDynamicReturnValue
function.
import { MyDynamic } from './MyDynamic'
it('should have performed MyDynamic', async () => {
dynamicApiJest.mockDynamicReturnValue(MyDynamic, { foo: 'bar' })
const result = await dynamicApi.perform('my-dynamic')
expect(MyDynamic).toHaveBeenPerformed()
expect(result).toEqual({ foo: 'bar' })
})
In order for typescript to see the global types you need to reference the types somewhere in your project, normally ./src/globals.d.ts
.
/// <reference types="@universal-packages/dynamic-api-jest" />
This library is developed in TypeScript and shipped fully typed.
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.