How to prevent deadlock error when testing DataMapper in NestJS?

Viewed 314

I recently started implementing DataMapper in my NestJS project. Running the app is fine, but when I run all the tests I run into a QueryFailedError: deadlock detected error on some test cases. I think I may be setting up my tests in an improper way but this is the only way I've found to work.

This is the template I'm using for my tests (I have about a dozen or so of 'em):

import {createConnection, Connection} from 'typeorm'

import {createOrmConfig} from '../../../ormconfig'

import {AllergyRepository} from './allergy.repository'

describe('AllergyRepository', () => {
  let repo: AllergyRepository
  let conn: Connection

  beforeAll(async () => {
    conn = await createConnection(createOrmConfig('test', false))
    repo = conn.getCustomRepository(AllergyRepository)
  })

  afterAll(async () => {
    conn.close()
  })

  it('should be defined', async () => {
    expect(conn).toBeDefined()
    expect(repo).toBeDefined()
  })
})

And this is how I'm implementing the Repository.

import {EntityRepository, Repository} from 'typeorm'

import {Allergy} from './allergy.entity'

@EntityRepository(Allergy)
export class AllergyRepository extends Repository<Allergy> {

  async getById(id: number): Promise<Allergy[]> {
    const result: Allergy[] = await this.query(
      `
        SELECT *
        FROM public.allergy
        WHERE id = $1
      `,
      [id],
    )

    return result
    }
    
}

What is a more efficient way to write integration tests for the Data Mapper approach for TypeORM in NestJS?

0 Answers
Related