Skip to content

Commit

Permalink
making mocha fail with unhandled promises
Browse files Browse the repository at this point in the history
  • Loading branch information
orlando committed Sep 18, 2018
1 parent 98ad21c commit 58bd833
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
// "vue",
"vue-app"
]
}
}
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defaults: &defaults
docker:
- image: circleci/node:8.11.4-stretch
- image: circleci/postgres:9.6.2-alpine
- image: circleci/redis:3-alpine

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion controllers/Admin/DisputesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { NotFoundError } = require('$lib/errors');
const DisputeTool = require('$models/DisputeTool');
const DisputeStatus = require('$models/DisputeStatus');
const config = require('$config/config');
const RestfulController = require('$libcore/controllers/RestfulController');
const RestfulController = require('$lib/core/controllers/RestfulController');

const {
authenticate,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
"eslint-plugin-mocha": "^4.3.0",
"eslint-plugin-prettier": "^2.4.0",
"eslint-plugin-vue": "^4.0.0-beta.2",
"faker": "^4.1.0",
"husky": "^0.14.3",
"istanbul-combine": "^0.3.0",
"lint-staged": "^6.0.0",
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/controllers/Admin.UsersControllerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ describe('Admin.UsersController', () => {
});

describe('when admin', () => {
// this endpoint is making requests to get user data from discourse
// right now is failing due Nock not matching the request
// this endpoint is making requests to get user data from discourse and is failing due Nock not matching the request
// but we don't need to be doing that request in the first place and is going to be removed
// I'm disabling this test until then
xit('should allow', () => testAllowed(testGet(url, admin)));
});
});
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/controllers/SessionsControllerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('SessionsController', () => {
const req = testGetPage(url);

// check redirection to sso
req.redirects(0).catch(({ status, response: { headers } }) => {
return req.redirects(0).catch(({ status, response: { headers } }) => {
expect(status).eq(302);
expect(headers.location.startsWith(config.sso.endpoint)).true;
});
Expand All @@ -23,7 +23,7 @@ describe('SessionsController', () => {
const url = config.router.helpers.login.url();
const req = testGetPage(url, user);

req.redirects(0).catch(({ status, response: { headers } }) => {
return req.redirects(0).catch(({ status, response: { headers } }) => {
expect(status).eq(302);
expect(headers.location).eq(config.router.helpers.root.url());
});
Expand All @@ -37,7 +37,7 @@ describe('SessionsController', () => {
const url = config.router.helpers.logout.url();
const req = testGetPage(url, user);

req.redirects(0).catch(({ status, response: { headers } }) => {
return req.redirects(0).catch(({ status, response: { headers } }) => {
expect(status).eq(302);
expect(headers.location).eq(config.router.helpers.root.url());
});
Expand Down
15 changes: 15 additions & 0 deletions tests/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ module.exports = function runTests(testDir) {

process.exit(1);
});

// Workaround to make Mocha fail on UnhandledPromiseRejection errors
// https://github.com/mochajs/mocha/issues/2640#issuecomment-348985952
let unhandledRejectionExitCode = 0;

process.on('unhandledRejection', reason => {
unhandledRejectionExitCode = 1;
throw reason;
});

process.prependListener('exit', code => {
if (code === 0) {
process.exit(unhandledRejectionExitCode);
}
});
};
73 changes: 52 additions & 21 deletions tests/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,57 @@
const uuid = require('uuid');
const sa = require('superagent');
const { expect } = require('chai');
const nock = require('nock');
const { execSync } = require('child_process');
const _ = require('lodash');
const qs = require('querystring');
const faker = require('faker');

// used to mock sessions
const {
sessions,
router,
siteURL,
discourse: { baseUrl },
} = require('$config/config');

const ids = {
_external: 0,
nextExternal() {
return ++this._external;
},
};

// mocking calls to Discourse
nock(baseUrl)
.post('/posts')
.query(true)
.times(1000)
.reply(200, (uri, body) => ({
body,
sent: qs.parse(uri.split('?')[1]),
post: { topic_id: ids.nextExternal() },
}));

const testGroups = {
dispute_pro: {
members: ['ann_l', 'dawn_l'],
owners: [],
},
dispute_coordinator: { members: ['dawn_l'], owners: [] },
};

Object.keys(testGroups).forEach(groupName =>
nock(baseUrl)
.get(`/groups/${groupName}/members.json`)
.query(true)
.times(1000)
.reply(200, {
members: testGroups[groupName].members.map((username, id) => ({ username, id })),
owners: testGroups[groupName].owners.map((username, id) => ({ username, id })),
}),
);

// mocking sessions
const expressSession = require('express-session');
const RedisStore = require('connect-redis')(expressSession);
const cookieSignature = require('cookie-signature');
Expand All @@ -20,17 +67,8 @@ const redisStoreInstance = new RedisStore({
client: redisClient,
});

const { sessions, router, siteURL } = require('$config/config');

const agent = sa.agent();

const ids = {
_external: 0,
nextExternal() {
return ++this._external;
},
};

const withSiteUrl = url => {
if (url.startsWith(siteURL)) {
return url;
Expand All @@ -39,14 +77,6 @@ const withSiteUrl = url => {
return `${siteURL}${url.startsWith('/') ? '' : '/'}${url}`;
};

const testGroups = {
dispute_pro: {
members: ['ann_l', 'dawn_l'],
owners: [],
},
dispute_coordinator: { members: ['dawn_l'], owners: [] },
};

// create objects helper
const helpers = {
testGroups,
Expand All @@ -64,9 +94,10 @@ const helpers = {
_.defaults(params, {
externalId: ids.nextExternal(),
admin: false,
email: `${uuid.v4()}@example.com`,
username: uuid.v4(),
avatarUrl: '{size}',
email: faker.internet.email(),
name: faker.name.findName(),
username: faker.internet.userName(),
avatarUrl: faker.image.imageUrl(),
});

const user = new User(params);
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3917,6 +3917,10 @@ eyes@0.1.x:
version "0.1.8"
resolved "https://registry.yarnpkg.com/eyes/-/eyes-0.1.8.tgz#62cf120234c683785d902348a800ef3e0cc20bc0"

faker@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/faker/-/faker-4.1.0.tgz#1e45bbbecc6774b3c195fad2835109c6d748cc3f"

falafel@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/falafel/-/falafel-2.1.0.tgz#96bb17761daba94f46d001738b3cedf3a67fe06c"
Expand Down

0 comments on commit 58bd833

Please sign in to comment.