unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
95 lines • 3.42 kB
JavaScript
import { createTestConfig } from '../test/config/test-config.js';
import { vi } from 'vitest';
// This mock setup MUST be at the top-level, before any other code that might trigger imports.
vi.mock('compression', () => ({
default: vi.fn().mockImplementation(() => {
// This is the actual middleware function Express would use
return (_req, _res, next) => {
next();
};
}),
}));
let getApp;
let compression;
const openApiService = {
// returns a middleware
validPath: vi.fn().mockReturnValue(() => { }),
useDocs: vi.fn(),
};
const appModule = await import('./app.js');
getApp = appModule.default;
test('should not throw when valid config', async () => {
const config = createTestConfig();
const app = await getApp(config, {}, { openApiService });
expect(typeof app.listen).toBe('function');
});
test('should call preHook', async () => {
let called = 0;
const config = createTestConfig({
preHook: () => {
called++;
},
});
await getApp(config, {}, { openApiService });
expect(called).toBe(1);
});
test('should call preRouterHook', async () => {
let called = 0;
const config = createTestConfig({
preRouterHook: () => {
called++;
},
});
await getApp(config, {}, { openApiService });
expect(called).toBe(1);
});
describe('compression middleware', () => {
beforeAll(async () => {
vi.resetModules(); // Crucial: Clears the module cache.
// Import 'compression' AFTER resetModules. This ensures we get the mock.
const compressionModule = await import('compression');
compression = compressionModule.default; // `compression` is now our vi.fn()
// Import 'app.js' AFTER resetModules and AFTER 'compression' is set to the mock.
// This ensures app.js uses the mocked version of compression.
const appModule = await import('./app.js');
getApp = appModule.default;
});
beforeEach(() => {
// Clear call history for the mock before each test in this describe block
if (compression &&
typeof compression.mockClear === 'function') {
compression.mockClear();
}
else {
// This case might happen if beforeAll failed or types are unexpected
console.warn('Compression mock was not available or not a mock function in beforeEach');
}
});
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,
},
});
await getApp(config, {}, { openApiService });
expect(compression).toHaveBeenCalledTimes(+expectCompressionEnabled);
});
});
//# sourceMappingURL=app.test.js.map