forked from goldbergyoni/nodejs-testing-best-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal-setup.js
38 lines (33 loc) · 1.29 KB
/
global-setup.js
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
const isPortReachable = require('is-port-reachable');
const path = require('path');
const dockerCompose = require('docker-compose');
const npm = require('npm');
const util = require('util');
module.exports = async () => {
console.time('global-setup');
// ️️️✅ Best Practice: Speed up during development, if already live then do nothing
const isDBReachable = await isPortReachable(54310);
if (!isDBReachable) {
// ️️️✅ Best Practice: Start the infrastructure within a test hook - No failures occur because the DB is down
await dockerCompose.upAll({
cwd: path.join(__dirname),
log: true,
});
await dockerCompose.exec(
'database',
['sh', '-c', 'until pg_isready ; do sleep 1; done'],
{
cwd: path.join(__dirname),
}
);
// ️️️✅ Best Practice: Use npm script for data seeding and migrations
const npmLoadAsPromise = util.promisify(npm.load);
await npmLoadAsPromise();
const npmCommandAsPromise = util.promisify(npm.commands.run);
await npmCommandAsPromise(['db:migrate']);
// ✅ Best Practice: Seed only metadata and not test record, read "Dealing with data" section for further information
await npmCommandAsPromise(['db:seed']);
}
// 👍🏼 We're ready
console.timeEnd('global-setup');
};