This commit is contained in:
typicode
2018-06-04 12:42:09 +02:00
committed by GitHub
parent 0241ad5d35
commit 4c292dfa06
18 changed files with 5435 additions and 2775 deletions

View File

@ -10,5 +10,5 @@ module.exports = {
}, },
] ]
}, },
env: { mocha: true } env: { jest: true }
} }

View File

@ -1,2 +1,3 @@
src src
test __tests__
__fixtures__

View File

@ -12,9 +12,9 @@ const serverReady = require('server-ready')
let PORT = 3100 let PORT = 3100
const middlewareFiles = { const middlewareFiles = {
en: './fixtures/middlewares/en.js', en: './../../__fixtures__/middlewares/en.js',
jp: './fixtures/middlewares/jp.js', jp: './../../__fixtures__/middlewares/jp.js',
postbody: './fixtures/middlewares/postbody.js' postbody: './../../__fixtures__/middlewares/postbody.js'
} }
const bin = path.join(__dirname, '../../lib/cli/bin') const bin = path.join(__dirname, '../../lib/cli/bin')
@ -60,11 +60,11 @@ describe('cli', () => {
serverReady(PORT, done) serverReady(PORT, done)
}) })
it('should support JSON file', done => { test('should support JSON file', done => {
request.get('/posts').expect(200, done) request.get('/posts').expect(200, done)
}) })
it('should send CORS headers', done => { test('should send CORS headers', done => {
const origin = 'http://example.com' const origin = 'http://example.com'
request request
@ -74,7 +74,7 @@ describe('cli', () => {
.expect(200, done) .expect(200, done)
}) })
it('should update JSON file', done => { test('should update JSON file', done => {
request request
.post('/posts') .post('/posts')
.send({ title: 'hello' }) .send({ title: 'hello' })
@ -90,11 +90,11 @@ describe('cli', () => {
describe('seed.js', () => { describe('seed.js', () => {
beforeEach(done => { beforeEach(done => {
child = cli(['fixtures/seed.js']) child = cli(['../../__fixtures__/seed.js'])
serverReady(PORT, done) serverReady(PORT, done)
}) })
it('should support JS file', done => { test('should support JS file', done => {
request.get('/posts').expect(200, done) request.get('/posts').expect(200, done)
}) })
}) })
@ -105,7 +105,7 @@ describe('cli', () => {
serverReady(PORT, done) serverReady(PORT, done)
}) })
it('should support URL file', done => { test('should support URL file', done => {
request.get('/posts').expect(200, done) request.get('/posts').expect(200, done)
}) })
}) })
@ -127,20 +127,20 @@ describe('cli', () => {
serverReady(PORT, done) serverReady(PORT, done)
}) })
it('should use routes.json and _id as the identifier', done => { test('should use routes.json and _id as the identifier', done => {
request.get('/blog/posts/2').expect(200, done) request.get('/blog/posts/2').expect(200, done)
}) })
it('should use _id as foreignKeySuffix', async () => { test('should use _id as foreignKeySuffix', async () => {
const response = await request.get('/posts/1/comments') const response = await request.get('/posts/1/comments')
assert.equal(response.body.length, 1) assert.equal(response.body.length, 1)
}) })
it('should apply middlewares', done => { test('should apply middlewares', done => {
request.get('/blog/posts/2').expect('X-Hello', 'World', done) request.get('/blog/posts/2').expect('X-Hello', 'World', done)
}) })
it('should allow only GET requests', done => { test('should allow only GET requests', done => {
request.post('/blog/posts').expect(403, done) request.post('/blog/posts').expect(403, done)
}) })
}) })
@ -151,7 +151,7 @@ describe('cli', () => {
serverReady(PORT, done) serverReady(PORT, done)
}) })
it('should apply all middlewares', done => { test('should apply all middlewares', done => {
request request
.get('/posts') .get('/posts')
.expect('X-Hello', 'World') .expect('X-Hello', 'World')
@ -165,7 +165,7 @@ describe('cli', () => {
serverReady(PORT, done) serverReady(PORT, done)
}) })
it('should have post body in middleware', done => { test('should have post body in middleware', done => {
request request
.post('/posts') .post('/posts')
.send({ name: 'test' }) .send({ name: 'test' })
@ -179,7 +179,7 @@ describe('cli', () => {
serverReady(PORT, done) serverReady(PORT, done)
}) })
it('should delay response', done => { test('should delay response', done => {
const start = new Date() const start = new Date()
request.get('/posts').expect(200, function(err) { request.get('/posts').expect(200, function(err) {
const end = new Date() const end = new Date()
@ -188,9 +188,9 @@ describe('cli', () => {
}) })
}) })
describe('db.json -s fixtures/public -S /some/path/snapshots', () => { describe('db.json -s ../../__fixtures__/public -S /some/path/snapshots', () => {
const snapshotsDir = path.join(osTmpdir(), 'snapshots') const snapshotsDir = path.join(osTmpdir(), 'snapshots')
const publicDir = 'fixtures/public' const publicDir = '../../__fixtures__/public'
beforeEach(done => { beforeEach(done => {
rimraf.sync(snapshotsDir) rimraf.sync(snapshotsDir)
@ -203,22 +203,22 @@ describe('cli', () => {
}) })
}) })
it('should serve fixtures/public', done => { test('should serve ../../__fixtures__/public', done => {
request.get('/').expect(/Hello/, done) request.get('/').expect(/Hello/, done)
}) })
it('should save a snapshot in snapshots dir', () => { test('should save a snapshot in snapshots dir', () => {
assert.equal(fs.readdirSync(snapshotsDir).length, 1) assert.equal(fs.readdirSync(snapshotsDir).length, 1)
}) })
}) })
describe('fixtures/seed.json --no-cors=true', () => { describe('../../__fixtures__/seed.json --no-cors=true', () => {
beforeEach(done => { beforeEach(done => {
child = cli(['fixtures/seed.js', '--no-cors=true']) child = cli(['../../__fixtures__/seed.js', '--no-cors=true'])
serverReady(PORT, done) serverReady(PORT, done)
}) })
it('should not send Access-Control-Allow-Origin headers', done => { test('should not send Access-Control-Allow-Origin headers', done => {
const origin = 'http://example.com' const origin = 'http://example.com'
request request
@ -238,13 +238,13 @@ describe('cli', () => {
}) })
}) })
describe('fixtures/seed.json --no-gzip=true', () => { describe('../../__fixtures__/seed.json --no-gzip=true', () => {
beforeEach(done => { beforeEach(done => {
child = cli(['fixtures/seed.js', '--no-gzip=true']) child = cli(['../../__fixtures__/seed.js', '--no-gzip=true'])
serverReady(PORT, done) serverReady(PORT, done)
}) })
it('should not set Content-Encoding to gzip', done => { test('should not set Content-Encoding to gzip', done => {
request request
.get('/posts') .get('/posts')
.expect(200) .expect(200)
@ -266,14 +266,14 @@ describe('cli', () => {
serverReady(PORT, done) serverReady(PORT, done)
}) })
it('should watch db file', done => { test('should watch db file', done => {
fs.writeFileSync(dbFile, JSON.stringify({ foo: [] })) fs.writeFileSync(dbFile, JSON.stringify({ foo: [] }))
setTimeout(() => { setTimeout(() => {
request.get('/foo').expect(200, done) request.get('/foo').expect(200, done)
}, 1000) }, 1000)
}) })
it('should watch routes file', done => { test('should watch routes file', done => {
fs.writeFileSync(routesFile, JSON.stringify({ '/api/*': '/$1' })) fs.writeFileSync(routesFile, JSON.stringify({ '/api/*': '/$1' }))
setTimeout(() => { setTimeout(() => {
request.get('/api/posts').expect(200, done) request.get('/api/posts').expect(200, done)
@ -288,7 +288,7 @@ describe('cli', () => {
serverReady(PORT, done) serverReady(PORT, done)
}) })
it("should create JSON file if it doesn't exist", done => { test("should create JSON file if it doesn't exist", done => {
request.get('/posts').expect(200, done) request.get('/posts').expect(200, done)
}) })
}) })
@ -298,7 +298,7 @@ describe('cli', () => {
dbFile = tempWrite.sync(JSON.stringify({ 'a/b': [] }), 'db-error.json') dbFile = tempWrite.sync(JSON.stringify({ 'a/b': [] }), 'db-error.json')
}) })
it('should exit with an error', done => { test('should exit with an error', done => {
child = cli([dbFile]) child = cli([dbFile])
child.on('exit', code => { child.on('exit', code => {
if (code === 1) { if (code === 1) {

View File

@ -6,7 +6,7 @@ const mixins = require('../../src/server/mixins')
describe('mixins', () => { describe('mixins', () => {
let db let db
before(() => { beforeAll(() => {
_.mixin(lodashId) _.mixin(lodashId)
_.mixin(mixins) _.mixin(mixins)
}) })
@ -25,7 +25,7 @@ describe('mixins', () => {
}) })
describe('getRemovable', () => { describe('getRemovable', () => {
it('should return removable documents', () => { test('should return removable documents', () => {
const expected = [ const expected = [
{ name: 'comments', id: 2 }, { name: 'comments', id: 2 },
{ name: 'comments', id: 3 } { name: 'comments', id: 3 }
@ -34,7 +34,7 @@ describe('mixins', () => {
assert.deepEqual(_.getRemovable(db, { foreignKeySuffix: 'Id' }), expected) assert.deepEqual(_.getRemovable(db, { foreignKeySuffix: 'Id' }), expected)
}) })
it('should support custom foreignKeySuffix', () => { test('should support custom foreignKeySuffix', () => {
const expected = [ const expected = [
{ name: 'comments', id: 2 }, { name: 'comments', id: 2 },
{ name: 'comments', id: 3 } { name: 'comments', id: 3 }
@ -45,11 +45,11 @@ describe('mixins', () => {
}) })
describe('createId', () => { describe('createId', () => {
it('should return a new id', () => { test('should return a new id', () => {
assert.equal(_.createId(db.comments), 4) assert.equal(_.createId(db.comments), 4)
}) })
it('should return a new uuid', () => { test('should return a new uuid', () => {
assert.notEqual(_.createId(db.photos), 3) assert.notEqual(_.createId(db.photos), 3)
}) })
}) })

View File

@ -26,7 +26,7 @@ describe('Server with custom foreign key', () => {
}) })
describe('GET /:parent/:parentId/:resource', () => { describe('GET /:parent/:parentId/:resource', () => {
it('should respond with json and corresponding nested resources', () => test('should respond with json and corresponding nested resources', () =>
request(server) request(server)
.get('/posts/1/comments') .get('/posts/1/comments')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -35,7 +35,7 @@ describe('Server with custom foreign key', () => {
}) })
describe('GET /:resource/:id', () => { describe('GET /:resource/:id', () => {
it('should respond with json and corresponding resource', () => test('should respond with json and corresponding resource', () =>
request(server) request(server)
.get('/posts/1') .get('/posts/1')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -44,7 +44,7 @@ describe('Server with custom foreign key', () => {
}) })
describe('GET /:resource?_embed=', () => { describe('GET /:resource?_embed=', () => {
it('should respond with corresponding resources and embedded resources', () => { test('should respond with corresponding resources and embedded resources', () => {
const posts = _.cloneDeep(db.posts) const posts = _.cloneDeep(db.posts)
posts[0].comments = [db.comments[0], db.comments[1]] posts[0].comments = [db.comments[0], db.comments[1]]
posts[1].comments = [db.comments[2]] posts[1].comments = [db.comments[2]]
@ -57,7 +57,7 @@ describe('Server with custom foreign key', () => {
}) })
describe('GET /:resource/:id?_embed=', () => { describe('GET /:resource/:id?_embed=', () => {
it('should respond with corresponding resources and embedded resources', () => { test('should respond with corresponding resources and embedded resources', () => {
const post = _.cloneDeep(db.posts[0]) const post = _.cloneDeep(db.posts[0])
post.comments = [db.comments[0], db.comments[1]] post.comments = [db.comments[0], db.comments[1]]
return request(server) return request(server)
@ -69,7 +69,7 @@ describe('Server with custom foreign key', () => {
}) })
describe('GET /:resource?_expand=', () => { describe('GET /:resource?_expand=', () => {
it('should respond with corresponding resource and expanded inner resources', () => { test('should respond with corresponding resource and expanded inner resources', () => {
const comments = _.cloneDeep(db.comments) const comments = _.cloneDeep(db.comments)
comments[0].post = db.posts[0] comments[0].post = db.posts[0]
comments[1].post = db.posts[0] comments[1].post = db.posts[0]
@ -83,7 +83,7 @@ describe('Server with custom foreign key', () => {
}) })
describe('GET /:resource/:id?_expand=', () => { describe('GET /:resource/:id?_expand=', () => {
it('should respond with corresponding resource and expanded inner resources', () => { test('should respond with corresponding resource and expanded inner resources', () => {
const comment = _.cloneDeep(db.comments[0]) const comment = _.cloneDeep(db.comments[0])
comment.post = db.posts[0] comment.post = db.posts[0]
return request(server) return request(server)
@ -95,7 +95,7 @@ describe('Server with custom foreign key', () => {
}) })
describe('POST /:parent/:parentId/:resource', () => { describe('POST /:parent/:parentId/:resource', () => {
it('should respond with json and set parentId', () => test('should respond with json and set parentId', () =>
request(server) request(server)
.post('/posts/1/comments') .post('/posts/1/comments')
.send({ body: 'foo' }) .send({ body: 'foo' })
@ -105,7 +105,7 @@ describe('Server with custom foreign key', () => {
}) })
describe('DELETE /:resource/:id', () => { describe('DELETE /:resource/:id', () => {
it('should respond with empty data, destroy resource and dependent resources', async () => { test('should respond with empty data, destroy resource and dependent resources', async () => {
await request(server) await request(server)
.del('/posts/1') .del('/posts/1')
.expect({}) .expect({})

View File

@ -91,7 +91,7 @@ describe('Server', () => {
}) })
describe('GET /db', () => { describe('GET /db', () => {
it('should respond with json and full database', () => test('should respond with json and full database', () =>
request(server) request(server)
.get('/db') .get('/db')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -100,7 +100,7 @@ describe('Server', () => {
}) })
describe('GET /:resource', () => { describe('GET /:resource', () => {
it('should respond with json and corresponding resources', () => test('should respond with json and corresponding resources', () =>
request(server) request(server)
.get('/posts') .get('/posts')
.set('Origin', 'http://example.com') .set('Origin', 'http://example.com')
@ -110,49 +110,49 @@ describe('Server', () => {
.expect(db.posts) .expect(db.posts)
.expect(200)) .expect(200))
it('should respond with 404 if resource is not found', () => test('should respond with 404 if resource is not found', () =>
request(server) request(server)
.get('/undefined') .get('/undefined')
.expect(404)) .expect(404))
}) })
describe('GET /:resource?attr=&attr=', () => { describe('GET /:resource?attr=&attr=', () => {
it('should respond with json and filter resources', () => test('should respond with json and filter resources', () =>
request(server) request(server)
.get('/comments?postId=1&published=true') .get('/comments?postId=1&published=true')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.comments[0]]) .expect([db.comments[0]])
.expect(200)) .expect(200))
it('should be strict', () => test('should be strict', () =>
request(server) request(server)
.get('/users?tel=123') .get('/users?tel=123')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.users[1]]) .expect([db.users[1]])
.expect(200)) .expect(200))
it('should support multiple filters', () => test('should support multiple filters', () =>
request(server) request(server)
.get('/comments?id=1&id=2') .get('/comments?id=1&id=2')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.comments[0], db.comments[1]]) .expect([db.comments[0], db.comments[1]])
.expect(200)) .expect(200))
it('should support deep filter', () => test('should support deep filter', () =>
request(server) request(server)
.get('/deep?a.b=1') .get('/deep?a.b=1')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.deep[0]]) .expect([db.deep[0]])
.expect(200)) .expect(200))
it('should ignore JSONP query parameters callback and _ ', () => test('should ignore JSONP query parameters callback and _ ', () =>
request(server) request(server)
.get('/comments?callback=1&_=1') .get('/comments?callback=1&_=1')
.expect('Content-Type', /text/) .expect('Content-Type', /text/)
.expect(new RegExp(db.comments[0].body)) // JSONP returns text .expect(new RegExp(db.comments[0].body)) // JSONP returns text
.expect(200)) .expect(200))
it('should ignore unknown query parameters', () => test('should ignore unknown query parameters', () =>
request(server) request(server)
.get('/comments?foo=1&bar=2') .get('/comments?foo=1&bar=2')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -160,7 +160,7 @@ describe('Server', () => {
.expect(200)) .expect(200))
// https://github.com/typicode/json-server/issues/510 // https://github.com/typicode/json-server/issues/510
it('should not fail with null value', () => { test('should not fail with null value', () => {
db.posts.push({ id: 99, body: null }) db.posts.push({ id: 99, body: null })
return request(server) return request(server)
.get('/posts?body=foo') .get('/posts?body=foo')
@ -171,42 +171,42 @@ describe('Server', () => {
}) })
describe('GET /:resource?q=', () => { describe('GET /:resource?q=', () => {
it('should respond with json and make a full-text search', () => test('should respond with json and make a full-text search', () =>
request(server) request(server)
.get('/tags?q=pho') .get('/tags?q=pho')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.tags[1], db.tags[2]]) .expect([db.tags[1], db.tags[2]])
.expect(200)) .expect(200))
it('should respond with json and make a deep full-text search', () => test('should respond with json and make a deep full-text search', () =>
request(server) request(server)
.get('/deep?q=1') .get('/deep?q=1')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(db.deep) .expect(db.deep)
.expect(200)) .expect(200))
it('should return an empty array when nothing is matched', () => test('should return an empty array when nothing is matched', () =>
request(server) request(server)
.get('/tags?q=nope') .get('/tags?q=nope')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([]) .expect([])
.expect(200)) .expect(200))
it('should support other query parameters', () => test('should support other query parameters', () =>
request(server) request(server)
.get('/comments?q=qu&published=true') .get('/comments?q=qu&published=true')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.comments[3]]) .expect([db.comments[3]])
.expect(200)) .expect(200))
it('should ignore duplicate q query parameters', () => test('should ignore duplicate q query parameters', () =>
request(server) request(server)
.get('/comments?q=foo&q=bar') .get('/comments?q=foo&q=bar')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.comments[0]]) .expect([db.comments[0]])
.expect(200)) .expect(200))
it('should support filtering by boolean value false', () => test('should support filtering by boolean value false', () =>
request(server) request(server)
.get('/comments?published=false') .get('/comments?published=false')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -215,7 +215,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?_end=', () => { describe('GET /:resource?_end=', () => {
it('should respond with a sliced array', () => test('should respond with a sliced array', () =>
request(server) request(server)
.get('/comments?_end=2') .get('/comments?_end=2')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -226,42 +226,42 @@ describe('Server', () => {
}) })
describe('GET /:resource?_sort=', () => { describe('GET /:resource?_sort=', () => {
it('should respond with json and sort on a field', () => test('should respond with json and sort on a field', () =>
request(server) request(server)
.get('/tags?_sort=body') .get('/tags?_sort=body')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.tags[1], db.tags[0], db.tags[2]]) .expect([db.tags[1], db.tags[0], db.tags[2]])
.expect(200)) .expect(200))
it('should reverse sorting with _order=DESC', () => test('should reverse sorting with _order=DESC', () =>
request(server) request(server)
.get('/tags?_sort=body&_order=DESC') .get('/tags?_sort=body&_order=DESC')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.tags[2], db.tags[0], db.tags[1]]) .expect([db.tags[2], db.tags[0], db.tags[1]])
.expect(200)) .expect(200))
it('should reverse sorting with _order=desc (case insensitive)', () => test('should reverse sorting with _order=desc (case insensitive)', () =>
request(server) request(server)
.get('/tags?_sort=body&_order=desc') .get('/tags?_sort=body&_order=desc')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.tags[2], db.tags[0], db.tags[1]]) .expect([db.tags[2], db.tags[0], db.tags[1]])
.expect(200)) .expect(200))
it('should sort on numerical field', () => test('should sort on numerical field', () =>
request(server) request(server)
.get('/posts?_sort=id&_order=DESC') .get('/posts?_sort=id&_order=DESC')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(db.posts.reverse()) .expect(db.posts.reverse())
.expect(200)) .expect(200))
it('should sort on nested field', () => test('should sort on nested field', () =>
request(server) request(server)
.get('/nested?_sort=resource.name') .get('/nested?_sort=resource.name')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect([db.nested[1], db.nested[0], db.nested[2]]) .expect([db.nested[1], db.nested[0], db.nested[2]])
.expect(200)) .expect(200))
it('should sort on multiple fields', () => test('should sort on multiple fields', () =>
request(server) request(server)
.get('/buyers?_sort=country,total&_order=asc,desc') .get('/buyers?_sort=country,total&_order=asc,desc')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -280,7 +280,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?_start=&_end=', () => { describe('GET /:resource?_start=&_end=', () => {
it('should respond with a sliced array', () => test('should respond with a sliced array', () =>
request(server) request(server)
.get('/comments?_start=1&_end=2') .get('/comments?_start=1&_end=2')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -291,7 +291,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?_start=&_limit=', () => { describe('GET /:resource?_start=&_limit=', () => {
it('should respond with a limited array', () => test('should respond with a limited array', () =>
request(server) request(server)
.get('/comments?_start=1&_limit=1') .get('/comments?_start=1&_limit=1')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -302,7 +302,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?_page=', () => { describe('GET /:resource?_page=', () => {
it('should paginate', () => test('should paginate', () =>
request(server) request(server)
.get('/list?_page=2') .get('/list?_page=2')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -313,7 +313,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?_page=&_limit=', () => { describe('GET /:resource?_page=&_limit=', () => {
it('should paginate with a custom limit', () => { test('should paginate with a custom limit', () => {
const link = [ const link = [
'<http://localhost/list?_page=1&_limit=1>; rel="first"', '<http://localhost/list?_page=1&_limit=1>; rel="first"',
'<http://localhost/list?_page=1&_limit=1>; rel="prev"', '<http://localhost/list?_page=1&_limit=1>; rel="prev"',
@ -333,7 +333,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?attr_gte=&attr_lte=', () => { describe('GET /:resource?attr_gte=&attr_lte=', () => {
it('should respond with a limited array', () => test('should respond with a limited array', () =>
request(server) request(server)
.get('/comments?id_gte=2&id_lte=3') .get('/comments?id_gte=2&id_lte=3')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -342,7 +342,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?attr_ne=', () => { describe('GET /:resource?attr_ne=', () => {
it('should respond with a limited array', () => test('should respond with a limited array', () =>
request(server) request(server)
.get('/comments?id_ne=1') .get('/comments?id_ne=1')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -351,7 +351,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?attr_like=', () => { describe('GET /:resource?attr_like=', () => {
it('should respond with an array that matches the like operator (case insensitive)', () => test('should respond with an array that matches the like operator (case insensitive)', () =>
request(server) request(server)
.get('/tags?body_like=photo') .get('/tags?body_like=photo')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -360,7 +360,7 @@ describe('Server', () => {
}) })
describe('GET /:parent/:parentId/:resource', () => { describe('GET /:parent/:parentId/:resource', () => {
it('should respond with json and corresponding nested resources', () => test('should respond with json and corresponding nested resources', () =>
request(server) request(server)
.get('/posts/1/comments') .get('/posts/1/comments')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -369,28 +369,28 @@ describe('Server', () => {
}) })
describe('GET /:resource/:id', () => { describe('GET /:resource/:id', () => {
it('should respond with json and corresponding resource', () => test('should respond with json and corresponding resource', () =>
request(server) request(server)
.get('/posts/1') .get('/posts/1')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(db.posts[0]) .expect(db.posts[0])
.expect(200)) .expect(200))
it('should support string id, respond with json and corresponding resource', () => test('should support string id, respond with json and corresponding resource', () =>
request(server) request(server)
.get('/refs/abcd-1234') .get('/refs/abcd-1234')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(db.refs[0]) .expect(db.refs[0])
.expect(200)) .expect(200))
it('should support integer id as string', () => test('should support integer id as string', () =>
request(server) request(server)
.get('/stringIds/1234') .get('/stringIds/1234')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(db.stringIds[0]) .expect(db.stringIds[0])
.expect(200)) .expect(200))
it('should respond with 404 if resource is not found', () => test('should respond with 404 if resource is not found', () =>
request(server) request(server)
.get('/posts/9001') .get('/posts/9001')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -399,7 +399,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?_embed=', () => { describe('GET /:resource?_embed=', () => {
it('should respond with corresponding resources and embedded resources', () => { test('should respond with corresponding resources and embedded resources', () => {
const posts = _.cloneDeep(db.posts) const posts = _.cloneDeep(db.posts)
posts[0].comments = [db.comments[0], db.comments[1]] posts[0].comments = [db.comments[0], db.comments[1]]
posts[1].comments = [db.comments[2], db.comments[3], db.comments[4]] posts[1].comments = [db.comments[2], db.comments[3], db.comments[4]]
@ -412,7 +412,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?_embed&_embed=', () => { describe('GET /:resource?_embed&_embed=', () => {
it('should respond with corresponding resources and embedded resources', () => { test('should respond with corresponding resources and embedded resources', () => {
const posts = _.cloneDeep(db.posts) const posts = _.cloneDeep(db.posts)
posts[0].comments = [db.comments[0], db.comments[1]] posts[0].comments = [db.comments[0], db.comments[1]]
posts[0].refs = [db.refs[0]] posts[0].refs = [db.refs[0]]
@ -427,7 +427,7 @@ describe('Server', () => {
}) })
describe('GET /:resource/:id?_embed=', () => { describe('GET /:resource/:id?_embed=', () => {
it('should respond with corresponding resources and embedded resources', () => { test('should respond with corresponding resources and embedded resources', () => {
const post = _.cloneDeep(db.posts[0]) const post = _.cloneDeep(db.posts[0])
post.comments = [db.comments[0], db.comments[1]] post.comments = [db.comments[0], db.comments[1]]
return request(server) return request(server)
@ -439,7 +439,7 @@ describe('Server', () => {
}) })
describe('GET /:resource/:id?_embed=&_embed=', () => { describe('GET /:resource/:id?_embed=&_embed=', () => {
it('should respond with corresponding resource and embedded resources', () => { test('should respond with corresponding resource and embedded resources', () => {
const post = _.cloneDeep(db.posts[0]) const post = _.cloneDeep(db.posts[0])
post.comments = [db.comments[0], db.comments[1]] post.comments = [db.comments[0], db.comments[1]]
post.refs = [db.refs[0]] post.refs = [db.refs[0]]
@ -452,7 +452,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?_expand=', () => { describe('GET /:resource?_expand=', () => {
it('should respond with corresponding resource and expanded inner resources', () => { test('should respond with corresponding resource and expanded inner resources', () => {
const refs = _.cloneDeep(db.refs) const refs = _.cloneDeep(db.refs)
refs[0].post = db.posts[0] refs[0].post = db.posts[0]
return request(server) return request(server)
@ -464,7 +464,7 @@ describe('Server', () => {
}) })
describe('GET /:resource/:id?_expand=', () => { describe('GET /:resource/:id?_expand=', () => {
it('should respond with corresponding resource and expanded inner resources', () => { test('should respond with corresponding resource and expanded inner resources', () => {
const comment = _.cloneDeep(db.comments[0]) const comment = _.cloneDeep(db.comments[0])
comment.post = db.posts[0] comment.post = db.posts[0]
return request(server) return request(server)
@ -476,7 +476,7 @@ describe('Server', () => {
}) })
describe('GET /:resource?_expand=&_expand', () => { describe('GET /:resource?_expand=&_expand', () => {
it('should respond with corresponding resource and expanded inner resources', () => { test('should respond with corresponding resource and expanded inner resources', () => {
const refs = _.cloneDeep(db.refs) const refs = _.cloneDeep(db.refs)
refs[0].post = db.posts[0] refs[0].post = db.posts[0]
refs[0].user = db.users[0] refs[0].user = db.users[0]
@ -489,7 +489,7 @@ describe('Server', () => {
}) })
describe('GET /:resource/:id?_expand=&_expand=', () => { describe('GET /:resource/:id?_expand=&_expand=', () => {
it('should respond with corresponding resource and expanded inner resources', () => { test('should respond with corresponding resource and expanded inner resources', () => {
const comments = db.comments[0] const comments = db.comments[0]
comments.post = db.posts[0] comments.post = db.posts[0]
comments.user = db.users[0] comments.user = db.users[0]
@ -502,7 +502,7 @@ describe('Server', () => {
}) })
describe('GET /:resource>_delay=', () => { describe('GET /:resource>_delay=', () => {
it('should delay response', done => { test('should delay response', done => {
const start = new Date() const start = new Date()
request(server) request(server)
.get('/posts?_delay=1100') .get('/posts?_delay=1100')
@ -514,7 +514,7 @@ describe('Server', () => {
}) })
describe('POST /:resource', () => { describe('POST /:resource', () => {
it('should respond with json, create a resource and increment id', async () => { test('should respond with json, create a resource and increment id', async () => {
await request(server) await request(server)
.post('/posts') .post('/posts')
.send({ body: 'foo', booleanValue: true, integerValue: 1 }) .send({ body: 'foo', booleanValue: true, integerValue: 1 })
@ -526,7 +526,7 @@ describe('Server', () => {
assert.equal(db.posts.length, 3) assert.equal(db.posts.length, 3)
}) })
it('should support x-www-form-urlencoded', async () => { test('should support x-www-form-urlencoded', async () => {
await request(server) await request(server)
.post('/posts') .post('/posts')
.type('form') .type('form')
@ -538,7 +538,7 @@ describe('Server', () => {
assert.equal(db.posts.length, 3) assert.equal(db.posts.length, 3)
}) })
it('should respond with json, create a resource and generate string id', async () => { test('should respond with json, create a resource and generate string id', async () => {
await request(server) await request(server)
.post('/refs') .post('/refs')
.send({ url: 'http://foo.com', postId: '1' }) .send({ url: 'http://foo.com', postId: '1' })
@ -549,7 +549,7 @@ describe('Server', () => {
}) })
describe('POST /:parent/:parentId/:resource', () => { describe('POST /:parent/:parentId/:resource', () => {
it('should respond with json and set parentId', () => test('should respond with json and set parentId', () =>
request(server) request(server)
.post('/posts/1/comments') .post('/posts/1/comments')
.send({ body: 'foo' }) .send({ body: 'foo' })
@ -559,7 +559,7 @@ describe('Server', () => {
}) })
describe('POST /:resource?_delay=', () => { describe('POST /:resource?_delay=', () => {
it('should delay response', done => { test('should delay response', done => {
const start = new Date() const start = new Date()
request(server) request(server)
.post('/posts?_delay=1100') .post('/posts?_delay=1100')
@ -572,7 +572,7 @@ describe('Server', () => {
}) })
describe('PUT /:resource/:id', () => { describe('PUT /:resource/:id', () => {
it('should respond with json and replace resource', async () => { test('should respond with json and replace resource', async () => {
const post = { id: 1, booleanValue: true, integerValue: 1 } const post = { id: 1, booleanValue: true, integerValue: 1 }
const res = await request(server) const res = await request(server)
.put('/posts/1') .put('/posts/1')
@ -589,7 +589,7 @@ describe('Server', () => {
assert.deepStrictEqual(db.posts[0], post) assert.deepStrictEqual(db.posts[0], post)
}) })
it('should respond with 404 if resource is not found', () => test('should respond with 404 if resource is not found', () =>
request(server) request(server)
.put('/posts/9001') .put('/posts/9001')
.send({ id: 1, body: 'bar' }) .send({ id: 1, body: 'bar' })
@ -599,7 +599,7 @@ describe('Server', () => {
}) })
describe('PUT /:resource:id?_delay=', () => { describe('PUT /:resource:id?_delay=', () => {
it('should delay response', done => { test('should delay response', done => {
const start = new Date() const start = new Date()
request(server) request(server)
.put('/posts/1?_delay=1100') .put('/posts/1?_delay=1100')
@ -613,7 +613,7 @@ describe('Server', () => {
}) })
describe('PATCH /:resource/:id', () => { describe('PATCH /:resource/:id', () => {
it('should respond with json and update resource', async () => { test('should respond with json and update resource', async () => {
const partial = { body: 'bar' } const partial = { body: 'bar' }
const post = { id: 1, body: 'bar' } const post = { id: 1, body: 'bar' }
const res = await request(server) const res = await request(server)
@ -627,7 +627,7 @@ describe('Server', () => {
assert.deepStrictEqual(db.posts[0], post) assert.deepStrictEqual(db.posts[0], post)
}) })
it('should respond with 404 if resource is not found', () => test('should respond with 404 if resource is not found', () =>
request(server) request(server)
.patch('/posts/9001') .patch('/posts/9001')
.send({ body: 'bar' }) .send({ body: 'bar' })
@ -637,7 +637,7 @@ describe('Server', () => {
}) })
describe('PATCH /:resource:id?_delay=', () => { describe('PATCH /:resource:id?_delay=', () => {
it('should delay response', done => { test('should delay response', done => {
const start = new Date() const start = new Date()
request(server) request(server)
.patch('/posts/1?_delay=1100') .patch('/posts/1?_delay=1100')
@ -651,7 +651,7 @@ describe('Server', () => {
}) })
describe('DELETE /:resource/:id', () => { describe('DELETE /:resource/:id', () => {
it('should respond with empty data, destroy resource and dependent resources', async () => { test('should respond with empty data, destroy resource and dependent resources', async () => {
await request(server) await request(server)
.del('/posts/1') .del('/posts/1')
.expect({}) .expect({})
@ -660,7 +660,7 @@ describe('Server', () => {
assert.equal(db.comments.length, 3) assert.equal(db.comments.length, 3)
}) })
it('should respond with 404 if resource is not found', () => test('should respond with 404 if resource is not found', () =>
request(server) request(server)
.del('/posts/9001') .del('/posts/9001')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -669,7 +669,7 @@ describe('Server', () => {
}) })
describe('DELETE /:resource:id?_delay=', () => { describe('DELETE /:resource:id?_delay=', () => {
it('should delay response', done => { test('should delay response', done => {
const start = new Date() const start = new Date()
request(server) request(server)
.del('/posts/1?_delay=1100') .del('/posts/1?_delay=1100')
@ -683,7 +683,7 @@ describe('Server', () => {
describe('Static routes', () => { describe('Static routes', () => {
describe('GET /', () => { describe('GET /', () => {
it('should respond with html', () => test('should respond with html', () =>
request(server) request(server)
.get('/') .get('/')
.expect(/You're successfully running JSON Server/) .expect(/You're successfully running JSON Server/)
@ -691,7 +691,7 @@ describe('Server', () => {
}) })
describe('GET /main.js', () => { describe('GET /main.js', () => {
it('should respond with js', () => test('should respond with js', () =>
request(server) request(server)
.get('/main.js') .get('/main.js')
.expect('Content-Type', /javascript/) .expect('Content-Type', /javascript/)
@ -699,7 +699,7 @@ describe('Server', () => {
}) })
describe('GET /main.css', () => { describe('GET /main.css', () => {
it('should respond with css', () => test('should respond with css', () =>
request(server) request(server)
.get('/main.css') .get('/main.css')
.expect('Content-Type', /css/) .expect('Content-Type', /css/)
@ -708,13 +708,13 @@ describe('Server', () => {
}) })
describe('Database state', () => { describe('Database state', () => {
it('should be accessible', () => { test('should be accessible', () => {
assert(router.db.getState()) assert(router.db.getState())
}) })
}) })
describe('Responses', () => { describe('Responses', () => {
it('should have no cache headers (for IE)', () => test('should have no cache headers (for IE)', () =>
request(server) request(server)
.get('/db') .get('/db')
.expect('Cache-Control', 'no-cache') .expect('Cache-Control', 'no-cache')
@ -723,17 +723,17 @@ describe('Server', () => {
}) })
describe('Rewriter', () => { describe('Rewriter', () => {
it('should rewrite using prefix', () => test('should rewrite using prefix', () =>
request(server) request(server)
.get('/api/posts/1') .get('/api/posts/1')
.expect(db.posts[0])) .expect(db.posts[0]))
it('should rewrite using params', () => test('should rewrite using params', () =>
request(server) request(server)
.get('/blog/posts/1/show') .get('/blog/posts/1/show')
.expect(db.posts[0])) .expect(db.posts[0]))
it('should rewrite using query without params', () => { test('should rewrite using query without params', () => {
const expectedPost = _.cloneDeep(db.posts[0]) const expectedPost = _.cloneDeep(db.posts[0])
expectedPost.comments = [db.comments[0], db.comments[1]] expectedPost.comments = [db.comments[0], db.comments[1]]
return request(server) return request(server)
@ -741,17 +741,17 @@ describe('Server', () => {
.expect(expectedPost) .expect(expectedPost)
}) })
it('should rewrite using params and query', () => test('should rewrite using params and query', () =>
request(server) request(server)
.get('/comments/special/1-quux') .get('/comments/special/1-quux')
.expect([db.comments[4]])) .expect([db.comments[4]]))
it('should rewrite query params', () => test('should rewrite query params', () =>
request(server) request(server)
.get('/articles?_id=1') .get('/articles?_id=1')
.expect(db.posts[0])) .expect(db.posts[0]))
it('should expose routes', () => test('should expose routes', () =>
request(server) request(server)
.get('/__rules') .get('/__rules')
.expect(rewriterRules)) .expect(rewriterRules))
@ -764,7 +764,7 @@ describe('Server', () => {
} }
}) })
it('should be possible to wrap response', () => test('should be possible to wrap response', () =>
request(server) request(server)
.get('/posts/1') .get('/posts/1')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -781,14 +781,14 @@ describe('Server', () => {
router.db._.id = '_id' router.db._.id = '_id'
}) })
it('should be possible to GET using a different id property', () => test('should be possible to GET using a different id property', () =>
request(server) request(server)
.get('/posts/1') .get('/posts/1')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(router.db.getState().posts[0]) .expect(router.db.getState().posts[0])
.expect(200)) .expect(200))
it('should be possible to POST using a different id property', () => test('should be possible to POST using a different id property', () =>
request(server) request(server)
.post('/posts') .post('/posts')
.send({ body: 'hello' }) .send({ body: 'hello' })

View File

@ -21,7 +21,7 @@ describe('Server', () => {
}) })
describe('GET /:resource', () => { describe('GET /:resource', () => {
it('should respond with corresponding resource', () => test('should respond with corresponding resource', () =>
request(server) request(server)
.get('/user') .get('/user')
.expect(db.user) .expect(db.user)
@ -29,7 +29,7 @@ describe('Server', () => {
}) })
describe('POST /:resource', () => { describe('POST /:resource', () => {
it('should create resource', () => { test('should create resource', () => {
const user = { name: 'bar' } const user = { name: 'bar' }
return request(server) return request(server)
.post('/user') .post('/user')
@ -40,7 +40,7 @@ describe('Server', () => {
}) })
describe('PUT /:resource', () => { describe('PUT /:resource', () => {
it('should update resource', () => { test('should update resource', () => {
const user = { name: 'bar' } const user = { name: 'bar' }
return request(server) return request(server)
.put('/user') .put('/user')
@ -51,7 +51,7 @@ describe('Server', () => {
}) })
describe('PATCH /:resource', () => { describe('PATCH /:resource', () => {
it('should update resource', () => test('should update resource', () =>
request(server) request(server)
.patch('/user') .patch('/user')
.send({ name: 'bar' }) .send({ name: 'bar' })

View File

@ -6,7 +6,7 @@ describe('utils', () => {
const array = [1, 2, 3, 4, 5] const array = [1, 2, 3, 4, 5]
const perPage = 2 const perPage = 2
it('should return first page', () => { test('should return first page', () => {
assert.deepEqual(utils.getPage(array, 1, perPage), { assert.deepEqual(utils.getPage(array, 1, perPage), {
items: [1, 2], items: [1, 2],
current: 1, current: 1,
@ -16,7 +16,7 @@ describe('utils', () => {
}) })
}) })
it('should return second page', () => { test('should return second page', () => {
assert.deepEqual(utils.getPage(array, 2, perPage), { assert.deepEqual(utils.getPage(array, 2, perPage), {
items: [3, 4], items: [3, 4],
current: 2, current: 2,
@ -27,7 +27,7 @@ describe('utils', () => {
}) })
}) })
it('should return third page (last)', () => { test('should return third page (last)', () => {
assert.deepEqual(utils.getPage(array, 3, perPage), { assert.deepEqual(utils.getPage(array, 3, perPage), {
items: [5], items: [5],
current: 3, current: 3,
@ -37,19 +37,19 @@ describe('utils', () => {
}) })
}) })
it('should return an empty array if page is greater than the last page', () => { test('should return an empty array if page is greater than the last page', () => {
assert.deepEqual(utils.getPage(array, 99, perPage), { assert.deepEqual(utils.getPage(array, 99, perPage), {
items: [] items: []
}) })
}) })
it('should return the array if perPage is greater than the array size', () => { test('should return the array if perPage is greater than the array size', () => {
assert.deepEqual(utils.getPage(array, 1, 99), { assert.deepEqual(utils.getPage(array, 1, 99), {
items: array items: array
}) })
}) })
it('should return an empty array if the array is empty', () => { test('should return an empty array if the array is empty', () => {
assert.deepEqual(utils.getPage([], 1, 1), { assert.deepEqual(utils.getPage([], 1, 1), {
items: [] items: []
}) })

View File

@ -2,15 +2,15 @@ const assert = require('assert')
const validateData = require('../../src/server/router/validate-data') const validateData = require('../../src/server/router/validate-data')
describe('validateData', () => { describe('validateData', () => {
it('should throw an error if data contains /', () => { test('should throw an error if data contains /', () => {
assert.throws(() => validateData({ 'a/b': [] }), /found \//) assert.throws(() => validateData({ 'a/b': [] }), /found \//)
}) })
it('should throw an error if data is an array', () => { test('should throw an error if data is an array', () => {
assert.throws(() => validateData([]), /must be an object/) assert.throws(() => validateData([]), /must be an object/)
}) })
it("shouldn't throw an error", () => { test("shouldn't throw an error", () => {
assert.doesNotThrow(() => validateData({ a: [] })) assert.doesNotThrow(() => validateData({ a: [] }))
}) })
}) })

7940
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -8,9 +8,7 @@
"test": "test" "test": "test"
}, },
"scripts": { "scripts": {
"test": "npm run test:cli && npm run test:server && npm run lint", "test": "cross-env NODE_ENV=test jest && npm run lint",
"test:cli": "npm run build && cross-env NODE_ENV=test mocha test/cli/*.js",
"test:server": "cross-env NODE_ENV=test mocha test/server/*.js",
"start": "run-p start:**", "start": "run-p start:**",
"start:babel-node": "babel-node src/cli/bin db.json -r routes.json", "start:babel-node": "babel-node src/cli/bin db.json -r routes.json",
"start:webpack": "webpack -d --watch", "start:webpack": "webpack -d --watch",
@ -69,11 +67,11 @@
"eslint-plugin-standard": "^3.1.0", "eslint-plugin-standard": "^3.1.0",
"html-webpack-plugin": "^3.2.0", "html-webpack-plugin": "^3.2.0",
"husky": "^0.14.3", "husky": "^0.14.3",
"jest": "^23.1.0",
"markdown-toc": "^1.2.0", "markdown-toc": "^1.2.0",
"milligram": "^1.3.0", "milligram": "^1.3.0",
"mini-css-extract-plugin": "^0.4.0", "mini-css-extract-plugin": "^0.4.0",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"mocha": "^4.1.0",
"npm-run-all": "^4.1.3", "npm-run-all": "^4.1.3",
"os-tmpdir": "^1.0.1", "os-tmpdir": "^1.0.1",
"pkg-ok": "^2.2.0", "pkg-ok": "^2.2.0",

View File

@ -1,3 +0,0 @@
--require babel-register
--reporter spec
--timeout 5000