Skip to content

Commit 2751d7b

Browse files
authored
feat: warn and reject invalid resets
* use Cypress v11 * update the github workflows * add testing local server * fixing setting the app * feat: reject reset with an empty object or array * fix: wait for the write promise * handle sync and async write
1 parent c9d037d commit 2751d7b

File tree

12 files changed

+151
-47
lines changed

12 files changed

+151
-47
lines changed

.github/workflows/badges.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
runs-on: ubuntu-20.04
2222
steps:
2323
- name: Checkout 🛎
24-
uses: actions/checkout@v2
24+
uses: actions/checkout@v3
2525

2626
- name: Update version badges 🏷
2727
run: npx -p dependency-version-badge update-badge json-server cypress

.github/workflows/ci.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ jobs:
55
runs-on: ubuntu-20.04
66
steps:
77
- name: Checkout 🛎
8-
uses: actions/checkout@v2
8+
uses: actions/checkout@v3
99

1010
- name: Run tests 🧪
1111
# https://github.com/cypress-io/github-action
12-
uses: cypress-io/github-action@v2
12+
uses: cypress-io/github-action@v4
1313
with:
1414
build: npm test
1515
start: npm start
1616

17+
- name: Run JSON server tests 🧪
18+
uses: cypress-io/github-action@v4
19+
with:
20+
# we have already installed all dependencies above
21+
install: false
22+
start: node ./cypress/fixtures/example-server.js
23+
config: 'baseUrl=http://localhost:4000'
24+
1725
- name: Semantic Release 🚀
1826
uses: cycjimmy/semantic-release-action@v2
1927
with:

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,22 @@ Then set it to be the middleware _before_ the rest of the `json-server` middlewa
8787
```js
8888
const jsonServer = require('json-server')
8989
const reset = require('json-server-reset')
90-
const setApp = require('json-server-reset/src/set-app')
91-
const bodyParser = require('json-server/lib/server/body-parser')
9290

9391
// create json server and its router first
9492
const server = jsonServer.create()
95-
const router = jsonServer.router(db)
96-
// then pass it to json-server-reset
97-
server.use(setApp(server, router.db))
98-
server.use(bodyParser)
93+
const router = jsonServer.router(dataFilename)
94+
server.use(jsonServer.defaults({
95+
static: '.', // optional static server folder
96+
bodyParser: true,
97+
readOnly: false
98+
}))
9999
server.use(reset)
100-
// the rest of middlewares
101-
server.use(jsonServer.defaults())
100+
server.db = router.db
102101
server.use(router)
103102
```
104103

104+
See [example-server.js](./cypress/fixtures/example-server.js)
105+
105106
## Examples
106107

107108
- [Cypress basics workshop](https://github.com/bahmutov/cypress-workshop-basics)

cypress.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { defineConfig } = require('cypress')
2+
3+
module.exports = defineConfig({
4+
video: false,
5+
e2e: {
6+
// We've imported your old cypress plugins here.
7+
// You may want to clean this up later by importing these.
8+
setupNodeEvents (on, config) {
9+
return require('./cypress/plugins/index.js')(on, config)
10+
},
11+
baseUrl: 'http://localhost:3000'
12+
}
13+
})

cypress.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

cypress/integration/spec.js renamed to cypress/e2e/spec.cy.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,37 @@ describe('json-server-reset', () => {
5252
todos: []
5353
})
5454
})
55+
56+
context('invalid input', () => {
57+
it('warns when resetting with non-existing keys', () => {
58+
cy.request('POST', '/reset', { todos: [] })
59+
cy.request('POST', '/reset', { people: [] })
60+
})
61+
62+
it('rejects resetting with an empty object', () => {
63+
cy.request({
64+
method: 'POST',
65+
url: '/reset',
66+
body: {},
67+
failOnStatusCode: false
68+
}).its('status').should('equal', 400)
69+
})
70+
71+
it('rejects resetting without an object', () => {
72+
cy.request({
73+
method: 'POST',
74+
url: '/reset',
75+
failOnStatusCode: false
76+
}).its('status').should('equal', 400)
77+
})
78+
79+
it('rejects resetting with an array', () => {
80+
cy.request({
81+
method: 'POST',
82+
url: '/reset',
83+
body: [],
84+
failOnStatusCode: false
85+
}).its('status').should('equal', 400)
86+
})
87+
})
5588
})

cypress/fixtures/example-server.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const jsonServer = require('json-server')
2+
const reset = require('../..')
3+
const path = require('path')
4+
const dataFilename = path.join(__dirname, 'data.json')
5+
6+
// create json server and its router first
7+
const server = jsonServer.create()
8+
const router = jsonServer.router(dataFilename)
9+
server.use(jsonServer.defaults({
10+
bodyParser: true,
11+
readOnly: false
12+
}))
13+
server.use(reset)
14+
server.db = router.db
15+
server.use(router)
16+
17+
server.listen(4000, () => {
18+
console.log('JSON Server is running on port 4000')
19+
})
File renamed without changes.

package-lock.json

Lines changed: 23 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,22 @@
4242
"unused-deps": "dependency-check --unused --no-dev .",
4343
"cy:open": "cypress open",
4444
"cy:run": "cypress run",
45+
"cy:open:4000": "cypress open --config baseUrl=http://localhost:4000",
4546
"start": "json-server cypress/fixtures/data.json --middlewares ./src/index.js",
47+
"start:server": "node ./cypress/fixtures/example-server",
4648
"e2e": "start-server-and-test 3000 cy:run",
4749
"dev": "start-server-and-test 3000 cy:open",
50+
"dev2": "start-test start:server 4000 cy:open:4000",
4851
"semantic-release": "semantic-release"
4952
},
5053
"devDependencies": {
5154
"ban-sensitive-files": "1.9.18",
5255
"check-more-types": "2.24.0",
53-
"cypress": "9.7.0",
56+
"cypress": "11.2.0",
5457
"dependency-check": "4.1.0",
5558
"deps-ok": "1.4.1",
5659
"git-issues": "1.3.1",
57-
"json-server": "0.17.0",
60+
"json-server": "0.17.1",
5861
"lazy-ass": "1.6.0",
5962
"license-checker": "15.0.0",
6063
"mocha": "4.1.0",
@@ -67,6 +70,6 @@
6770
"debug": "4.3.4"
6871
},
6972
"peerDependencies": {
70-
"json-server": ">=0.15"
73+
"json-server": ">=0.17"
7174
}
7275
}

0 commit comments

Comments
 (0)