Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move test reinit to mocha's after hooks #558

Merged
merged 3 commits into from
Aug 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions test/integration/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,20 @@ const initialize = () => migrator
.raw('drop owned by current_user')
.then(() => migrator.migrate.latest({ directory: appRoot + '/lib/model/migrations' }))
.then(() => withDefaults({ db, bcrypt }).transacting(populate));
const reinit = (f) => (x) => { initialize().then(() => f(x)); };

before(initialize);

let mustReinitAfter;
beforeEach(() => {
if(mustReinitAfter) throw new Error(`Failed to reinitalize after previous test: '${mustReinitAfter}'. You may need to increase your mocha timeout.`);
});
afterEach(async () => {
if(mustReinitAfter) {
await initialize();
mustReinitAfter = false;
}
});

// augments a supertest object with a `.as(user, cb)` method, where user may be the
// name of a fixture user or an object with email/password. the user will be logged
// in and the following single request will be performed as that user.
Expand Down Expand Up @@ -121,9 +131,10 @@ const testService = (test) => () => new Promise((resolve, reject) => {
// for some tests we explicitly need to make concurrent requests, in which case
// the transaction butchering we do for testService will not work. for these cases,
// we offer testServiceFullTrx:
const testServiceFullTrx = (test) => () => new Promise((resolve, reject) =>
test(augment(request(service(baseContainer))), baseContainer)
.then(reinit(resolve), reinit(reject)));
const testServiceFullTrx = (test) => function() {
mustReinitAfter = this.test.fullTitle();
return test(augment(request(service(baseContainer))), baseContainer);
};

// for some tests we just want a container, without any of the webservice stuffs between.
// this is that, with the same transaction trickery as a normal test.
Expand All @@ -135,8 +146,10 @@ const testContainer = (test) => () => new Promise((resolve, reject) => {
});

// complete the square of options:
const testContainerFullTrx = (test) => () => new Promise((resolve, reject) =>
test(baseContainer).then(reinit(resolve), reinit(reject)));
const testContainerFullTrx = (test) => function() {
mustReinitAfter = this.test.fullTitle();
return test(baseContainer);
};

// called to get a container context per task. ditto all // from testService.
// here instead our weird hijack work involves injecting our own constructed
Expand Down