orphic-cypress
Version:
Set of utilities and typescript transformers to cover storybook stories with cypress component tests
67 lines (66 loc) • 2.73 kB
TypeScript
import { StoryFileCy } from "./types";
/**
* A function which will gather the required storybook files.
* Often, require strings need to be hard coded. Ours looked like this
*
* ```ts
* const requireFileCallback: RequireFileCallback = (fullFilePath) => {
* const replaced = fullFilePath
* .replace("src/app/", "")
* .replace("src/common/", "");
* // We have to give webpack a little bit to hook onto, so we remove
* // the module entrypoint and include that directly as a string to `require`
* if (fullFilePath.startsWith("src/app")) {
* return require("app/" + replaced);
* }
* if (fullFilePath.startsWith("src/common")) {
* return require("common/" + replaced);
* }
* return;
* };
* ```
*/
export type RequireFileCallback = (fullFilePath: string) => StoryFileCy | undefined;
/**
* Execute all tests as part of one large cypress describe block.
* Put it into a file like `mount.cy.ts` with
*
* ```ts
* import { mountTest } from "./test";
*
* // if the full file needs to be skipped for some reason, instead of just
* // putting `cySkip: true` on the default export for that file. E.g. if
* // the file uses webpack plugins that you don't want to bother with
* const skipFiles = [
* "src/common/components/SomeComponent/index.stories.tsx",
* "src/app/other/component/index.stories.tsx",
* ];
* mountTest(skipFiles);
* ```
*/
export declare const mountTest: (skipFiles?: string[], requireFileCallback?: RequireFileCallback, description?: string) => void;
/**
* Recursively look for files in a provided directory that include a pattern, `.stories.ts`
* by default. Could be done easily with the `glob` library, but this is simple enough to
* keep locally maintained. See {@link setStorybookFiles} for use inside `setupNodeEvents`
*/
export declare const getStorybookFiles: (dir: string, storyPattern?: string | RegExp) => string[];
/**
* Get storybook files recursively, then make them available at
* `Cypress.env("storybookFiles")`. Put this in `setupNodeEvents` if either
* opting for the mountTest style of tests or if you want to maintain the
* option of switching to isolated component files.
*
* Drop in to `setupNodeEvents` for `component` tests in cypress.config.ts
* If this is the only thing you're doing there, could look like
* `setupNodeEvents: setStorybookFiles`. Otherwise:
*
* ```ts
* setupNodeEvents: (on, config) => {
* on.task({...});
* setStorybookFiles(on, config, optionalStoryPattern);
* return config; // be sure to return config
* },
* ```
*/
export declare const setStorybookFiles: (on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions, storyPattern?: string | RegExp) => Cypress.PluginConfigOptions;