Skip to content

Commit a154143

Browse files
committed
chore: add e2e tests using Cypress
1 parent 7dd18fa commit a154143

File tree

9 files changed

+124
-2
lines changed

9 files changed

+124
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
.DS_Store
33
npm-debug.log
4+
cypress/videos

cypress.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"baseUrl": "http://localhost:3000",
3+
"videoRecording": false
4+
}

cypress/fixtures/data.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"todos": []
3+
}

cypress/fixtures/example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "hello@cypress.io",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}

cypress/integration/spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-env mocha */
2+
/* global cy */
3+
describe('json-server-reset', () => {
4+
const reset = () => {
5+
cy.request({
6+
method: 'POST',
7+
url: '/reset',
8+
body: {
9+
todos: []
10+
}
11+
})
12+
}
13+
beforeEach(reset)
14+
15+
const getTodos = () => cy.request('/todos').its('body')
16+
17+
const addTodo = () =>
18+
cy.request({
19+
method: 'POST',
20+
url: '/todos',
21+
body: {
22+
id: 1,
23+
title: 'do something'
24+
}
25+
})
26+
27+
it('starts with empty list of todos', () => {
28+
getTodos().should('deep.equal', [])
29+
})
30+
31+
it('can add a todo', () => {
32+
addTodo()
33+
getTodos().should('deep.equal', [{ id: 1, title: 'do something' }])
34+
})
35+
36+
it('can reset todos', () => {
37+
addTodo()
38+
reset()
39+
getTodos().should('deep.equal', [])
40+
})
41+
})

cypress/plugins/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ***********************************************************
2+
// This example plugins/index.js can be used to load plugins
3+
//
4+
// You can change the location of this file or turn off loading
5+
// the plugins file with the 'pluginsFile' configuration option.
6+
//
7+
// You can read more here:
8+
// https://on.cypress.io/plugins-guide
9+
// ***********************************************************
10+
11+
// This function is called when a project is opened or re-opened (e.g. due to
12+
// the project's config changing)
13+
14+
module.exports = (on, config) => {
15+
// `on` is used to hook into various events Cypress emits
16+
// `config` is the resolved Cypress config
17+
}

cypress/support/commands.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ***********************************************
2+
// This example commands.js shows you how to
3+
// create various custom commands and overwrite
4+
// existing commands.
5+
//
6+
// For more comprehensive examples of custom
7+
// commands please read more here:
8+
// https://on.cypress.io/custom-commands
9+
// ***********************************************
10+
//
11+
//
12+
// -- This is a parent command --
13+
// Cypress.Commands.add("login", (email, password) => { ... })
14+
//
15+
//
16+
// -- This is a child command --
17+
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
18+
//
19+
//
20+
// -- This is a dual command --
21+
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
22+
//
23+
//
24+
// -- This is will overwrite an existing command --
25+
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })

cypress/support/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/index.js is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands'
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@
6161
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
6262
"test": "npm run unit",
6363
"unit": "mocha src/*-spec.js",
64-
"unused-deps": "dependency-check --unused --no-dev ."
64+
"unused-deps": "dependency-check --unused --no-dev .",
65+
"cy:open": "cypress open",
66+
"cy:run": "cypress run",
67+
"start": "json-server cypress/fixtures/data.json --middlewares ./src/index.js",
68+
"e2e": "start-server-and-test start http://localhost:3000 cy:run"
6569
},
6670
"release": {
6771
"analyzeCommits": "simple-commit-message"
@@ -73,12 +77,14 @@
7377
"dependency-check": "2.9.1",
7478
"deps-ok": "1.2.1",
7579
"git-issues": "1.3.1",
80+
"json-server": "0.12.1",
7681
"lazy-ass": "1.6.0",
7782
"license-checker": "15.0.0",
7883
"mocha": "4.0.1",
7984
"nsp": "3.1.0",
8085
"pre-git": "3.16.0",
8186
"prettier-standard": "7.0.3",
82-
"standard": "10.0.3"
87+
"standard": "10.0.3",
88+
"start-server-and-test": "1.0.0"
8389
}
8490
}

0 commit comments

Comments
 (0)