Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cypress-codegen

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cypress-codegen

A [Cypress](https://www.cypress.io/) plugin which automatically adds and enables IntelliSense for your [custom commands](https://docs.cypress.io/api/cypress-api/custom-commands)!

  • 1.1.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

cypress-codegen

A Cypress plugin which automatically adds and enables IntelliSense for your custom commands!

Table of Contents

Why Do I Need This Plugin?

The process for adding Cypress custom commands to test suites is quite manual and involves bloating projects with too much boilerplate code. Additionally, custom commands are hard to write because we don't get IntelliSense or the ease of navigating to the command's definition. The cypress-codegen plugin will dynamically import all of your project's custom commands and will even enable IntelliSense and "go to definition" shortcuts!

Installation

npm i --save-dev cypress-codegen

Usage

Model your Cypress project exactly like the one in this repository!

  1. Add the required plugin code to cypress.config.ts like so:
import { cypressCodegen } from 'cypress-codegen/dist/plugin';
import { defineConfig } from 'cypress';

export default defineConfig({
    e2e: {
        setupNodeEvents(on, config) {
            cypressCodegen(on, config);
            return config;
        }
    },

    component: {
        setupNodeEvents(on, config) {
            cypressCodegen(on, config);
            return config;
        },
        devServer: {
            framework: 'create-react-app',
            bundler: 'webpack'
        }
    }
});
  1. Add to cypress/support/component.ts and/or cypress/support/e2e.ts:
import 'cypress-codegen';
  1. Put all of your custom commands in cypress/commands as regular functions. It is recommended to separate each command into its own file of the same name.

  2. Run any Cypress test, and cypress-codegen will:

    • load these functions as valid custom commands
    • generate a special Cypress type definition for each function which will enable IntelliSense and "go to definition" shortcuts in your Cypress test code

Example

Check out this project's cypress directory for a generic example!

Custom Command Chaining

If you want to create custom commands that are meant to be scoped to a previous command's result, put Scoped at the end of your custom command function name (e.g. myCustomCommandScoped). This will set the prevSubject option when adding the custom command. See the Cypress docs for more details.

Code Styling

cypress-codegen will attempt to read your prettierrc config by default. However, you may pass a config override in the cypress.config.ts invocation:

setupNodeEvents(on, config) {
    cypressCodegen(on, config, prettierConfigOverride);
    return config;
}

Configuration

Javascript

The IntelliSense codegen feature is enabled by default. To disable the codegen feature (perhaps for a Javascript Cypress project), set the CODEGEN Cypress environment variable to false. You will still get the benefit of the custom commands being loaded automatically without even having to invoke Cypress.Commands.add()!

CYPRESS_CODEGEN=false npx cypress run

or in cypress.json:

{
    "env": {
        "CODEGEN": false
    }
}

FAQs

Package last updated on 07 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc