2021-09-14 20:17:13 +02:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import express from 'express';
|
|
|
|
import { createTestConfig } from '../test/config/test-config';
|
|
|
|
import { start, create } from './server-impl';
|
2017-06-06 09:56:29 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
jest.mock(
|
|
|
|
'./routes',
|
|
|
|
() =>
|
|
|
|
class Index {
|
|
|
|
router() {
|
|
|
|
return express.Router();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-12-16 14:49:11 +01:00
|
|
|
const noop = () => {};
|
|
|
|
|
2019-06-18 19:59:24 +02:00
|
|
|
const eventStore = new EventEmitter();
|
2020-04-13 22:38:46 +02:00
|
|
|
const settingStore = {
|
|
|
|
get: () => {
|
|
|
|
Promise.resolve('secret');
|
|
|
|
},
|
|
|
|
};
|
2019-06-18 19:59:24 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
jest.mock('./metrics', () => ({
|
|
|
|
createMetricsMonitor() {
|
|
|
|
return {
|
|
|
|
startMonitoring: noop,
|
|
|
|
stopMonitoring: noop,
|
|
|
|
};
|
2017-06-06 09:56:29 +02:00
|
|
|
},
|
2021-05-28 11:10:24 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('./db', () => ({
|
|
|
|
createStores() {
|
|
|
|
return {
|
2021-06-15 12:32:35 +02:00
|
|
|
db: { destroy: () => undefined },
|
2021-05-28 11:10:24 +02:00
|
|
|
clientInstanceStore: { destroy: noop },
|
|
|
|
clientMetricsStore: { destroy: noop, on: noop },
|
|
|
|
eventStore,
|
2022-09-15 09:47:57 +02:00
|
|
|
publicSignupTokenStore: { destroy: noop, on: noop },
|
2021-05-28 11:10:24 +02:00
|
|
|
settingStore,
|
|
|
|
};
|
2021-04-22 10:07:10 +02:00
|
|
|
},
|
2021-05-28 11:10:24 +02:00
|
|
|
}));
|
|
|
|
|
2021-09-10 12:07:04 +02:00
|
|
|
jest.mock('../migrator', () => ({
|
|
|
|
migrateDb: () => Promise.resolve(),
|
|
|
|
}));
|
2021-05-28 11:10:24 +02:00
|
|
|
|
|
|
|
jest.mock(
|
|
|
|
'./util/version',
|
|
|
|
() =>
|
2021-08-12 15:04:37 +02:00
|
|
|
function () {
|
2021-05-28 11:10:24 +02:00
|
|
|
return 'unleash-test-version';
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
test('should call preHook', async () => {
|
2017-06-06 09:56:29 +02:00
|
|
|
let called = 0;
|
2021-04-22 10:07:10 +02:00
|
|
|
const config = createTestConfig({
|
|
|
|
server: { port: 0 },
|
2017-06-06 09:56:29 +02:00
|
|
|
preHook: () => {
|
|
|
|
called++;
|
|
|
|
},
|
|
|
|
});
|
2021-09-14 20:17:13 +02:00
|
|
|
const { stop } = await start(config);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(called).toBe(1);
|
2021-09-14 20:17:13 +02:00
|
|
|
await stop();
|
2017-06-06 09:56:29 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should call preRouterHook', async () => {
|
2017-06-06 09:56:29 +02:00
|
|
|
let called = 0;
|
2021-09-14 20:17:13 +02:00
|
|
|
const { stop } = await start(
|
2021-04-22 10:07:10 +02:00
|
|
|
createTestConfig({
|
|
|
|
server: { port: 0 },
|
|
|
|
preRouterHook: () => {
|
|
|
|
called++;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(called === 1).toBe(true);
|
2021-09-14 20:17:13 +02:00
|
|
|
await stop();
|
2017-06-06 09:56:29 +02:00
|
|
|
});
|
2019-06-18 19:59:24 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should call eventHook', async () => {
|
2019-06-18 19:59:24 +02:00
|
|
|
let called = 0;
|
2021-04-22 10:07:10 +02:00
|
|
|
const config = createTestConfig({
|
|
|
|
server: { port: 0 },
|
2019-06-18 19:59:24 +02:00
|
|
|
eventHook: () => {
|
|
|
|
called++;
|
|
|
|
},
|
|
|
|
});
|
2021-09-14 20:17:13 +02:00
|
|
|
const { stop } = await start(config);
|
2019-06-18 19:59:24 +02:00
|
|
|
eventStore.emit('feature-created', {});
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(called === 1).toBe(true);
|
2021-09-14 20:17:13 +02:00
|
|
|
await stop();
|
2019-06-18 19:59:24 +02:00
|
|
|
});
|
2020-06-17 08:03:02 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should auto-create server on start()', async () => {
|
2021-09-14 20:17:13 +02:00
|
|
|
const { server, stop } = await start(
|
2021-04-22 10:07:10 +02:00
|
|
|
createTestConfig({ server: { port: 0 } }),
|
|
|
|
);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(typeof server === 'undefined').toBe(false);
|
2021-09-14 20:17:13 +02:00
|
|
|
await stop();
|
2020-06-17 08:03:02 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not create a server using create()', async () => {
|
2021-04-22 10:07:10 +02:00
|
|
|
const config = createTestConfig({ server: { port: 0 } });
|
2021-09-14 20:17:13 +02:00
|
|
|
const { server, stop } = await create(config);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(server).toBeUndefined();
|
2021-09-14 20:17:13 +02:00
|
|
|
await stop();
|
2020-06-17 08:03:02 +02:00
|
|
|
});
|
2020-12-16 14:49:11 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should shutdown the server when calling stop()', async () => {
|
2021-09-14 20:17:13 +02:00
|
|
|
const { server, stop } = await start(
|
2021-04-22 10:07:10 +02:00
|
|
|
createTestConfig({ server: { port: 0 } }),
|
|
|
|
);
|
2020-12-16 14:49:11 +01:00
|
|
|
await stop();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(server.address()).toBe(null);
|
2020-12-16 14:49:11 +01:00
|
|
|
});
|