samedi 14 novembre 2020

Mocking getCustomRepository() to pass 'test' as connection for Jest tests

I am writing some Jest test's for an expressJS application which is using typescript and typeORM. The tests have been setup to run off a 'test' DB schema. There is some setup prior to running the tests which sets why the DB connection and tables etc etc.

I've run into an issue when testing my routes, specifically around how getCustomRepository() gets called during the test run. getCustomRepository takes two parameters, the repository you are looking for and also an optional connectionName. For my test to run on the test DB the connectionName for all calls to getCustomRepository() needs to be 'test'. E.g:

getCustomRepository(repository, "test")

Does anyone know a good way of handling this? I was looking at possibly using jest.spyOn(typeorm, 'getCustomRepository').mockImplementation(() => <something>); to override the call to getCustomRepository so that it always passes in connectionName but I'm not sure how to capture the original parameters (if that's even possible) using Jest.

Aucun commentaire:

Enregistrer un commentaire