Skip to content

Commit

Permalink
Add e2e test with Cypress (#172)
Browse files Browse the repository at this point in the history
* Add e2e test with Cypress

* Try with normal tsconfig.json

* Fix environments

* Avoid build

* Fix tests E2E

* Try with a build version

* Fix coverage

* Improve linting

* Fix cypress

* Enable always cypress records

* Add screenshots
  • Loading branch information
jordisala1991 committed Jan 26, 2022
1 parent 311c70d commit dc2b15b
Show file tree
Hide file tree
Showing 17 changed files with 3,478 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/frontend-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
run: yarn install --frozen-lockfile

- name: Run ESLint to ensure Javascript coding standards
run: yarn run eslint components contexts types hooks layouts lib pages ui
run: yarn run eslint components contexts types hooks layouts lib pages ui tests

- name: Run Prettier to ensure Javascript styling
run: yarn run prettier --check **/*.{js,ts,tsx}
62 changes: 59 additions & 3 deletions .github/workflows/frontend-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ on:
- main

jobs:
frontend-test:
name: Node ${{ matrix.node }}
frontend-unit-test:
name: Node ${{ matrix.node }} - Unit

runs-on: ubuntu-latest

Expand Down Expand Up @@ -53,8 +53,64 @@ jobs:
- name: Install all NPM dependencies
run: yarn install --frozen-lockfile

- name: Execute Jest Javascript tests
- name: Execute Jest Unit tests
run: yarn run test --coverage

- name: Send coverage to Codecov
uses: codecov/codecov-action@v2

frontend-e2e-test:
name: Node ${{ matrix.node }} - E2E

runs-on: ubuntu-latest

defaults:
run:
working-directory: frontend

strategy:
matrix:
node: ['14.15']

steps:
- name: Checkout the repository to Github workspace
uses: actions/checkout@v2

- name: Setup the Node environment
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}

- name: Find Yarn cache directory
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: Recover Yarn cache to speed up builds
uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ runner.os }}-yarn-

- name: Install all NPM dependencies
run: yarn install --frozen-lockfile

- name: Execute Cypress E2E tests
uses: cypress-io/github-action@v2
with:
install: false
build: yarn run build-e2e
start: yarn run start-e2e
wait-on: 'http://localhost:3000'
parallel: true
record: true
working-directory: frontend
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }}

- name: Send coverage to Codecov
uses: codecov/codecov-action@v2
with:
file: coverage/clover.xml
3 changes: 3 additions & 0 deletions frontend/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"env": {
"test": {
"plugins": ["transform-dynamic-import"]
},
"e2e": {
"plugins": ["istanbul"]
}
},
"plugins": [["styled-components", { "ssr": true }]]
Expand Down
6 changes: 6 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

# testing
.coverage
.nyc_output
coverage

# next.js
/.next/
Expand All @@ -35,3 +37,7 @@ yarn-error.log*
.env.development.local
.env.test.local
.env.production.local

# cypress
tests/e2e/screenshots
tests/e2e/videos
14 changes: 14 additions & 0 deletions frontend/.nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"all": true,
"reporter": [
"html",
"clover"
],
"exclude": [
"**/__setups__/**",
"**/__tests__/**",
"**/public/vendor/**",
"**/{jest,next,prettier}.config.js",
"**/**.d.ts"
]
}
5 changes: 4 additions & 1 deletion frontend/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
*.json
*.min.css
*.module.css
node_modules/*
node_modules
coverage
.next
.coverage
15 changes: 15 additions & 0 deletions frontend/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"baseUrl": "http://localhost:3000/",
"testFiles": "**/*.feature",
"fixturesFolder": "tests/e2e/fixtures",
"integrationFolder": "tests/e2e/features",
"pluginsFile": "tests/e2e/plugins",
"screenshotsFolder": "tests/e2e/screenshots",
"supportFile": "tests/e2e/support",
"videosFolder": "tests/e2e/videos",
"env": {
"codeCoverage": {
"url": "/api/__coverage__"
}
}
}
4 changes: 4 additions & 0 deletions frontend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ module.exports = {
"**/*.{js,jsx,ts,tsx}",
"!**/node_modules/**",
"!**/.coverage/**",
"!**/__setups__/**",
"!**/public/vendor/**",
"!**/tests/**",
"!**/{jest,next,prettier}.config.js"
],
moduleNameMapper: {
'\\.(css|less|scss|sss|styl)$': '<rootDir>/node_modules/jest-css-modules',
Expand Down
19 changes: 17 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
"scripts": {
"dev": "NODE_OPTIONS='--inspect' next dev",
"build": "next build",
"build-e2e": "NODE_ENV=e2e next build",
"start": "next start",
"start-e2e": "NODE_ENV=e2e next start",
"build-start": "next build && next start",
"design-tokens": "design-tokens",
"test": "jest --env=jsdom",
"test-watch": "jest --watch --env=jsdom",
"prettify": "prettier --write **/*.{js,ts,tsx}",
"lint:js": "eslint components contexts types hooks layouts lib pages ui",
"lint:fix": "eslint components contexts types hooks hocs layouts lib pages types ui --fix"
"lint:js": "eslint components contexts types hooks layouts lib pages ui tests",
"lint:fix": "eslint components contexts types hooks hocs layouts lib pages types ui tests --fix",
"test:e2e:dev": "cypress open",
"test:e2e:run": "cypress run"
},
"cypress-cucumber-preprocessor": {
"nonGlobalStepDefinitions": true,
"stepDefinitions": "tests/e2e/features"
},
"dependencies": {
"@apollo/client": "3.3.18",
Expand Down Expand Up @@ -46,14 +54,20 @@
"yup": "0.32.9"
},
"devDependencies": {
"@cypress/code-coverage": "3.9.12",
"@runroom/design-tokens": "1.0.0",
"@runroom/npm-scripts": "1.0.8",
"@svgr/webpack": "^6.0.0",
"@testing-library/cypress": "8.0.2",
"@testing-library/jest-dom": "5.14.1",
"@testing-library/react": "12.1.2",
"@types/cypress-cucumber-preprocessor": "4.0.1",
"@types/jest": "27.0.2",
"@typescript-eslint/eslint-plugin": "4.28.5",
"babel-plugin-istanbul": "6.1.1",
"babel-plugin-transform-dynamic-import": "2.1.0",
"cypress": "9.3.1",
"cypress-cucumber-preprocessor": "4.3.1",
"eslint": "7.31.0",
"eslint-config-next": "11.0.1",
"eslint-config-prettier": "8.3.0",
Expand All @@ -64,6 +78,7 @@
"jest-canvas-mock": "2.3.1",
"jest-css-modules": "2.1.0",
"jest-next-dynamic": "1.0.1",
"nyc": "15.1.0",
"prettier": "2.3.2"
}
}
10 changes: 10 additions & 0 deletions frontend/pages/api/__coverage__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*!
* This file is part of the Stooa codebase.
*
* (c) 2020 - present Runroom SL
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

export { default } from '@cypress/code-coverage/middleware/nextjs';
28 changes: 28 additions & 0 deletions frontend/tests/e2e/features/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*!
* This file is part of the Stooa codebase.
*
* (c) 2020 - present Runroom SL
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { Then, When } from 'cypress-cucumber-preprocessor/steps';

When('navigates to {string}', (url = '') => {
cy.visit(url, { timeout: 10000 });
});

When('clicks on {string}', (text = '') => {
cy.findAllByRole('link', { name: text }).first().click({ force: true });
});

Then('sees {string}', (text = '') => {
cy.findByText(text).should('be.visible');

cy.screenshot();
});

Then('gets redirect to {string}', (url = '') => {
cy.location('pathname', { timeout: 10000 }).should('eq', url);
});
13 changes: 13 additions & 0 deletions frontend/tests/e2e/features/home.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Home
As a Stooa user
I can access the Home page

Scenario: User can see the Home page
When navigates to "/"
Then sees "The online fishbowl tool"

Scenario: User will be redirect to register when trying to create a fishbowl
When navigates to "/"
And clicks on "Create a free fishbowl"
Then gets redirect to "/register"
And sees the register form
16 changes: 16 additions & 0 deletions frontend/tests/e2e/features/home/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*!
* This file is part of the Stooa codebase.
*
* (c) 2020 - present Runroom SL
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { Then } from 'cypress-cucumber-preprocessor/steps';

Then('sees the register form', () => {
cy.findByRole('heading', { name: 'Register to get started' });

cy.screenshot();
});
27 changes: 27 additions & 0 deletions frontend/tests/e2e/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
* This file is part of the Stooa codebase.
*
* (c) 2020 - present Runroom SL
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { defaultOptions } from '@cypress/browserify-preprocessor';
import coverage from '@cypress/code-coverage/task';
import { sync } from 'resolve';

const cucumber = require('cypress-cucumber-preprocessor').default // eslint-disable-line @typescript-eslint/no-var-requires

export default function handler(on, config) {
coverage(on, config);

const options = {
...defaultOptions,
typescript: sync('typescript', { baseDir: config.projectRoot })
};

on('file:preprocessor', cucumber(options));

return config;
};
11 changes: 11 additions & 0 deletions frontend/tests/e2e/support/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*!
* This file is part of the Stooa codebase.
*
* (c) 2020 - present Runroom SL
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import '@testing-library/cypress/add-commands';
import '@cypress/code-coverage/support';
1 change: 1 addition & 0 deletions frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"isolatedModules": true,
"sourceMap": true,
"jsx": "preserve",
"types": ["cypress", "@testing-library/cypress"],
"typeRoots": ["./node_modules/@types/*", "./*.d.ts", "./types/*"]
},
"exclude": ["node_modules"],
Expand Down
Loading

0 comments on commit dc2b15b

Please sign in to comment.