Skip to content

Commit 5414b38

Browse files
update tests to wait for Database to finish seeding using events
1 parent e7fffc4 commit 5414b38

6 files changed

Lines changed: 32 additions & 9 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"babel-node": "babel-node --presets=es2015",
88
"nodemon": "nodemon -e pug,js,css --exec npm run babel-node -- src/bin/www",
99
"start": "SET NODE_ENV=dev && npm run nodemon",
10-
"test": "mocha --exit --timeout 5000 --compilers js:babel-core/register",
11-
"coverage": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha --exit --timeout 5000 --compilers js:babel-core/register"
10+
"test": "mocha --exit --timeout 10000 --compilers js:babel-core/register",
11+
"coverage": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha --exit --timeout 10000 --compilers js:babel-core/register"
1212
},
1313
"author": "",
1414
"license": "ISC",

src/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ db.on('connected', function() {
4242
// Callback to populate DB once collections have been cleared
4343
seeder.populateModels(data, function() {
4444
console.log("Finished seeding Database!");
45+
app.emit("appStarted"); //Emits an event to tell our tests it is ok to now test.
4546
});
4647
});
4748
});

src/bin/www

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var port = normalizePort(process.env.PORT || '3000');
1616
app.set('port', port);
1717
console.log("Listening on port", port);
1818

19+
1920
/**
2021
* Create HTTP server.
2122
*/
@@ -26,10 +27,11 @@ var server = http.createServer(app);
2627
* Listen on provided port, on all network interfaces.
2728
*/
2829

29-
server.listen(port);
3030
server.on('error', onError);
3131
server.on('listening', onListening);
3232

33+
34+
3335
/**
3436
* Normalize a port into a number, string, or false.
3537
*/

src/finishSeed.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const server = require("./app");
2+
3+
let dataBaseFinishSeed = false;
4+
5+
server.on('appStarted', () => {
6+
dataBaseFinishSeed = true;
7+
});
8+
9+
export {dataBaseFinishSeed}

test/course_test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
//During the test the env variable is set to test
33
process.env.NODE_ENV = 'test';
4+
import {dataBaseFinishSeed} from "../src/finishSeed";
45

56
const Course = require('../src/models/course'),
67
chai = require('chai'),
@@ -12,17 +13,19 @@ const should = chai.should();
1213
const courseIndexLink = '/api/courses/';
1314
chai.use(chaiHttp);
1415

16+
1517
const validAuth = {
1618
user: 'joe@smith.com',
1719
pass: 'password'
1820
};
1921

2022
describe('Courses', () => {
21-
// beforeEach((done) => {
22-
// Course.remove({}, (err) => {
23-
// done();
24-
// });
25-
// });
23+
beforeEach((done) => { //This hangs the tests until databaesFinish seed..
24+
if(dataBaseFinishSeed){done();} //if it has finished seeding it will hit callback, else forever hangs
25+
server.on('appStarted', () => { //Once finish seed, set to true and stop hang.
26+
done();
27+
});
28+
});
2629
/*
2730
Our GET Tests
2831
*/
@@ -68,7 +71,7 @@ describe('Courses', () => {
6871
.auth(validAuth.user, validAuth.pass)
6972
.send(course)
7073
.end((err, res) => {
71-
res.body.should.have.property('message').equal('test');
74+
res.body.should.have.property('message').equal('Course Successfully added!');
7275
res.should.have.status(201);
7376
res.body.should.be.a('object');
7477
res.body.course.should.have.property('title');

test/user_test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ const expect = chai.expect;
1313
const postAPI = '/api/users';
1414
const getAPI = postAPI;
1515

16+
import {dataBaseFinishSeed} from "../src/finishSeed";
17+
1618

1719
describe('Users', () => {
20+
beforeEach((done) => { //This hangs the tests until databaesFinish seed..
21+
if(dataBaseFinishSeed){done();} //if it has finished seeding it will hit callback, else forever hangs
22+
server.on('appStarted', () => { //Once finish seed, set to true and stop hang.
23+
done();
24+
});
25+
});
1826

1927
describe('/GET users', () => {
2028
it('should not GET if auth headers are missing', (done) => {

0 commit comments

Comments
 (0)