-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetup.ts
40 lines (30 loc) · 1.17 KB
/
setup.ts
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
import * as path from 'path';
import { GenericContainer } from 'testcontainers';
import { StartedTestContainer } from 'testcontainers/dist/test-container';
import logger from 'testcontainers/dist/logger';
let mockServer: StartedTestContainer;
const startMockServer = async (): Promise<StartedTestContainer> => {
let mockServerIP: string;
let mockServerPort: string;
const buildContext = path.resolve(__dirname);
logger.info(`BuildContext path: ${buildContext}`);
const port = 3000;
try {
const container = await GenericContainer.fromDockerfile(buildContext).build();
mockServer = await container.withExposedPorts(3000).start();
mockServerIP = await mockServer.getContainerIpAddress();
mockServerPort = String(await mockServer.getMappedPort(port));
const url = `http://${mockServerIP}:${mockServerPort}`;
logger.info(`MOCK_SERVER_URL: ${url}`);
process.env.MOCK_SERVER_URL = url;
return mockServer;
} catch (error) {
logger.error(error);
throw new Error(error);
}
};
export const stopMockServer = async () => {
await mockServer.stop();
logger.info('Mock Server stopped.');
};
export default async () => await startMockServer();