Skip to content

Commit

Permalink
fix(tests): refactoring tests to dismiss the supertest wrapper
Browse files Browse the repository at this point in the history
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
lirantal committed Jan 1, 2017
1 parent 7430140 commit dc17715
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 55 deletions.
12 changes: 12 additions & 0 deletions server/config/env/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ module.exports = {
// Enable mongoose debug mode
debug: process.env.MONGODB_DEBUG || false
},
orm: {
dbname: 'meandev',
user: 'root',
pass: 'root',
options: {
// sequelize supports one of: mysql, postgres, sqlite, mariadb and mssql.
dialect: 'mysql',
host: '',
port: ''
}
},
log: {
// logging with Morgan - https://github.com/expressjs/morgan
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
Expand Down Expand Up @@ -82,6 +93,7 @@ module.exports = {
},
seedDB: {
seed: process.env.MONGO_SEED === 'true',
reset: process.env.MONGO_SEED_RESET === 'true',
options: {
logResults: process.env.MONGO_SEED_LOG_RESULTS !== 'false',
seedUser: {
Expand Down
146 changes: 91 additions & 55 deletions server/modules/tasks/test/tasks.server.routes.sanity.test.js
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());
});
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"passport-local": "~1.0.0",
"passport-paypal-openidconnect": "~0.1.1",
"passport-twitter": "~1.0.4",
"request": "^2.79.0",
"sequelize": "^3.28.0",
"serve-favicon": "~2.3.0",
"socket.io": "^1.4.8",
Expand Down

0 comments on commit dc17715

Please sign in to comment.