mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
Merge pull request #6196 from crookedneighbor/add_unit_test_helpers
Add unit test helpers
This commit is contained in:
@@ -121,13 +121,14 @@
|
|||||||
"karma-ng-html2js-preprocessor": "~0.1.0",
|
"karma-ng-html2js-preprocessor": "~0.1.0",
|
||||||
"karma-phantomjs-launcher": "~0.1.0",
|
"karma-phantomjs-launcher": "~0.1.0",
|
||||||
"karma-requirejs": "~0.2.0",
|
"karma-requirejs": "~0.2.0",
|
||||||
"requirejs": "~2.1",
|
|
||||||
"karma-script-launcher": "~0.1.0",
|
"karma-script-launcher": "~0.1.0",
|
||||||
"lcov-result-merger": "^1.0.2",
|
"lcov-result-merger": "^1.0.2",
|
||||||
|
"lodash.defaultsdeep": "^3.10.0",
|
||||||
"mocha": "^2.3.3",
|
"mocha": "^2.3.3",
|
||||||
"mongodb": "^2.0.46",
|
"mongodb": "^2.0.46",
|
||||||
"mongoskin": "~0.6.1",
|
"mongoskin": "~0.6.1",
|
||||||
"protractor": "~2.0.0",
|
"protractor": "~2.0.0",
|
||||||
|
"requirejs": "~2.1",
|
||||||
"rewire": "^2.3.3",
|
"rewire": "^2.3.3",
|
||||||
"shelljs": "^0.4.0",
|
"shelljs": "^0.4.0",
|
||||||
"sinon": "^1.17.2",
|
"sinon": "^1.17.2",
|
||||||
|
|||||||
@@ -297,6 +297,7 @@ gulp.task('test:e2e:safe', ['test:prepare', 'test:prepare:server'], (cb) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
gulp.task('test:api-v2', ['test:prepare:server'], (done) => {
|
gulp.task('test:api-v2', ['test:prepare:server'], (done) => {
|
||||||
|
process.env.API_VERSION = 'v2';
|
||||||
awaitPort(TEST_SERVER_PORT).then(() => {
|
awaitPort(TEST_SERVER_PORT).then(() => {
|
||||||
runMochaTests('./test/api/v2/**/*.js', server, done)
|
runMochaTests('./test/api/v2/**/*.js', server, done)
|
||||||
});
|
});
|
||||||
@@ -329,15 +330,16 @@ gulp.task('test:api-v3', ['test:api-v3:unit', 'test:api-v3:integration']);
|
|||||||
|
|
||||||
gulp.task('test:api-v3:watch', ['test:api-v3:unit:watch', 'test:api-v3:integration:watch']);
|
gulp.task('test:api-v3:watch', ['test:api-v3:unit:watch', 'test:api-v3:integration:watch']);
|
||||||
|
|
||||||
gulp.task('test:api-v3:unit', ['test:prepare:server'], (done) => {
|
gulp.task('test:api-v3:unit', (done) => {
|
||||||
runMochaTests('./test/api/v3/unit/**/*.js', null, done)
|
runMochaTests('./test/api/v3/unit/**/*.js', null, done)
|
||||||
});
|
});
|
||||||
|
|
||||||
gulp.task('test:api-v3:unit:watch', ['test:prepare:server'], () => {
|
gulp.task('test:api-v3:unit:watch', () => {
|
||||||
gulp.watch(['website/src/**', 'test/api/v3/unit/**'], ['test:api-v3:unit']);
|
gulp.watch(['website/src/**', 'test/api/v3/unit/**'], ['test:api-v3:unit']);
|
||||||
});
|
});
|
||||||
|
|
||||||
gulp.task('test:api-v3:integration', ['test:prepare:server'], (done) => {
|
gulp.task('test:api-v3:integration', ['test:prepare:server'], (done) => {
|
||||||
|
process.env.API_VERSION = 'v3';
|
||||||
awaitPort(TEST_SERVER_PORT).then(() => {
|
awaitPort(TEST_SERVER_PORT).then(() => {
|
||||||
runMochaTests('./test/api/v3/unit/**/*.js', server, done)
|
runMochaTests('./test/api/v3/unit/**/*.js', server, done)
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,33 +1,29 @@
|
|||||||
|
import {
|
||||||
|
generateRes,
|
||||||
|
generateReq,
|
||||||
|
generateNext,
|
||||||
|
} from '../../../../helpers/api-unit.helper';
|
||||||
|
|
||||||
import errorHandler from '../../../../../website/src/middlewares/api-v3/errorHandler';
|
import errorHandler from '../../../../../website/src/middlewares/api-v3/errorHandler';
|
||||||
|
|
||||||
import { BadRequest } from '../../../../../website/src/libs/api-v3/errors';
|
import { BadRequest } from '../../../../../website/src/libs/api-v3/errors';
|
||||||
import logger from '../../../../../website/src/libs/api-v3/logger';
|
import logger from '../../../../../website/src/libs/api-v3/logger';
|
||||||
|
|
||||||
describe('errorHandler', () => {
|
describe('errorHandler', () => {
|
||||||
let res, req;
|
let res, req, next;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
res = {
|
res = generateRes();
|
||||||
status: sinon.stub().returnsThis(),
|
req = generateReq();
|
||||||
json: sinon.stub(),
|
next = generateNext();
|
||||||
};
|
|
||||||
req = {
|
|
||||||
originalUrl: 'foo',
|
|
||||||
headers: {},
|
|
||||||
body: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
sinon.stub(logger, 'error');
|
sandbox.stub(logger, 'error');
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
logger.error.restore();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends internal server error if error is not a CustomError', () => {
|
it('sends internal server error if error is not a CustomError', () => {
|
||||||
let error = new Error();
|
let error = new Error();
|
||||||
|
|
||||||
errorHandler(error, req, res);
|
errorHandler(error, req, res, next);
|
||||||
|
|
||||||
expect(res.status).to.be.calledOnce;
|
expect(res.status).to.be.calledOnce;
|
||||||
expect(res.json).to.be.calledOnce;
|
expect(res.json).to.be.calledOnce;
|
||||||
@@ -42,7 +38,7 @@ describe('errorHandler', () => {
|
|||||||
it('sends CustomError', () => {
|
it('sends CustomError', () => {
|
||||||
let error = new BadRequest();
|
let error = new BadRequest();
|
||||||
|
|
||||||
errorHandler(error, req, res);
|
errorHandler(error, req, res, next);
|
||||||
|
|
||||||
expect(res.status).to.be.calledOnce;
|
expect(res.status).to.be.calledOnce;
|
||||||
expect(res.json).to.be.calledOnce;
|
expect(res.json).to.be.calledOnce;
|
||||||
@@ -57,7 +53,7 @@ describe('errorHandler', () => {
|
|||||||
it('logs error', () => {
|
it('logs error', () => {
|
||||||
let error = new BadRequest();
|
let error = new BadRequest();
|
||||||
|
|
||||||
errorHandler(error, req, res);
|
errorHandler(error, req, res, next);
|
||||||
|
|
||||||
expect(logger.error).to.be.calledOnce;
|
expect(logger.error).to.be.calledOnce;
|
||||||
expect(logger.error).to.be.calledWith(error.stack, {
|
expect(logger.error).to.be.calledWith(error.stack, {
|
||||||
@@ -68,7 +64,6 @@ describe('errorHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('does not send error if error is not defined', () => {
|
it('does not send error if error is not defined', () => {
|
||||||
let next = sinon.stub();
|
|
||||||
errorHandler(null, req, res, next);
|
errorHandler(null, req, res, next);
|
||||||
|
|
||||||
expect(next).to.be.calledOnce;
|
expect(next).to.be.calledOnce;
|
||||||
|
|||||||
@@ -208,9 +208,10 @@ export function resetHabiticaDB() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function _requestMaker(user, method, additionalSets) {
|
function _requestMaker(user, method, additionalSets) {
|
||||||
|
const API_V = process.env.API_VERSION || 'v2'
|
||||||
return (route, send, query) => {
|
return (route, send, query) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let request = superagent[method](`http://localhost:${API_TEST_SERVER_PORT}/api/v2${route}`)
|
let request = superagent[method](`http://localhost:${API_TEST_SERVER_PORT}/api/${API_V}${route}`)
|
||||||
.accept('application/json');
|
.accept('application/json');
|
||||||
|
|
||||||
if (user && user._id && user.apiToken) {
|
if (user && user._id && user.apiToken) {
|
||||||
|
|||||||
46
test/helpers/api-unit.helper.js
Normal file
46
test/helpers/api-unit.helper.js
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
// @TODO: remove when lodash can be upgraded
|
||||||
|
import defaults from 'lodash.defaultsdeep';
|
||||||
|
import { model as User } from '../../website/src/models/user'
|
||||||
|
import { model as Group } from '../../website/src/models/group'
|
||||||
|
import i18n from '../../common/script/src/i18n';
|
||||||
|
require('coffee-script');
|
||||||
|
i18n.translations = require('../../website/src/libs/i18n.js').translations;
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
sandbox.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
export function generateUser(options={}) {
|
||||||
|
return new User(options).toObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateGroup(options={}) {
|
||||||
|
return new Group(options).toObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateRes(options={}) {
|
||||||
|
let defaultRes = {
|
||||||
|
send: sandbox.stub(),
|
||||||
|
status: sandbox.stub().returnsThis(),
|
||||||
|
json: sandbox.stub(),
|
||||||
|
locals: {
|
||||||
|
user: generateUser(options.localsUser),
|
||||||
|
group: generateGroup(options.localsGroup),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return defaults(options, defaultRes);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateReq(options={}) {
|
||||||
|
let defaultReq = {
|
||||||
|
body: {},
|
||||||
|
query: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
return defaults(options, defaultReq);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateNext(func) {
|
||||||
|
return func || sandbox.stub();
|
||||||
|
}
|
||||||
@@ -8,3 +8,5 @@ global.sinon = require("sinon");
|
|||||||
chai.use(require("sinon-chai"))
|
chai.use(require("sinon-chai"))
|
||||||
chai.use(require("chai-as-promised"));
|
chai.use(require("chai-as-promised"));
|
||||||
global.expect = chai.expect
|
global.expect = chai.expect
|
||||||
|
|
||||||
|
global.sandbox = sinon.sandbox.create();
|
||||||
|
|||||||
Reference in New Issue
Block a user