@evanion/nestjs-correlation-id
Version:
Transparently forward or add correlation id to all requests
63 lines (62 loc) • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const correlation_id_middleware_1 = require("./correlation-id.middleware");
const mockCorrelationConfig = {
header: 'x-correlation-id',
generator: () => '12345',
};
const mockCorrelationService = {
getCorrelationId: jest.fn().mockImplementation(() => 'test123'),
setCorrelationId: jest.fn(),
};
describe('CorrelationIdMiddleware', () => {
let middleware;
beforeEach(() => {
middleware = new correlation_id_middleware_1.CorrelationIdMiddleware(mockCorrelationService, mockCorrelationConfig);
});
it('should be defined', () => {
expect(middleware).toBeDefined();
});
it('should set the correlation id in request object', () => {
const req = {
get: jest.fn(),
headers: {},
};
const res = {
get: jest.fn(),
set: jest.fn(),
headers: {},
};
jest.spyOn(res, 'set');
middleware.use(req, res, jest.fn());
expect(req.headers['x-correlation-id']).toBe('test123');
});
it('should set the correlation id in response object', () => {
const req = {
get: jest.fn().mockImplementation(() => 'test123'),
headers: {},
};
const res = {
get: jest.fn(),
set: jest.fn(),
headers: {},
};
jest.spyOn(res, 'set');
middleware.use(req, res, () => { });
expect(res.set).toHaveBeenCalledWith('x-correlation-id', 'test123');
});
it('should set the correlation id in correlationService', () => {
const req = {
get: jest.fn().mockImplementation(() => 'test123'),
headers: {},
};
const res = {
get: jest.fn(),
set: jest.fn(),
headers: {},
};
jest.spyOn(res, 'set');
middleware.use(req, res, jest.fn());
expect(mockCorrelationService.setCorrelationId).toHaveBeenCalledWith('test123');
});
});