I am building a nodejs | express API with typeorm and everything works normally when calling the routes via postman or some client. The issue appears in tests made with jest and I got the following error: "DataSource is not set for this entity". Am i missing something?
I am using a Postgres database for the tests.
jest config:
module.exports = async () => {
return {
preset: "ts-jest",
testEnvironment: "node",
verbose: true,
moduleNameMapper: {
"@exmpl/(.*)": "<rootDir>/src/$1"
},
};
}
DataSource file:
import { DataSource } from "typeorm"
import path from "path"
export const AppDataSource = new DataSource({
name: "default",
migrationsTableName: 'migrations',
type: "postgres",
host: process.env.POSTGRES_HOST,
port: Number(process.env.POSTGRES_PORT),
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
synchronize: false,
logging: false,
entities: [path.join(__dirname, '../entities/**/*.entity.js')],
migrations: [path.join(__dirname, '../migrations/**/*.js')],
subscribers: [],
cache: true
})
Test file:
import path from "path";
import request from "supertest"
import { AppDataSource } from "../src/db/DataSource"
import { Role } from "../src/entities/User/Role.enum"
import app from "../src/App"
let connection: any;
describe('Auth', () => {
beforeAll(async () => {
AppDataSource.setOptions({
entities: [path.join(__dirname, '../dist/entities/**/*.entity.js')],
migrations: [path.join(__dirname, '../dist/migrations/**/*.js')],
synchronize: true,
dropSchema: true,
})
connection = await AppDataSource.initialize()
await connection.synchronize(true)
})
afterAll(async () => {
await connection.destroy()
})
test('should signUp', async () => {
const response = await request(app)
.post('/auth/register')
.send({
firstName: "test",
lastName: "test",
email: "[email protected]",
role: Role.USER,
status: "active",
password: "Pa$$w0rd"
})
console.log(response.body)
//Temporary pass
expect(2+2).toBe(4)
})
})
Babel config:
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
plugins: [
['@babel/plugin-syntax-decorators', { decoratorsBeforeExport: true }],
['@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true }],
['@babel/plugin-proposal-class-properties', { "loose": true }],
]
};
tsconfig:
{
"compilerOptions": {
/* Language and Environment */
"target": "es2016",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
/* Modules */
"module": "commonjs",
"moduleResolution": "node",
"rootDirs": ["src/", "config/"],
"typeRoots": ["./node_modules/@types", "src/typings"],
/* JavaScript Support */
/* Emit */
"declaration": true,
"sourceMap": true,
"outDir": "dist",
"removeComments": true,
/* Interop Constraints */
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
/* Type Checking */
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
/* Completeness */
"skipLibCheck": true
},
"include": ["src/**/*"]
}
After a little research, I find out that the issue was the wrong path to the entities folder. It should not be the path to the compiled files but to the typescript source files.
I post this answer in case someone has the same issue.
I replaced this:
AppDataSource.setOptions({
entities: [path.join(__dirname, '../dist/entities/**/*.entity.js')],
migrations: [path.join(__dirname, '../dist/migrations/**/*.js')],
synchronize: true,
dropSchema: true,
})
with this:
AppDataSource.setOptions({
entities: entities: [path.join(__dirname, '../../src/entities/**/*.entity.ts')],
migrations: [path.join(__dirname, '../../src/migrations/**/*.ts')],
synchronize: true,
dropSchema: true,
})
I m not sure why this is like this so if someone knows to explain it would be great.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With