fastlane
Version:
Fastlane is a fast and flexible API framework for Node.js. It automatically creates Express routes from your project's file structure, making it easy to build APIs quickly and efficiently.
68 lines • 2.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const handler_1 = require("../handler");
// Mock fs.promises.readdir
jest.mock('fs', () => ({
promises: {
readdir: jest.fn()
}
}));
// Mock dynamic imports
jest.mock('../attach', () => {
const originalModule = jest.requireActual('../attach');
return {
...originalModule,
// We need to re-export methodHandler for our tests
methodHandler: originalModule.methodHandler,
// But we'll mock the dynamic import in attachRoutes
attachRoutes: jest.fn().mockImplementation((dir) => {
const router = (0, express_1.Router)();
// The implementation will be provided in each test
return router;
})
};
});
describe('methodHandler', () => {
let req;
let res;
let next;
beforeEach(() => {
req = {};
res = {
json: jest.fn()
};
next = jest.fn();
});
test('should handle undefined return value', async () => {
const handler = (0, handler_1.methodHandler)(() => undefined);
await handler(req, res, next);
expect(res.json).toHaveBeenCalledWith({ success: true });
});
test('should handle array return value', async () => {
const handler = (0, handler_1.methodHandler)(() => [1, 2, 3]);
await handler(req, res, next);
expect(res.json).toHaveBeenCalledWith({ data: [1, 2, 3], success: true });
});
test('should handle object return value', async () => {
const handler = (0, handler_1.methodHandler)(() => ({ user: { id: 1 } }));
await handler(req, res, next);
expect(res.json).toHaveBeenCalledWith({ user: { id: 1 }, success: true });
});
test('should handle primitive return value', async () => {
const handler = (0, handler_1.methodHandler)(() => 'hello');
await handler(req, res, next);
expect(res.json).toHaveBeenCalledWith({ data: 'hello', success: true });
});
test('should handle errors', async () => {
const error = new Error('Test error');
const handler = (0, handler_1.methodHandler)(() => {
throw error;
});
await handler(req, res, next);
expect(next).toHaveBeenCalledWith(error);
});
});
// For a more complete test suite, you would also want to test the attachRoutes function
// This would require more complex mocking of the file system and dynamic imports
//# sourceMappingURL=handler.test.js.map