Files
directus/tests/setup/teardown.ts
Pascal Jufer acd41eb0be Syntax fixes (#5367)
* Declare return types on functions

And a very few other type related minor fixes

* Minor syntax fixes

* Remove unnecessary escape chars in regexes
* Remove unnecessary awaits
* Replace deprecated req.connection with req.socket
* Replace deprecated upload with uploadOne
* Remove unnecessary eslint-disable-next-line comments
* Comment empty functions / catch or finally clauses
* Fix irregular whitespaces
* Add missing returns (null)
* Remove unreachable code
* A few logical fixes
* Remove / Handle non-null assertions which are certainly unnecessary (e.g. in
tests)
2021-04-29 12:11:43 -04:00

114 lines
3.0 KiB
TypeScript

import Listr from 'listr';
import Dockerode from 'dockerode';
import { getDBsToTest } from '../get-dbs-to-test';
import config, { CONTAINER_PERSISTENCE_FILE } from '../config';
import { GlobalConfigTsJest } from 'ts-jest/dist/types';
import { readFileSync, rmSync } from 'fs';
import global from './global';
const docker = new Dockerode();
if (require.main === module) {
teardown(undefined, true);
}
export default async function teardown(jestConfig?: GlobalConfigTsJest, isAfterWatch = false): Promise<void> {
if (jestConfig?.watch || jestConfig?.watchAll) return;
const vendors = getDBsToTest();
console.log('\n');
await new Listr([
{
skip: () => !isAfterWatch,
title: 'Restore container info',
task: async () => {
const containers = JSON.parse(readFileSync(CONTAINER_PERSISTENCE_FILE).toString());
if (!(containers.db && containers.directus)) {
throw new Error('Could not load saved containers');
}
const restorePersistedContainer = ({ vendor, id }: { vendor: string; id: string }) => ({
vendor,
container: docker.getContainer(id),
});
global.databaseContainers = containers.db.map(restorePersistedContainer);
global.directusContainers = containers.directus.map(restorePersistedContainer);
rmSync(CONTAINER_PERSISTENCE_FILE);
},
},
{
title: 'Stop Docker containers',
task: () => {
return new Listr(
vendors.map((vendor) => {
return {
title: config.names[vendor]!,
task: () => {
return new Listr(
[
{
title: 'Database',
task: async () =>
await global.databaseContainers
.find((containerInfo) => containerInfo.vendor === vendor)
?.container.stop(),
},
{
title: 'Directus',
task: async () =>
await global.directusContainers
.find((containerInfo) => containerInfo.vendor === vendor)
?.container.stop(),
},
],
{ concurrent: true, exitOnError: false }
);
},
};
}),
{ concurrent: true, exitOnError: false }
);
},
},
{
title: 'Remove Docker containers',
task: () => {
return new Listr(
vendors.map((vendor) => {
return {
title: config.names[vendor]!,
task: () => {
return new Listr(
[
{
title: 'Database',
task: async () =>
await global.databaseContainers
.find((containerInfo) => containerInfo.vendor === vendor)
?.container.remove(),
},
{
title: 'Directus',
task: async () =>
await global.directusContainers
.find((containerInfo) => containerInfo.vendor === vendor)
?.container.remove(),
},
],
{ concurrent: true, exitOnError: false }
);
},
};
}),
{ concurrent: true, exitOnError: false }
);
},
},
]).run();
console.log('\n');
console.log(`👮‍♀️ Tests complete!\n`);
}