I was writing tests for a NodeJS application that used TypeORM. Express is a web server, whle TypeORM is an object relationship management tool.

If I ran the full test suite everything worked fine, but if I ran only a single test there was an initialization error telling me that TypeORM did not know how to find certain entities. After some testing I realized that tests which made an explicit call to an express endpoint worked fine, but tests which called internal methods directly failed.

If the test did not call an Express URL TypeORM was not getting initialized. I couldn't figure out why--because it looked like it should have been, but I did find a solution.

First, I needed go grab the connection:

view plain print about
1const connection = getConnection('default');

Then I needed to check if we were connected or not, and if so create the connection:

view plain print about
1if (!connection.isConnected) {
2 await createConnection(connectionConfigurationObject);

Added this check as part of each test solved the issue. It initialized TypeORM on tests were it was not already initialized, and used the existing connection if it were not already initialized.