Skip to content

Improve TypeORM Testing Documentation #246

Open
@jelling

Description

@jelling

I'm submitting a...

[X] Documentation issue or request

Current behavior

When testing classes that use TypeORM using Jest, there is inconsistent behavior if the TypeORM database connection is established in a Jest setupFile, like:

createConnection()

This worked for most for all of our entities except for one where it gave the dreaded "no metadata found" TypeORM error. The same entity worked fine when not running inside a TestModule so the entity definition wasn't the issue. It's possible there was a race condition but all of the other entities worked consistently.

Eventually, I figured out that the proper way to use TypeORM inside of a Jest Spec is like this:

import { TypeOrmModule } from "@nestjs/typeorm";
...

describe('Example Service', async function() {

  beforeAll(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [TypeOrmModule.forRoot()],
      providers: [...services],
    }).compile();

  });

...

I.e. always inject the Nest.Js TypeORM module into your testing module.

Expected behavior

Update the testing section of these docs to show an example using a real database as well as the mocked example.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions