-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): refactoring tests to dismiss the supertest wrapper
removing supertest/superagent from Ava unit tests and instead replacing it with request wrapped up in native promises. This also eliminates the need for global supertest config and wrapping of expressjs app and database.
- Loading branch information
Showing
3 changed files
with
104 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 91 additions & 55 deletions
146
server/modules/tasks/test/tasks.server.routes.sanity.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,113 @@ | ||
'use strict'; | ||
|
||
import test from 'ava'; | ||
import app from '../../../config/lib/app'; | ||
import supertest from 'supertest'; | ||
import request from 'request'; | ||
|
||
/** | ||
* Pre-condition for this test is to run on a clean database setup with no records existing | ||
*/ | ||
|
||
// Bootstrap the application components (db, orm and express app) | ||
// Make the components available to tests through the shared object `t.context` | ||
var appComponents; | ||
function bootstrapTest() { | ||
return app.bootstrap(); | ||
} | ||
|
||
// Before all tests would run we will bootstrap the ExpressJS app server | ||
// and it's related components (Mongoose DB and Sequelize ORM) | ||
test.before('Bootstrapping App for Tasks Routes test', async t => { | ||
// Before each test we setup a request object with defaults | ||
// Making the request object available to tests through the shared object `t.context` | ||
test.beforeEach('Setting up test defaults', t => { | ||
const requestObject = request.defaults({ | ||
|
||
appComponents = await bootstrapTest(); | ||
// Set the Base URL for all API requests | ||
baseUrl: 'http://localhost:3001', | ||
// Set data send/received to be JSON compatible | ||
json: true, | ||
// Set the cookie jar option to true which persists cookies | ||
// between API requests made, which enables us to perform | ||
// login and further API calls as a logged-in user. | ||
jar: true | ||
}); | ||
|
||
t.truthy(appComponents.db, 'bootstrapped with db property'); | ||
t.truthy(appComponents.orm, 'bootstrapped with orm property'); | ||
t.truthy(appComponents.app, 'bootstrapped with app property'); | ||
t.context.request = requestObject; | ||
}); | ||
|
||
test('API: Get All Tasks for anonymous user', async t => { | ||
let response = await supertest(appComponents.app) | ||
.get('/api/tasks') | ||
.expect(200); | ||
|
||
t.is(200, response.statusCode, 'successful'); | ||
t.true(response.body.length === 0, 'returns empty results'); | ||
|
||
test('API: Get All Tasks as anonymous user', async t => { | ||
let response = await new Promise(function (resolve, reject) { | ||
t.context.request.get({ | ||
uri: '/api/tasks' | ||
}, function (error, response, body) { | ||
if (error) { | ||
reject(error); | ||
} | ||
resolve(response); | ||
}); | ||
}); | ||
|
||
t.is(response.statusCode, 200, response.body.toString()); | ||
t.true(response.body.length === 0, response.body.toString()); | ||
}); | ||
|
||
test('API: Get My Tasks for anonymous user', async t => { | ||
let response = await supertest(appComponents.app) | ||
.get('/api/tasks/me') | ||
.expect(401); | ||
|
||
t.is(401, response.statusCode, 'successful'); | ||
t.true(response.body.message == 'No session user', 'No session user'); | ||
|
||
test('API: Get My Tasks as anonymous user', async t => { | ||
let response = await new Promise(function (resolve, reject) { | ||
t.context.request.get({ | ||
uri: '/api/tasks/me' | ||
}, function (error, response, body) { | ||
if (error) { | ||
reject(error); | ||
} | ||
resolve(response); | ||
}); | ||
}); | ||
|
||
t.is(response.statusCode, 401, response.body.toString()); | ||
t.true(response.body.message === 'No session user', response.body.toString()); | ||
}); | ||
|
||
test('API: Create Task with anonymous user', async t => { | ||
let response = await supertest(appComponents.app) | ||
.post('/api/tasks') | ||
.expect(401); | ||
|
||
t.is(401, response.statusCode, 'successful'); | ||
t.true(response.body.message == 'No session user', 'No session user'); | ||
|
||
test('API: Create Task as anonymous user', async t => { | ||
let response = await new Promise(function (resolve, reject) { | ||
t.context.request.post({ | ||
uri: '/api/tasks', | ||
body: { | ||
title: 'my test task' | ||
} | ||
}, function (error, response, body) { | ||
if (error) { | ||
reject(error); | ||
} | ||
resolve(response); | ||
}); | ||
}); | ||
|
||
t.is(response.statusCode, 401, response.body.toString()); | ||
t.true(response.body.message === 'No session user', response.body.toString()); | ||
}); | ||
|
||
test('API: Update Task with anonymous user', async t => { | ||
let response = await supertest(appComponents.app) | ||
.put('/api/tasks') | ||
.expect(401); | ||
|
||
t.is(401, response.statusCode, 'successful'); | ||
t.true(response.body.message == 'No session user', 'No session user'); | ||
|
||
test('API: Update Task as anonymous user', async t => { | ||
let response = await new Promise(function (resolve, reject) { | ||
t.context.request.put({ | ||
uri: '/api/tasks', | ||
body: { | ||
title: 'my test task' | ||
} | ||
}, function (error, response, body) { | ||
if (error) { | ||
reject(error); | ||
} | ||
resolve(response); | ||
}); | ||
}); | ||
|
||
t.is(response.statusCode, 401, response.body.toString()); | ||
t.true(response.body.message === 'No session user', response.body.toString()); | ||
}); | ||
|
||
test('API: Delete Task with anonymous user', async t => { | ||
let response = await supertest(appComponents.app) | ||
.delete('/api/tasks') | ||
.expect(401); | ||
|
||
t.is(401, response.statusCode, 'successful'); | ||
t.true(response.body.message == 'No session user', 'No session user'); | ||
|
||
test('API: Delete Task as anonymous user', async t => { | ||
let response = await new Promise(function (resolve, reject) { | ||
t.context.request.delete({ | ||
uri: '/api/tasks' | ||
}, function (error, response, body) { | ||
if (error) { | ||
reject(error); | ||
} | ||
resolve(response); | ||
}); | ||
}); | ||
|
||
t.is(response.statusCode, 401, response.body.toString()); | ||
t.true(response.body.message === 'No session user', response.body.toString()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters