Skip to content
Permalink
Browse files

Jest (#792)

  • Loading branch information...
typicode committed Jun 4, 2018
1 parent 0241ad5 commit 4c292dfa06cb82e6d25f7f452d36062343396d00
@@ -10,5 +10,5 @@ module.exports = {
},
]
},
env: { mocha: true }
env: { jest: true }
}
@@ -1,2 +1,3 @@
src
test
__tests__
__fixtures__
File renamed without changes.
File renamed without changes.
@@ -12,9 +12,9 @@ const serverReady = require('server-ready')
let PORT = 3100

const middlewareFiles = {
en: './fixtures/middlewares/en.js',
jp: './fixtures/middlewares/jp.js',
postbody: './fixtures/middlewares/postbody.js'
en: './../../__fixtures__/middlewares/en.js',
jp: './../../__fixtures__/middlewares/jp.js',
postbody: './../../__fixtures__/middlewares/postbody.js'
}

const bin = path.join(__dirname, '../../lib/cli/bin')
@@ -60,11 +60,11 @@ describe('cli', () => {
serverReady(PORT, done)
})

it('should support JSON file', done => {
test('should support JSON file', done => {
request.get('/posts').expect(200, done)
})

it('should send CORS headers', done => {
test('should send CORS headers', done => {
const origin = 'http://example.com'

request
@@ -74,7 +74,7 @@ describe('cli', () => {
.expect(200, done)
})

it('should update JSON file', done => {
test('should update JSON file', done => {
request
.post('/posts')
.send({ title: 'hello' })
@@ -90,11 +90,11 @@ describe('cli', () => {

describe('seed.js', () => {
beforeEach(done => {
child = cli(['fixtures/seed.js'])
child = cli(['../../__fixtures__/seed.js'])
serverReady(PORT, done)
})

it('should support JS file', done => {
test('should support JS file', done => {
request.get('/posts').expect(200, done)
})
})
@@ -105,7 +105,7 @@ describe('cli', () => {
serverReady(PORT, done)
})

it('should support URL file', done => {
test('should support URL file', done => {
request.get('/posts').expect(200, done)
})
})
@@ -127,20 +127,20 @@ describe('cli', () => {
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)
})

it('should use _id as foreignKeySuffix', async () => {
test('should use _id as foreignKeySuffix', async () => {
const response = await request.get('/posts/1/comments')
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)
})

it('should allow only GET requests', done => {
test('should allow only GET requests', done => {
request.post('/blog/posts').expect(403, done)
})
})
@@ -151,7 +151,7 @@ describe('cli', () => {
serverReady(PORT, done)
})

it('should apply all middlewares', done => {
test('should apply all middlewares', done => {
request
.get('/posts')
.expect('X-Hello', 'World')
@@ -165,7 +165,7 @@ describe('cli', () => {
serverReady(PORT, done)
})

it('should have post body in middleware', done => {
test('should have post body in middleware', done => {
request
.post('/posts')
.send({ name: 'test' })
@@ -179,7 +179,7 @@ describe('cli', () => {
serverReady(PORT, done)
})

it('should delay response', done => {
test('should delay response', done => {
const start = new Date()
request.get('/posts').expect(200, function(err) {
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 publicDir = 'fixtures/public'
const publicDir = '../../__fixtures__/public'

beforeEach(done => {
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)
})

it('should save a snapshot in snapshots dir', () => {
test('should save a snapshot in snapshots dir', () => {
assert.equal(fs.readdirSync(snapshotsDir).length, 1)
})
})

describe('fixtures/seed.json --no-cors=true', () => {
describe('../../__fixtures__/seed.json --no-cors=true', () => {
beforeEach(done => {
child = cli(['fixtures/seed.js', '--no-cors=true'])
child = cli(['../../__fixtures__/seed.js', '--no-cors=true'])
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'

request
@@ -238,13 +238,13 @@ describe('cli', () => {
})
})

describe('fixtures/seed.json --no-gzip=true', () => {
describe('../../__fixtures__/seed.json --no-gzip=true', () => {
beforeEach(done => {
child = cli(['fixtures/seed.js', '--no-gzip=true'])
child = cli(['../../__fixtures__/seed.js', '--no-gzip=true'])
serverReady(PORT, done)
})

it('should not set Content-Encoding to gzip', done => {
test('should not set Content-Encoding to gzip', done => {
request
.get('/posts')
.expect(200)
@@ -266,14 +266,14 @@ describe('cli', () => {
serverReady(PORT, done)
})

it('should watch db file', done => {
test('should watch db file', done => {
fs.writeFileSync(dbFile, JSON.stringify({ foo: [] }))
setTimeout(() => {
request.get('/foo').expect(200, done)
}, 1000)
})

it('should watch routes file', done => {
test('should watch routes file', done => {
fs.writeFileSync(routesFile, JSON.stringify({ '/api/*': '/$1' }))
setTimeout(() => {
request.get('/api/posts').expect(200, done)
@@ -288,7 +288,7 @@ describe('cli', () => {
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)
})
})
@@ -298,7 +298,7 @@ describe('cli', () => {
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.on('exit', code => {
if (code === 1) {
@@ -6,7 +6,7 @@ const mixins = require('../../src/server/mixins')
describe('mixins', () => {
let db

before(() => {
beforeAll(() => {
_.mixin(lodashId)
_.mixin(mixins)
})
@@ -25,7 +25,7 @@ describe('mixins', () => {
})

describe('getRemovable', () => {
it('should return removable documents', () => {
test('should return removable documents', () => {
const expected = [
{ name: 'comments', id: 2 },
{ name: 'comments', id: 3 }
@@ -34,7 +34,7 @@ describe('mixins', () => {
assert.deepEqual(_.getRemovable(db, { foreignKeySuffix: 'Id' }), expected)
})

it('should support custom foreignKeySuffix', () => {
test('should support custom foreignKeySuffix', () => {
const expected = [
{ name: 'comments', id: 2 },
{ name: 'comments', id: 3 }
@@ -45,11 +45,11 @@ describe('mixins', () => {
})

describe('createId', () => {
it('should return a new id', () => {
test('should return a new id', () => {
assert.equal(_.createId(db.comments), 4)
})

it('should return a new uuid', () => {
test('should return a new uuid', () => {
assert.notEqual(_.createId(db.photos), 3)
})
})
@@ -26,7 +26,7 @@ describe('Server with custom foreign key', () => {
})

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)
.get('/posts/1/comments')
.expect('Content-Type', /json/)
@@ -35,7 +35,7 @@ describe('Server with custom foreign key', () => {
})

describe('GET /:resource/:id', () => {
it('should respond with json and corresponding resource', () =>
test('should respond with json and corresponding resource', () =>
request(server)
.get('/posts/1')
.expect('Content-Type', /json/)
@@ -44,7 +44,7 @@ describe('Server with custom foreign key', () => {
})

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)
posts[0].comments = [db.comments[0], db.comments[1]]
posts[1].comments = [db.comments[2]]
@@ -57,7 +57,7 @@ describe('Server with custom foreign key', () => {
})

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])
post.comments = [db.comments[0], db.comments[1]]
return request(server)
@@ -69,7 +69,7 @@ describe('Server with custom foreign key', () => {
})

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)
comments[0].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=', () => {
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])
comment.post = db.posts[0]
return request(server)
@@ -95,7 +95,7 @@ describe('Server with custom foreign key', () => {
})

describe('POST /:parent/:parentId/:resource', () => {
it('should respond with json and set parentId', () =>
test('should respond with json and set parentId', () =>
request(server)
.post('/posts/1/comments')
.send({ body: 'foo' })
@@ -105,7 +105,7 @@ describe('Server with custom foreign key', () => {
})

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)
.del('/posts/1')
.expect({})
Oops, something went wrong.

0 comments on commit 4c292df

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.