Skip to content

Instantly share code, notes, and snippets.

@RobDoan
Forked from textbook/cypress-cra.md
Created March 19, 2023 03:27
Show Gist options
  • Save RobDoan/102fcb9ae37832749b07d08de943a22d to your computer and use it in GitHub Desktop.
Save RobDoan/102fcb9ae37832749b07d08de943a22d to your computer and use it in GitHub Desktop.

Revisions

  1. @textbook textbook revised this gist Oct 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion new-cra-app.sh
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ cat <<-EOF > cypress.config.js
    video: false,
    });
    EOF
    echo "import '@testing-library/cypress/add-commands';" >> cypress/support/commands.js
    echo "\nimport '@testing-library/cypress/add-commands';" >> cypress/support/commands.js

    echo 'Creating basic E2E test'
    mkdir cypress/e2e/
  2. @textbook textbook revised this gist Jun 6, 2022. 2 changed files with 40 additions and 25 deletions.
    29 changes: 17 additions & 12 deletions cypress-cra.md
    Original file line number Diff line number Diff line change
    @@ -13,26 +13,31 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    2. Open the Cypress UI to bootstrap the test setup:

    ```bash
    $ ./node_modules/.bin/cypress open
    $ npx cypress open --e2e
    ```

    NPM installs the executables for all of your installed modules in the `.bin` directory, so you can use this for things you don't want to add to the package file.
    You can quit the Cypress UI after you've had a look at the examples.
    NPX will use the executable in the `node_modules/.bin/` directory, so you can use it for things you don't want to add to the package file.
    3. Delete the example files:
    ```bash
    $ rm -rf ./cypress/fixtures/example.json ./cypress/integration/examples/
    $ rm -rf ./cypress/fixtures/example.json
    ```
    4. Set a base URL in the `cypress.json` per the [best practices](https://docs.cypress.io/guides/references/best-practices.html#Setting-a-global-baseUrl) (I've also disabled automatic video recording):
    4. Set a base URL in the `cypress.config.js` per the [best practices](https://docs.cypress.io/guides/references/best-practices.html#Setting-a-global-baseUrl) (I've also disabled automatic video recording):

    ```json
    {
    "baseUrl": "http://localhost:3000",
    "video": false
    }
    ```js
    const { defineConfig } = require("cypress");
    module.exports = defineConfig({
    e2e: {
    baseUrl: "http://localhost:3000",
    setupNodeEvents(on, config) {
    // implement node event listeners here
    },
    },
    video: false,
    });
    ```
    5. Set up the Testing Library utilities by adding the following to `./cypress/support/commands.js`:
    @@ -61,7 +66,7 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    },
    ```

    8. Create a new test file e.g. `./cypress/integration/home.test.js` with the following:
    8. Create a new test file e.g. `./cypress/e2e/home.cy.js` with the following:

    ```javascript
    it("should load the page", () => {
    36 changes: 23 additions & 13 deletions new-cra-app.sh
    Original file line number Diff line number Diff line change
    @@ -7,33 +7,43 @@ if [[ $# -ne 1 ]]; then
    exit 1
    fi

    echo 'Create default React app'
    npx create-react-app@latest --use-npm "$1"
    echo 'Creating default React app'
    npx create-react-app@latest "$1"

    cd "$1"

    echo 'Install Cypress dependencies'
    echo 'Installing Cypress dependencies'
    npm install \
    cypress \
    eslint-plugin-cypress \
    @testing-library/cypress

    echo 'Opening Cypress, please exit the UI once it has loaded'
    node_modules/.bin/cypress open
    npx cypress open --e2e

    echo 'Removing preinstalled examples'
    rm -rf \
    cypress/fixtures/example.json \
    cypress/integration/examples/
    cypress/fixtures/example.json

    echo 'Updating Cypress configuration'
    echo '{"baseUrl": "http://localhost:3000", "video": false}' \
    | jq \
    | tee cypress.json
    cat <<-EOF > cypress.config.js
    const { defineConfig } = require("cypress");
    module.exports = defineConfig({
    e2e: {
    baseUrl: "http://localhost:3000",
    setupNodeEvents(on, config) {
    // implement node event listeners here
    },
    },
    video: false,
    });
    EOF
    echo "import '@testing-library/cypress/add-commands';" >> cypress/support/commands.js

    echo 'Creating basic E2E test'
    cat <<-EOF > cypress/integration/home.test.js
    mkdir cypress/e2e/
    cat <<-EOF > cypress/e2e/home.cy.js
    it("should load the page", () => {
    cy.visit("/");
    cy.findAllByText(/learn react/i).should("have.length", 1);
    @@ -49,12 +59,12 @@ cat <<-EOF >> .gitignore
    EOF

    echo 'Setting NPM package configuration'
    npm set-script e2e 'cypress run'
    npm set-script lint 'eslint cypress/ src/'
    cat package.json \
    | jq '.scripts.e2e = "cypress run"' \
    | jq '.scripts.lint = "eslint cypress/ src/"' \
    | jq '.eslintConfig.overrides = [{"extends": ["plugin:cypress/recommended"], "files": ["cypress/**/*.js"]}]' \
    | tee package.json

    echo 'Install and configure Cypress'
    echo 'Committing changes'
    git add .
    git commit -m 'Set up Cypress testing'
  3. @textbook textbook revised this gist May 16, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cypress-cra.md
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    ```json
    {
    "baseUrl": "http://localhost:3000",
    "videos": false
    "video": false
    }
    ```
  4. @textbook textbook revised this gist Apr 10, 2021. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions cypress-cra.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    Here is how to add Cypress E2E tests to a Create React App bootstrapped application. Assumes the \*nix command line, you may need to adapt this for a Windows command line (or use WSL or Git Bash).

    1. Install [Cypress](https://www.cypress.io/) and the [Testing Library](https://testing-library.com/docs/cypress-testing-library/intro) utilities for it (to match the helpers CRA installs):
    1. Install [Cypress](https://www.cypress.io/) and the [Testing Library](https://testing-library.com/docs/cypress-testing-library/intro) utilities for it (to match the helpers CRA installs):

    ```bash
    $ npm i {,@testing-library/}cypress
    @@ -10,7 +10,7 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat

    CRA seems to install everything as a production dependency, but you can add `--save-dev` if you like.

    2. Open the Cypress UI to bootstrap the test setup:
    2. Open the Cypress UI to bootstrap the test setup:

    ```bash
    $ ./node_modules/.bin/cypress open
    @@ -20,13 +20,13 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    You can quit the Cypress UI after you've had a look at the examples.

    3. Delete the example files:
    3. Delete the example files:

    ```bash
    $ rm -rf ./cypress/fixtures/example.json ./cypress/integration/examples/
    ```

    4. Set a base URL in the `cypress.json` per the [best practices](https://docs.cypress.io/guides/references/best-practices.html#Setting-a-global-baseUrl) (I've also disabled automatic video recording):
    4. Set a base URL in the `cypress.json` per the [best practices](https://docs.cypress.io/guides/references/best-practices.html#Setting-a-global-baseUrl) (I've also disabled automatic video recording):
    ```json
    {
    @@ -35,21 +35,21 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    }
    ```
    5. Set up the Testing Library utilities by adding the following to `./cypress/support/commands.js`:
    5. Set up the Testing Library utilities by adding the following to `./cypress/support/commands.js`:
    ```javascript
    import "@testing-library/cypress/add-commands";
    ```
    6. Make sure you exclude any videos and screenshots recorded by Cypress from your commits by adding the following to `.gitignore`:
    6. Make sure you exclude any videos and screenshots recorded by Cypress from your commits by adding the following to `.gitignore`:
    ```ignore
    # Cypress
    cypress/screenshots/
    cypress/videos/
    ```
    7. Add a script that runs the E2E tests into the package file:
    7. Add a script that runs the E2E tests into the package file:
    ```json
    "scripts":
    @@ -61,7 +61,7 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    },
    ```
    8. Create a new test file e.g. `./cypress/integration/home.test.js` with the following:
    8. Create a new test file e.g. `./cypress/integration/home.test.js` with the following:
    ```javascript
    it("should load the page", () => {
    @@ -72,9 +72,9 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    This is the same as the default `src/App.test.js`, but running via Cypress rather than Jest.
    9. Start the app in one terminal (`npm start`) then run the tests in another (`npm run e2e`). It should pass!
    9. Start the app in one terminal (`npm start`) then run the tests in another (`npm run e2e`). It should pass!
    10. _[Optional]_ Install the ESLint plugin with `npm install eslint-plugin-cypress` then add the following to the `eslintConfig` object in `package.json`:
    10. Install the ESLint plugin with `npm install eslint-plugin-cypress` then add the following to the `eslintConfig` object in `package.json`:
    ```json
    "overrides": [
    @@ -89,4 +89,4 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    ]
    ```
    This will ensure the linting passes, by adding the Cypress globals, and give some useful hints on good test practices.
    This is optional, but will ensure the linting passes (by adding the Cypress globals) and give some useful hints on good test practices.
  5. @textbook textbook revised this gist Apr 10, 2021. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions cypress-cra.md
    Original file line number Diff line number Diff line change
    @@ -77,16 +77,16 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    10. _[Optional]_ Install the ESLint plugin with `npm install eslint-plugin-cypress` then add the following to the `eslintConfig` object in `package.json`:
    ```json
    "overrides": [
    {
    "extends": [
    "plugin:cypress/recommended"
    ],
    "files": [
    "cypress/**/*.js"
    ]
    }
    ]
    "overrides": [
    {
    "extends": [
    "plugin:cypress/recommended"
    ],
    "files": [
    "cypress/**/*.js"
    ]
    }
    ]
    ```
    This will ensure the linting passes, by adding the Cypress globals, and give some useful hints on good test practices.
  6. @textbook textbook revised this gist Apr 10, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion cypress-cra.md
    Original file line number Diff line number Diff line change
    @@ -87,4 +87,6 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    ]
    }
    ]
    ```
    ```
    This will ensure the linting passes, by adding the Cypress globals, and give some useful hints on good test practices.
  7. @textbook textbook revised this gist Apr 10, 2021. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion cypress-cra.md
    Original file line number Diff line number Diff line change
    @@ -72,4 +72,19 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    This is the same as the default `src/App.test.js`, but running via Cypress rather than Jest.
    9. Start the app in one terminal (`npm start`) then run the tests in another (`npm run e2e`). It should pass!
    9. Start the app in one terminal (`npm start`) then run the tests in another (`npm run e2e`). It should pass!
    10. _[Optional]_ Install the ESLint plugin with `npm install eslint-plugin-cypress` then add the following to the `eslintConfig` object in `package.json`:
    ```json
    "overrides": [
    {
    "extends": [
    "plugin:cypress/recommended"
    ],
    "files": [
    "cypress/**/*.js"
    ]
    }
    ]
    ```
  8. @textbook textbook revised this gist Apr 10, 2021. 1 changed file with 13 additions and 6 deletions.
    19 changes: 13 additions & 6 deletions cypress-cra.md
    Original file line number Diff line number Diff line change
    @@ -26,23 +26,30 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    $ rm -rf ./cypress/fixtures/example.json ./cypress/integration/examples/
    ```

    4. Set a base URL in the `cypress.json` per the [best practices](https://docs.cypress.io/guides/references/best-practices.html#Setting-a-global-baseUrl):
    4. Set a base URL in the `cypress.json` per the [best practices](https://docs.cypress.io/guides/references/best-practices.html#Setting-a-global-baseUrl) (I've also disabled automatic video recording):
    ```json
    {
    "baseUrl": "http://localhost:3000"
    "baseUrl": "http://localhost:3000",
    "videos": false
    }
    ```
    5. Make sure you exclude any videos and screenshots recorded by Cypress from your commits by adding the following to `.gitignore`:
    5. Set up the Testing Library utilities by adding the following to `./cypress/support/commands.js`:
    ```javascript
    import "@testing-library/cypress/add-commands";
    ```
    6. Make sure you exclude any videos and screenshots recorded by Cypress from your commits by adding the following to `.gitignore`:
    ```ignore
    # Cypress
    cypress/screenshots/
    cypress/videos/
    ```
    6. Add a script that runs the E2E tests into the package file:
    7. Add a script that runs the E2E tests into the package file:
    ```json
    "scripts":
    @@ -54,7 +61,7 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    },
    ```
    7. Create a new test file e.g. `./cypress/integration/home.test.js` with the following:
    8. Create a new test file e.g. `./cypress/integration/home.test.js` with the following:
    ```javascript
    it("should load the page", () => {
    @@ -65,4 +72,4 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    This is the same as the default `src/App.test.js`, but running via Cypress rather than Jest.
    8. Start the app in one terminal (`npm start`) then run the tests in another (`npm run e2e`). It should pass!
    9. Start the app in one terminal (`npm start`) then run the tests in another (`npm run e2e`). It should pass!
  9. @textbook textbook revised this gist Mar 11, 2021. 1 changed file with 8 additions and 11 deletions.
    19 changes: 8 additions & 11 deletions new-cra-app.sh
    Original file line number Diff line number Diff line change
    @@ -34,17 +34,18 @@ echo "import '@testing-library/cypress/add-commands';" >> cypress/support/comman

    echo 'Creating basic E2E test'
    cat <<-EOF > cypress/integration/home.test.js
    it("should load the page", () => {
    cy.visit("/");
    cy.findAllByText(/learn react/i).should("have.length", 1);
    });
    it("should load the page", () => {
    cy.visit("/");
    cy.findAllByText(/learn react/i).should("have.length", 1);
    });
    EOF

    echo 'Ignoring Cypress test assets'
    cat <<-EOF >> .gitignore
    # cypress
    cypress/screenshots/
    cypress/videos/
    # cypress
    cypress/screenshots/
    cypress/videos/
    EOF

    echo 'Setting NPM package configuration'
    @@ -54,10 +55,6 @@ cat package.json \
    | jq '.eslintConfig.overrides = [{"extends": ["plugin:cypress/recommended"], "files": ["cypress/**/*.js"]}]' \
    | tee package.json


    echo 'Fix code style'
    npm run lint -- --fix

    echo 'Install and configure Cypress'
    git add .
    git commit -m 'Set up Cypress testing'
  10. @textbook textbook revised this gist Mar 11, 2021. 1 changed file with 63 additions and 0 deletions.
    63 changes: 63 additions & 0 deletions new-cra-app.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/bin/bash

    set -euo pipefail

    if [[ $# -ne 1 ]]; then
    echo 'Usage: new-cra-app.sh <app-name>'
    exit 1
    fi

    echo 'Create default React app'
    npx create-react-app@latest --use-npm "$1"

    cd "$1"

    echo 'Install Cypress dependencies'
    npm install \
    cypress \
    eslint-plugin-cypress \
    @testing-library/cypress

    echo 'Opening Cypress, please exit the UI once it has loaded'
    node_modules/.bin/cypress open

    echo 'Removing preinstalled examples'
    rm -rf \
    cypress/fixtures/example.json \
    cypress/integration/examples/

    echo 'Updating Cypress configuration'
    echo '{"baseUrl": "http://localhost:3000", "video": false}' \
    | jq \
    | tee cypress.json
    echo "import '@testing-library/cypress/add-commands';" >> cypress/support/commands.js

    echo 'Creating basic E2E test'
    cat <<-EOF > cypress/integration/home.test.js
    it("should load the page", () => {
    cy.visit("/");
    cy.findAllByText(/learn react/i).should("have.length", 1);
    });
    EOF

    echo 'Ignoring Cypress test assets'
    cat <<-EOF >> .gitignore
    # cypress
    cypress/screenshots/
    cypress/videos/
    EOF

    echo 'Setting NPM package configuration'
    cat package.json \
    | jq '.scripts.e2e = "cypress run"' \
    | jq '.scripts.lint = "eslint cypress/ src/"' \
    | jq '.eslintConfig.overrides = [{"extends": ["plugin:cypress/recommended"], "files": ["cypress/**/*.js"]}]' \
    | tee package.json


    echo 'Fix code style'
    npm run lint -- --fix

    echo 'Install and configure Cypress'
    git add .
    git commit -m 'Set up Cypress testing'
  11. @textbook textbook revised this gist Oct 25, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cypress-cra.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Here is how to add Cypress E2E tests to a Create React App bootstrapped applicat
    $ npm i {,@testing-library/}cypress
    ```

    `i` is short for install, and the braces `{}` are expanded
    `i` is short for install, and the braces `{}` are expanded by [brace expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html) to `cypress @testing-library/cypress`.

    CRA seems to install everything as a production dependency, but you can add `--save-dev` if you like.

  12. @textbook textbook revised this gist Oct 25, 2020. 1 changed file with 9 additions and 5 deletions.
    14 changes: 9 additions & 5 deletions cypress-cra.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,12 @@
    Here is how to add Cypress E2E tests to a Create React App bootstrapped application. Assumes the \*nix command line, you may need to adapt this for a Windows command line (or use WSL or Git Bash).

    1. Install [Cypress](https://www.cypress.io/) and the [Testing Library](https://testing-library.com/docs/cypress-testing-library/intro) utilities for it (to match the helpers CRA installs):

    ```bash
    $ npm install cypress @testing-library/cypress
    $ npm i {,@testing-library/}cypress
    ```

    `i` is short for install, and the braces `{}` are expanded

    CRA seems to install everything as a production dependency, but you can add `--save-dev` if you like.

    @@ -30,15 +34,15 @@
    }
    ```

    - Make sure you exclude any videos and screenshots recorded by Cypress from your commits by adding the following to `.gitignore`:
    5. Make sure you exclude any videos and screenshots recorded by Cypress from your commits by adding the following to `.gitignore`:

    ```ignore
    # Cypress
    cypress/screenshots/
    cypress/videos/
    ```

    - Add a script that runs the E2E tests into the package file:
    6. Add a script that runs the E2E tests into the package file:

    ```json
    "scripts":
    @@ -50,7 +54,7 @@
    },
    ```

    - Create a new test file e.g. `./cypress/integration/home.test.js` with the following:
    7. Create a new test file e.g. `./cypress/integration/home.test.js` with the following:

    ```javascript
    it("should load the page", () => {
    @@ -61,4 +65,4 @@

    This is the same as the default `src/App.test.js`, but running via Cypress rather than Jest.

    - Start the app in one terminal (`npm start`) then run the tests in another (`npm run e2e`). It should pass!
    8. Start the app in one terminal (`npm start`) then run the tests in another (`npm run e2e`). It should pass!
  13. @textbook textbook created this gist Oct 25, 2020.
    64 changes: 64 additions & 0 deletions cypress-cra.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    1. Install [Cypress](https://www.cypress.io/) and the [Testing Library](https://testing-library.com/docs/cypress-testing-library/intro) utilities for it (to match the helpers CRA installs):

    ```bash
    $ npm install cypress @testing-library/cypress
    ```

    CRA seems to install everything as a production dependency, but you can add `--save-dev` if you like.

    2. Open the Cypress UI to bootstrap the test setup:

    ```bash
    $ ./node_modules/.bin/cypress open
    ```

    NPM installs the executables for all of your installed modules in the `.bin` directory, so you can use this for things you don't want to add to the package file.
    You can quit the Cypress UI after you've had a look at the examples.

    3. Delete the example files:

    ```bash
    $ rm -rf ./cypress/fixtures/example.json ./cypress/integration/examples/
    ```

    4. Set a base URL in the `cypress.json` per the [best practices](https://docs.cypress.io/guides/references/best-practices.html#Setting-a-global-baseUrl):

    ```json
    {
    "baseUrl": "http://localhost:3000"
    }
    ```

    - Make sure you exclude any videos and screenshots recorded by Cypress from your commits by adding the following to `.gitignore`:

    ```ignore
    # Cypress
    cypress/screenshots/
    cypress/videos/
    ```

    - Add a script that runs the E2E tests into the package file:

    ```json
    "scripts":
    "e2e": "cypress run",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
    },
    ```

    - Create a new test file e.g. `./cypress/integration/home.test.js` with the following:

    ```javascript
    it("should load the page", () => {
    cy.visit("/");
    cy.findAllByText(/learn react/i).should("have.length", 1);
    });
    ```

    This is the same as the default `src/App.test.js`, but running via Cypress rather than Jest.

    - Start the app in one terminal (`npm start`) then run the tests in another (`npm run e2e`). It should pass!