Skip to content

Commit dc17715

Browse files
committed
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.
1 parent 7430140 commit dc17715

3 files changed

Lines changed: 104 additions & 55 deletions

File tree

server/config/env/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ module.exports = {
1212
// Enable mongoose debug mode
1313
debug: process.env.MONGODB_DEBUG || false
1414
},
15+
orm: {
16+
dbname: 'meandev',
17+
user: 'root',
18+
pass: 'root',
19+
options: {
20+
// sequelize supports one of: mysql, postgres, sqlite, mariadb and mssql.
21+
dialect: 'mysql',
22+
host: '',
23+
port: ''
24+
}
25+
},
1526
log: {
1627
// logging with Morgan - https://github.com/expressjs/morgan
1728
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
@@ -82,6 +93,7 @@ module.exports = {
8293
},
8394
seedDB: {
8495
seed: process.env.MONGO_SEED === 'true',
96+
reset: process.env.MONGO_SEED_RESET === 'true',
8597
options: {
8698
logResults: process.env.MONGO_SEED_LOG_RESULTS !== 'false',
8799
seedUser: {
Lines changed: 91 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,113 @@
11
'use strict';
22

33
import test from 'ava';
4-
import app from '../../../config/lib/app';
5-
import supertest from 'supertest';
4+
import request from 'request';
65

76
/**
87
* Pre-condition for this test is to run on a clean database setup with no records existing
98
*/
109

11-
// Bootstrap the application components (db, orm and express app)
12-
// Make the components available to tests through the shared object `t.context`
13-
var appComponents;
14-
function bootstrapTest() {
15-
return app.bootstrap();
16-
}
1710

18-
// Before all tests would run we will bootstrap the ExpressJS app server
19-
// and it's related components (Mongoose DB and Sequelize ORM)
20-
test.before('Bootstrapping App for Tasks Routes test', async t => {
11+
// Before each test we setup a request object with defaults
12+
// Making the request object available to tests through the shared object `t.context`
13+
test.beforeEach('Setting up test defaults', t => {
14+
const requestObject = request.defaults({
2115

22-
appComponents = await bootstrapTest();
16+
// Set the Base URL for all API requests
17+
baseUrl: 'http://localhost:3001',
18+
// Set data send/received to be JSON compatible
19+
json: true,
20+
// Set the cookie jar option to true which persists cookies
21+
// between API requests made, which enables us to perform
22+
// login and further API calls as a logged-in user.
23+
jar: true
24+
});
2325

24-
t.truthy(appComponents.db, 'bootstrapped with db property');
25-
t.truthy(appComponents.orm, 'bootstrapped with orm property');
26-
t.truthy(appComponents.app, 'bootstrapped with app property');
26+
t.context.request = requestObject;
2727
});
2828

29-
test('API: Get All Tasks for anonymous user', async t => {
30-
let response = await supertest(appComponents.app)
31-
.get('/api/tasks')
32-
.expect(200);
33-
34-
t.is(200, response.statusCode, 'successful');
35-
t.true(response.body.length === 0, 'returns empty results');
36-
29+
test('API: Get All Tasks as anonymous user', async t => {
30+
let response = await new Promise(function (resolve, reject) {
31+
t.context.request.get({
32+
uri: '/api/tasks'
33+
}, function (error, response, body) {
34+
if (error) {
35+
reject(error);
36+
}
37+
resolve(response);
38+
});
39+
});
40+
41+
t.is(response.statusCode, 200, response.body.toString());
42+
t.true(response.body.length === 0, response.body.toString());
3743
});
3844

39-
test('API: Get My Tasks for anonymous user', async t => {
40-
let response = await supertest(appComponents.app)
41-
.get('/api/tasks/me')
42-
.expect(401);
43-
44-
t.is(401, response.statusCode, 'successful');
45-
t.true(response.body.message == 'No session user', 'No session user');
46-
45+
test('API: Get My Tasks as anonymous user', async t => {
46+
let response = await new Promise(function (resolve, reject) {
47+
t.context.request.get({
48+
uri: '/api/tasks/me'
49+
}, function (error, response, body) {
50+
if (error) {
51+
reject(error);
52+
}
53+
resolve(response);
54+
});
55+
});
56+
57+
t.is(response.statusCode, 401, response.body.toString());
58+
t.true(response.body.message === 'No session user', response.body.toString());
4759
});
4860

49-
test('API: Create Task with anonymous user', async t => {
50-
let response = await supertest(appComponents.app)
51-
.post('/api/tasks')
52-
.expect(401);
53-
54-
t.is(401, response.statusCode, 'successful');
55-
t.true(response.body.message == 'No session user', 'No session user');
56-
61+
test('API: Create Task as anonymous user', async t => {
62+
let response = await new Promise(function (resolve, reject) {
63+
t.context.request.post({
64+
uri: '/api/tasks',
65+
body: {
66+
title: 'my test task'
67+
}
68+
}, function (error, response, body) {
69+
if (error) {
70+
reject(error);
71+
}
72+
resolve(response);
73+
});
74+
});
75+
76+
t.is(response.statusCode, 401, response.body.toString());
77+
t.true(response.body.message === 'No session user', response.body.toString());
5778
});
5879

59-
test('API: Update Task with anonymous user', async t => {
60-
let response = await supertest(appComponents.app)
61-
.put('/api/tasks')
62-
.expect(401);
63-
64-
t.is(401, response.statusCode, 'successful');
65-
t.true(response.body.message == 'No session user', 'No session user');
66-
80+
test('API: Update Task as anonymous user', async t => {
81+
let response = await new Promise(function (resolve, reject) {
82+
t.context.request.put({
83+
uri: '/api/tasks',
84+
body: {
85+
title: 'my test task'
86+
}
87+
}, function (error, response, body) {
88+
if (error) {
89+
reject(error);
90+
}
91+
resolve(response);
92+
});
93+
});
94+
95+
t.is(response.statusCode, 401, response.body.toString());
96+
t.true(response.body.message === 'No session user', response.body.toString());
6797
});
6898

69-
test('API: Delete Task with anonymous user', async t => {
70-
let response = await supertest(appComponents.app)
71-
.delete('/api/tasks')
72-
.expect(401);
73-
74-
t.is(401, response.statusCode, 'successful');
75-
t.true(response.body.message == 'No session user', 'No session user');
76-
99+
test('API: Delete Task as anonymous user', async t => {
100+
let response = await new Promise(function (resolve, reject) {
101+
t.context.request.delete({
102+
uri: '/api/tasks'
103+
}, function (error, response, body) {
104+
if (error) {
105+
reject(error);
106+
}
107+
resolve(response);
108+
});
109+
});
110+
111+
t.is(response.statusCode, 401, response.body.toString());
112+
t.true(response.body.message === 'No session user', response.body.toString());
77113
});

server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"passport-local": "~1.0.0",
6868
"passport-paypal-openidconnect": "~0.1.1",
6969
"passport-twitter": "~1.0.4",
70+
"request": "^2.79.0",
7071
"sequelize": "^3.28.0",
7172
"serve-favicon": "~2.3.0",
7273
"socket.io": "^1.4.8",

0 commit comments

Comments
 (0)