[SOLVED] Jest test fials with error “DataSource is not set for this entity”


This Content is from Stack Overflow. Question asked by Nick N.

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?

  • Node: 16.15.1
  • “supertest”: “^6.2.4”,
  • “ts-node”: “^10.9.1”,
  • “typeorm”: “^0.3.7”,
  • “typescript”: “^4.7.4”
  • “node-postgres”: “^0.6.2”,
  • “form”:”^7.1.0″,
  • “pg”: “^8.7.3”,
  • “jest”: “^28.1.3”,
  • “ts-jest”: “^28.0.7”
  • “express”: “^4.18.1”,

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 () => {
            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)
            firstName: "test",
            lastName: "test",
            email: "test@test.cc",
            role: Role.USER,
            status: "active",
            password: "Pa$w0rd"


        //Temporary pass

Babel config:

module.exports = {
    presets: [
      ['@babel/preset-env', {targets: {node: 'current'}}],
    plugins: [
        ['@babel/plugin-syntax-decorators', { decoratorsBeforeExport: true }],
        ['@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true }],
        ['@babel/plugin-proposal-class-properties', { "loose": true }],


  "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:

   entities: [path.join(__dirname, '../dist/entities/**/*.entity.js')],
   migrations: [path.join(__dirname, '../dist/migrations/**/*.js')],
   synchronize: true,
   dropSchema: true,

with this:

   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.

Answered by Nick N.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 4.0.

people found this article helpful. What about you?