2021-09-14 19:30:11 +02:00
|
|
|
import express from 'express';
|
|
|
|
import { createTestConfig } from '../test/config/test-config';
|
2023-12-16 08:06:26 +01:00
|
|
|
import compression from 'compression';
|
|
|
|
|
|
|
|
jest.mock('compression', () =>
|
|
|
|
jest.fn().mockImplementation(() => (req, res, next) => {
|
|
|
|
next();
|
|
|
|
}),
|
|
|
|
);
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
jest.mock(
|
|
|
|
'./routes',
|
|
|
|
() =>
|
|
|
|
class Index {
|
|
|
|
router() {
|
|
|
|
return express.Router();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
const getApp = require('./app').default;
|
2016-12-29 11:42:54 +01:00
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('should not throw when valid config', async () => {
|
2021-04-22 10:07:10 +02:00
|
|
|
const config = createTestConfig();
|
2022-01-06 10:31:00 +01:00
|
|
|
const app = await getApp(config, {}, {});
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(typeof app.listen).toBe('function');
|
2016-12-29 11:42:54 +01:00
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('should call preHook', async () => {
|
2016-12-29 11:42:54 +01:00
|
|
|
let called = 0;
|
2021-04-22 10:07:10 +02:00
|
|
|
const config = createTestConfig({
|
2017-06-28 10:17:14 +02:00
|
|
|
preHook: () => {
|
|
|
|
called++;
|
|
|
|
},
|
|
|
|
});
|
2022-01-06 10:31:00 +01:00
|
|
|
await getApp(config, {}, {});
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(called).toBe(1);
|
2016-12-29 11:42:54 +01:00
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('should call preRouterHook', async () => {
|
2016-12-29 11:42:54 +01:00
|
|
|
let called = 0;
|
2021-04-22 10:07:10 +02:00
|
|
|
const config = createTestConfig({
|
2017-06-28 10:17:14 +02:00
|
|
|
preRouterHook: () => {
|
|
|
|
called++;
|
|
|
|
},
|
|
|
|
});
|
2022-01-06 10:31:00 +01:00
|
|
|
await getApp(config, {}, {});
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(called).toBe(1);
|
2016-12-29 11:42:54 +01:00
|
|
|
});
|
2023-12-16 08:06:26 +01:00
|
|
|
|
|
|
|
describe('compression middleware', () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
(compression as jest.Mock).mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
(compression as jest.Mock).mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
test.each([
|
|
|
|
{
|
|
|
|
disableCompression: true,
|
|
|
|
expectCompressionEnabled: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
disableCompression: false,
|
|
|
|
expectCompressionEnabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
disableCompression: null,
|
|
|
|
expectCompressionEnabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
disableCompression: undefined,
|
|
|
|
expectCompressionEnabled: true,
|
|
|
|
},
|
|
|
|
])(
|
|
|
|
`should expect the compression middleware to be $expectCompressionEnabled when configInput.server.disableCompression is $disableCompression`,
|
|
|
|
async ({ disableCompression, expectCompressionEnabled }) => {
|
|
|
|
const config = createTestConfig({
|
|
|
|
server: {
|
|
|
|
disableCompression: disableCompression as any,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await getApp(config, {}, {});
|
|
|
|
expect(compression).toBeCalledTimes(+expectCompressionEnabled);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|