@unito/integration-sdk
Version:
Integration SDK
33 lines (28 loc) • 1.02 kB
text/typescript
import express from 'express';
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import notFound from '../../src/middlewares/notFound.js';
describe('notFound middleware', () => {
it('respond with error without calling next', () => {
let actualStatus: number | undefined;
let actualJson: Record<string, unknown> | undefined;
const response = {
headersSent: false,
status: (code: number) => {
actualStatus = code;
return {
json: (json: Record<string, unknown>) => {
actualJson = json;
},
};
},
locals: { logger: { error: () => undefined } },
} as unknown as express.Response;
notFound({ path: 'unknownPath' } as express.Request, response, () => {
throw new Error('next should not be called');
});
assert.strictEqual(actualStatus, 404);
assert.strictEqual(actualJson?.code, '404');
assert.deepEqual(actualJson?.message, 'Path unknownPath not found.');
});
});