orphic-cypress
Version:
Set of utilities and typescript transformers to cover storybook stories with cypress component tests
90 lines • 2.69 kB
JavaScript
/**
* @module cypress
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.addTasks = exports.addCommands = void 0;
/**
* Add cypress commands from raw typescript functions.
* This allows smaller definition footprints while keeping documentation
* and go to definition IDE utils.
*
* ```ts
* // original
* Cypress.Commands.add('clickLink', (label) => {
* cy.get('a').contains(label).click()
* });
* // with addCommands
* export const clickLink = (label: string) =>
* cy.get('a').contains(label).click();
* // likely create object with `import * as commands from ...`
* const commands = { clickLink };
* addCommands(commands);
* // type def
* type Commands = typeof commands;
* declare global {
* namespace Cypress {
* interface Chainable extends Commands {
* // can still put types here defined in the old Cypress.Commands.add way
* getInDocument(selector: string): Chainable;
* // overwrite default type, should really accept string | number
* type(text: string | number, options?: Partial<TypeOptions>): Chainable;
* }
* }
* }
* ```
*/
const addCommands = (commands) => {
for (const [key, value] of Object.entries(commands)) {
const chainableKey = key;
if (value.commandOptions) {
Cypress.Commands.add(chainableKey, value.commandOptions, value);
}
else {
Cypress.Commands.add(chainableKey, value);
}
}
};
exports.addCommands = addCommands;
/**
* Add cypress tasks defined as commands so that
* ```ts
* cy.task("doSomething", 1)
* // becomes
* cy.doSomething(1)
* ```
* with type support and go to definition IDE utils.
*
* ```ts
* // likely create object with `import * as tasks from ...`
* const tasks = { getUUID: () => 'a uuid' };
* addTasks(tasks);
* // type def, see above `addCommands` for further namespace extension details
* type Commands = typeof commands & Tasks<typeof tasks>;
* declare global { // ...
* ```
* Then also add tasks in cypress.config.ts
* ```ts
* import * as tasks from "./cypress/support/tasks";
*
* export default defineConfig({
* // ... other config
* setupNodeEvents: (on, config) => {
* on("task", tasks);
* },
* });
* ```
* afterwards, tasks will be available as commands that return well-typed promises
* ```ts
* cy.getUUID().then(uuid => ...)`
* ```
*/
const addTasks = (tasks) => {
for (const key of Object.keys(tasks)) {
const chainableKey = key;
const taskFn = (arg) => cy.task(key, arg);
Cypress.Commands.add(chainableKey, taskFn);
}
};
exports.addTasks = addTasks;
//# sourceMappingURL=add.js.map
;