A collection of utilities for to manage Prismic test data
npm install @prismicio/e2e-tests-utilsThis library comprises utility functions tailored for end-to-end tests at Prismic, with a primary focus on tasks like repository creation, configuration, and deletion.
#### Purpose
In the context of testing, it's essential to operate in a controlled environment where the necessary conditions are defined explicitly. To avoid redundant logic across various services for setting up test data, this library consolidates and provides the required functionalities.
``bash`
npm install --save-dev @prismicio/e2e-tests-utils
Useful to run the tests on a clean repository with a controlled configuration, for example in a CI.
`typescript
import { createRepositoriesManager } from "@prismicio/e2e-tests-utils";
import type { RepositoryConfig } from "@prismicio/e2e-tests-utils";
const config: RepositoryConfig = {
locales: ["en-gb", "fr-fr"],
defaultLocale: "en-gb",
// ... see RepositoryConfig for other options
};
const authConfig = { email, password };
const testUtils = createRepositoriesManager({ urlConfig :"https://prismic.io", authConfig});
const repository = await testUtils.createRepository(config);
// A repository is now available with a unique name and configured as required
repository.getBaseURL() // https://my-unique-repo-name.prismic.io
// run your tests, delete the repository
await testUtils.tearDown();
`
Use Custom Types and Shared slices typed from the @prismicio/types-internal library.
If the custom types already exists on the repository, they will be updated.
`typescript
import { createRepositoriesManager } from "@prismicio/e2e-tests-utils";
import {
CustomType,
SharedSlice,
} from "@prismicio/types-internal/lib/customtypes";
const slice: SharedSlice = {
id: "test_slice",
type: "SharedSlice",
name: "A slice demo",
// ...
};
const customType: CustomType = {
id: "test_ct",
label: "A custom type",
// ...
json: {
Main: {
slices: {
type: "Slices",
fieldset: "Slice Zone",
config: {
choices: {
[slice.id]: {
type: "SharedSlice",
},
},
},
},
},
},
};
// Create them individually
await repository.createSlices([slice]);
await repository.createCustomTypes([customType]);
// Or create them via the RepositoryConfig object
const config = { slices: [slice], customTypes: [customType] };
await testUtils.createRepository(config);
`
`typescript`
const token = await testUtils.getUserApiToken();
The library provides a few api clients for Prismic services. For example the Content Api.
`typescript`
const contentApi = repository.getContentApiClient();
const document = await contentApi.getDocumentByID(documentId);
The @prismicio/client package could have been used but had too many abstractions (caching, error handling) that we don't necessarly want for test execution.
`typescript`
const repository = await testUtils.getRepositoryManager(name);
// configure multiple settings and once
await repository.configure(settings);
// or configure them individually
await repository.createRelease("next release");
When you create the manager, you have 2 options:
- Provide the base Prismic url, like createRepositoriesManager({ urlConfig: "https://prismic.io", authConfig: credentials }). The library infers the service urls from the base URL like http://authentication.prismic.iocreateRepositoriesManager({ urlConfig: { baseURL: "https://...", customTypesApi : "https://...", authenticationApi : "https://..."}, authConfig })
- One a dev environment, you might want to have control over each service url. Use
#### Customise the log level
By default, the library logs each operation, this can be changed:
- Set the environment variable to prismic_e2e_tests_utils_silent to true to remove all logs.prismic_e2e_tests_utils_log_level
- Set the environment variable to to info or debug`.