Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele committed Mar 22, 2019
1 parent c57facf commit f22b7c3
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const logger = createLogger({
],
});

if (process.env.NODE_ENV !== 'production') {
if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
logger.add(new transports.Console({
format: format.combine(
format.colorize(),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon ./bin/www",
"test": "./node_modules/.bin/mocha --exit",
"test": "./node_modules/.bin/mocha test/**/*.test.js --exit",
"test:cover": "nyc --report-dir ./coverage --reporter=html --reporter=text-summary node_modules/mocha/bin/_mocha --exit",
"test:coveralls": "nyc npm run test:cover && nyc report --reporter=text-lcov | coveralls",
"lint": "./node_modules/.bin/eslint routes/ models/ helpers/ lib/ app.js public/js",
Expand Down
2 changes: 1 addition & 1 deletion test/mock/mockPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = async function mockPlayer(UserId, steamId, username) {
}

if (_.isUndefined(steamId)) {
steamId = faker.random.number();
steamId = faker.random.number(76561190000000000, 76561200000000000);
}

if (_.isUndefined(UserId)) {
Expand Down
2 changes: 1 addition & 1 deletion test/mock/mockUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function mockServer(username, steamId) {
}

if (_.isUndefined(steamId)) {
steamId = faker.random.number();
steamId = faker.random.number(76561190000000000, 76561200000000000);
}

const user = {
Expand Down
40 changes: 40 additions & 0 deletions test/user/deleteUser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';

// Require the dev-dependencies
const chai = require('chai');
const chaiHttp = require('chai-http');
const mock = require('../mock');

// Start the server
const server = require('../../bin/www');

const should = chai.should(); // eslint-disable-line no-unused-vars

chai.use(chaiHttp);

describe('DELETE /api/user/:id', () => {
let testUser;
before(async () => {
testUser = await mock.user();
});

it('Returns 200 and expected data', (done) => {
chai.request(server)
.delete(`/api/user/${testUser.id}`)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.eq(1)
done();
});
});

it('Returns 404 for unknown ID', (done) => {
chai.request(server)
.delete('/api/user/unknownId')
.end((err, res) => {
res.should.have.status(404);
done();
});
});
});
40 changes: 40 additions & 0 deletions test/user/getUserById.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';

// Require the dev-dependencies
const chai = require('chai');
const chaiHttp = require('chai-http');
const mock = require('../mock');

// Start the server
const server = require('../../bin/www');

const should = chai.should(); // eslint-disable-line no-unused-vars

chai.use(chaiHttp);

describe('GET /api/user/:id', () => {
let testUser;
before(async () => {
testUser = await mock.user();
});

it('Returns 200 and expected data', (done) => {
chai.request(server)
.get(`/api/user/${testUser.id}`)
.end((err, res) => {
res.should.have.status(200);
res.body.id.should.be.eq(testUser.id);
done();
});
});

it('Returns 404 for unknown ID', (done) => {
chai.request(server)
.get('/api/user/unknownId')
.end((err, res) => {
res.should.have.status(404);
done();
});
});
});

0 comments on commit f22b7c3

Please sign in to comment.