1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/src/lib/app.test.ts
Pedro Papadopolis 24c2a70138
feat: Make compression middleware optional (#5306)
## Why
Currently AWS API Gateway doesn't have compression enabled by default,
this PR will make it easier to for example deploy Unleash over to AWS
Lambda without further configuration in API Gateway, frameworks like
Serverless requires a bit more work to set up compression and some times
one might not need compression at all.

## How
Create a new config flag called `disableCompression` which will not
include `compression` middleware in express' instance when set as true.
2023-12-16 08:06:26 +01:00

90 lines
2.2 KiB
TypeScript

import express from 'express';
import { createTestConfig } from '../test/config/test-config';
import compression from 'compression';
jest.mock('compression', () =>
jest.fn().mockImplementation(() => (req, res, next) => {
next();
}),
);
jest.mock(
'./routes',
() =>
class Index {
router() {
return express.Router();
}
},
);
const getApp = require('./app').default;
test('should not throw when valid config', async () => {
const config = createTestConfig();
const app = await getApp(config, {}, {});
expect(typeof app.listen).toBe('function');
});
test('should call preHook', async () => {
let called = 0;
const config = createTestConfig({
preHook: () => {
called++;
},
});
await getApp(config, {}, {});
expect(called).toBe(1);
});
test('should call preRouterHook', async () => {
let called = 0;
const config = createTestConfig({
preRouterHook: () => {
called++;
},
});
await getApp(config, {}, {});
expect(called).toBe(1);
});
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);
},
);
});