Typeorm Connection "default" Was Not Found When Connection Is Created In Jest Globalsetup
Solution 1:
require('ts-node/register')
shouldn't present in .ts files. They are already processed by TypeScript compiler.
This is not what globalSetup
and globalTeardown
are for. They run in Jest parent process and are evaluated once, while each test suite runs in child processes.
This can be achieved by providing a common setup in setupFilesAfterEnv
option:
// jest.setup.ts
...
beforeAll(async () => {
await createConnections()
})
afterAll(async () => {
const defaultConnection = getConnection('default')
await defaultConnection.close()
})
Since Jest tests run in parallel, this will result in multiple database connections. If this is not desirable because of connection limit, Jest runInBand
option needs to be used.
A setup for all tests not desirable because not all test suites need database connection, while they will unconditionally take time and occupy database connection pool. In this case jest.setup.ts can be imported directly in tests that use a database instead of setupFilesAfterEnv
, no need to specify beforeAll
and afterAll
in each suite.
Post a Comment for "Typeorm Connection "default" Was Not Found When Connection Is Created In Jest Globalsetup"