support.js
1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
const Support = require('../support');
const runningQueries = new Map();
const runningTransactions = new Map(); // map transaction option to queries.
before(function() {
this.sequelize.addHook('transactionCreated', t => { // tracking race condition, remove me if no longer present.
t.trace = new Error().stack;
});
this.sequelize.addHook('beforeQuery', (options, query, sql) => {
runningQueries.set(query, options);
if (options.transaction) {
const queryList = runningTransactions.get(options.transaction.id);
if (queryList) {
queryList.push(sql);
} else {
runningTransactions.set(options.transaction.id, [sql]);
}
}
});
this.sequelize.addHook('afterQuery', (options, query, sql) => {
runningQueries.delete(query);
if (options.transaction && ['COMMIT', 'ROLLBACK'].includes(sql)) {
runningTransactions.delete(options.transaction.id);
}
});
});
beforeEach(function() {
return Support.clearDatabase(this.sequelize);
});
afterEach(function() {
if (runningQueries.size === 0) {
return;
}
let msg = `Expected 0 running queries. ${runningQueries.size} queries still running in ${this.currentTest.fullTitle()}\n`;
msg += 'Queries:\n\n';
for (const [query, options] of runningQueries) {
msg += `${query.uuid}: ${query.sql}\n`;
if (options.transaction) {
const relatedTransaction = runningTransactions.get(options.transaction.id);
if (relatedTransaction) {
msg += options.transaction.trace;
msg += 'In transaction:\n\n';
msg += relatedTransaction.join('\n');
}
}
}
throw new Error(msg);
});
module.exports = Support;