Warning
This is an internal-only package that is not intended for publishing outside this monorepo
The testing support library offers the following features:
- Management of Contentful test space data
- Fetching of Contentful test entry data
- MSW handlers for the Experience API and Insights API
- Mock server based on the MSW handlers
[!INFO]
In order to manage test data in a Contentful space, a
.contentfulrc.jsonfile must be appropriately configured inlib/mocksbased upon the supplied.contentfulrc.example.jsonfile.
Ensure you have msw installed in your package:
pnpm add -D mswAdd the following code to your unit test setup script (commonly in test/setup.ts):
import { experienceApiHandlers, insightsApiHandlers } from 'mocks'
import { setupServer } from 'msw/node'
export const server = setupServer(
...experienceApiHandlers.getHandlers(),
...insightsApiHandlers.getHandlers(),
)
beforeAll(() => {
server.listen({ onUnhandledRequest: 'error' })
})
afterAll(() => {
server.close()
})
// reset going both ways, for extra safety!
beforeEach(() => {
server.resetHandlers()
})
afterEach(() => {
server.resetHandlers()
})With this setup, any calls to supported Experience/Insights endpoints will be handled by the MSW handlers. MSW should additionally ensure that any unsupported endpoints are captured and logged with warnings.
Warning
MSW will similarly block any non-related calls to other APIs or networked services, so it is highly encouraged to review MSW's documentation.
Use this simple command to run a mock server instance:
pnpm --filter mocks serveThe server runs in a process attached to the current terminal. It is recommended to use a process manager such as PM2 to manage the mock server as a detached daemon.
To fetch space configuration data (Content Types, etc.) and entries in a given space, use the following command:
pnpm --filter mocks fetch:ctflSpace data will be placed within lib/mocks/src/contentful/data/space/ctfl-space-data.json. Entries
will be placed in the lib/mocks/src/contentful/data/entries directory, with a file for each entry
named according to its entry ID.
Warning
Do not commit updated Contentful space data or entry files to the repository without first consulting the repository maintainers
Ensure your .contentfulrc.json file contains data for the new Contentful space. Then, simply run
the following command:
pnpm --filter mocks upload:ctfl:spaceAutomatic upload of entry data is not yet directly supported by this package.