I want to test my route and I exported my app in server.test.ts file but I got this:
Cannot find module './middleware/errorHandler.js' from 'src/server.ts'
app.ts
import express from 'express';
const app = express();
// Custom Error Handler
app.use(errorHandler);
app.listen(3000, () => {});
Error handler:
import * as Sentry from '@sentry/node';
import { NextFunction, Request, Response } from 'express';
export const errorHandler = (err: Error, _req: Request, res: Response, _next: NextFunction) => {
Sentry.captureException(err);
console.log(`Errorhandler: ${err}`);
return res.status(500).json({error: 'Etwas ist schief gelaufen.'});
};
server.test.js
import request from 'supertest';
import {app} from '../src/server';
describe('hello', () => {
test('test it', async () => {
const res = await request(app)
.get('/api/photos')
expect(res.statusCode).toEqual(200)
expect(res.body).toEqual({
message: "Hello World"
})
})
})
jest.config.json
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
roots: ['<rootDir>'],
modulePaths: [
"<rootDir>",
],
moduleDirectories: [
"node_modules",
"src/",
"src"
],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};
What I am doing wrong ? I can normally start my server and it find my imported file but not in tests.
What I am doing wrong ? I use jest