Skip to content

Latest commit

 

History

History
102 lines (75 loc) · 2.86 KB

File metadata and controls

102 lines (75 loc) · 2.86 KB

Contentful Optimization SDK Suite Testing Support Library & Server

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.json file must be appropriately configured in lib/mocks based upon the supplied .contentfulrc.example.json file.

Using Mocks in Unit Tests

Ensure you have msw installed in your package:

pnpm add -D msw

Add 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.

Using Mocks in Local Dev & E2E Tests

Use this simple command to run a mock server instance:

pnpm --filter mocks serve

The 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.

Updating Local Mocks & Fixtures

To fetch space configuration data (Content Types, etc.) and entries in a given space, use the following command:

pnpm --filter mocks fetch:ctfl

Space 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

Setting Up a New Contentful Test Space

Ensure your .contentfulrc.json file contains data for the new Contentful space. Then, simply run the following command:

pnpm --filter mocks upload:ctfl:space

Automatic upload of entry data is not yet directly supported by this package.