Git Product home page Git Product logo

cypress-typescript-out-of-the-box's Introduction

Cypress with typescript working out of the box

In order to get typescript out of the box you will need to upgrade to cypress 4.4.0 or later

Prerequisites

  • Node v10+
  • Restore packages by running npm install in repo root dir

Steps from scratch

  • create a new project
npm init
  • add typescript to the project
npm i -D typescript
  • add cypress to the project
npm i -D cypress
  • run cypress for the first time
npx cypress open
  • add typescript configuration file in cypress base dir e.g. cypress/tsconfig.json with config:
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": false,
    "baseUrl": "../node_modules/cypress",
    "target": "es6",
    "lib": ["es6", "dom"],
    "types": ["cypress", "node"]
  },
  "include": ["**/*.ts"],
  "files": ["support/index.d.ts"]
}
  • rename files with extension js in ts
  • add a custom command in cypress/support/commands.ts
Cypress.Commands.add('getByTestId', (value) => {
  return cy.get(`[data-cy=${value}]`);
});
  • add in support dir index.d.ts file with typescript definitions for custom commands (IDE intelisense support) e.g.
// in cypress/support/index.d.ts
// load type definitions that come with Cypress module
/// <reference types="cypress" />

declare namespace Cypress {
  interface Chainable {
    /**
     * Custom command to select DOM element by data-cy attribute.
     * @example cy.getByTestId('greeting')
    */
    getByTestId(value: string): Chainable<Element>
  }
}
  • Include the TypeScript definition file, cypress/support/index.d.ts, with the rest of the source files, by adding following proprety into tsconfig.json
  "files": [
    "support/index.d.ts"
  ]
  • add typescript spec file
describe('Test suite shloud', () => {

  it('pass', () => {
    cy.wrap('foo').should('equal', 'foo');
  });

  it('pass for custom command', () => {
    cy.getByTestId('foo').should('not.exist');
  });

});

Voilà !

You got cypress with typescript working out of the box !

cypress-typescript-out-of-the-box's People

Contributors

skylock avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.