DEV Community

Cover image for Unit testing NestJS with Typeorm in memory
Julien Prugne for Webeleon

Posted on

17 2

Unit testing NestJS with Typeorm in memory

Assuming you already configured TypeORM in your NestJS project.
If you need to setup TypeORM, consider reading the awesome NestJS documentation.

For the sake of testing something, I'll setup a simple API that will serve spaceships! You can access the sample repository on github.

Install SQLITE

npm i -D better-sqlite3
Enter fullscreen mode Exit fullscreen mode

A few helpers

Ok, we can write this for every test files BUT we are lazy developers and will not spend our time re-writing the same code over and over and over...

Let's write a simple helper function that will provide an easy to import pre-configured TypeORM module!
src/test-utils/TypeORMSQLITETestingModule.ts

import { TypeOrmModule } from '@nestjs/typeorm';
import { Spaceship } from '../spaceships/spaceship.entity';

export const TypeOrmSQLITETestingModule = () => [
  TypeOrmModule.forRoot({
    type: 'better-sqlite3',
    database: ':memory:',
    dropSchema: true,
    entities: [Spaceship],
    synchronize: true,
  }),
  TypeOrmModule.forFeature([Spaceship]),
];
Enter fullscreen mode Exit fullscreen mode

This one is not mandatory but a simple test dataset is always a nice to have. src/test-utils/testDataset.seed.ts

import { getConnection } from 'typeorm';
import { Spaceship } from '../spaceships/spaceship.entity';

export const testDatasetSeed = async () => {
  const connection = await getConnection();
  const entityManager = connection.createEntityManager();

  entityManager.insert<Spaceship>(Spaceship, {
    name: 'moa',
    type: 'cruiser',
    origin: 'caldari',
  });
  entityManager.insert<Spaceship>(Spaceship, {
    name: 'caracal',
    type: 'cruiser',
    origin: 'caldari',
  });
  entityManager.insert<Spaceship>(Spaceship, {
    name: 'rokh',
    type: 'battleship',
    origin: 'caldari',
  });
};
Enter fullscreen mode Exit fullscreen mode

Using the helpers in a test file

In order to allow tests to use the im memory database, you'll just need to call the function ...TypeOrmSQLITETestingModule() and spread it since it provide the TypeOrmModule.forRoot and TypeOrmModule.forFeature.

Finally, seed the test data (or not) await testDatasetSeed();.

import { Test, TestingModule } from '@nestjs/testing';
import { SpaceshipsService } from './spaceships.service';
import { TypeOrmSQLITETestingModule } from '../test-utils/TypeOrmSQLITETestingModule';
import { testDatasetSeed } from '../test-utils/testDataset.seed';

describe('SpaceshipsService', () => {
  let service: SpaceshipsService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [...TypeOrmSQLITETestingModule()],
      providers: [SpaceshipsService],
    }).compile();

    service = module.get<SpaceshipsService>(SpaceshipsService);
    await testDatasetSeed();
  });

  it('listSpaceships', async () => {
    const spaceships = await service.listSpaceships();
    expect(spaceships).toHaveLength(3);
  });
});
Enter fullscreen mode Exit fullscreen mode

Questions?

https://media.giphy.com/media/Ss0aKjyh6UgHhnv0yG/giphy.gif

I'll be glad to answers questions in the comments.

If you liked my discord consider joining my coding lair!
☎️Webeleon coding lair on discord

You can also email me and offer me a contract πŸ’°
βœ‰οΈEmail me!

And since I'm a nice guy, here, take this sample repo containing a working codebase!
🎁Get the code of the tuto from github

Neon image

Serverless Postgres in 300ms (❗️)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free β†’

Top comments (7)

Collapse
 
dariansampare profile image
Darian Sampare β€’

Pretty nice trick for testing individual service queries without mocking! Any clue if this approach works for Postgres?

Collapse
 
wh1t3h47 profile image
AntΓ΄nio Martos Harres β€’

You can try pg-mem and it can also by used with typeORM
github.com/oguimbal/pg-mem/wiki/Li...

Collapse
 
bassochette profile image
Julien Prugne β€’

Looks like what I am looking for to enhance my tests.
I'll give it a try in the evening.
Thanks mate

Collapse
 
bassochette profile image
Julien Prugne β€’

From my researches you can't run postgres in memory.
This solution, is not optimal even with mysql more a trick to simplify a bit testing and ci setup.

Imo, you should setup a test db and reset it between tests. You'd be able to test all those neat postgres features. It's more complicated and add some ci setup but should be worth it.

Collapse
 
mangelov profile image
Mikhail Angelov β€’

I made a simple lib, wich can help with it github.com/mikhail-angelov/nestjs-...

Collapse
 
polopopeye profile image
Kenneth Suarez β€’

If we have our entity with created and updated fields, how can we solved using SQLite, as is not suported or there is any kind of workaround?

Collapse
 
eugene0928 profile image
Umidjon Tukhtaev β€’

Can you tell how to unit test typeorm repositories in nestjs

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay