I've encountered a similar issue as the one discussed in #5164 and also in this thread. Here is a sample of working test code:
// AccountResolver.test.ts
describe('Account entity', () => {
it('add account', async () => {
await createConnections()
const defaultConnection = getConnection('default')
const actual = await callGraphql(
`mutation {
addAccount(options: {
accountIdentifier: "7csdcd8-8a5f-49c3-ab9a-0198d42dd253"
name: "Jake, Bob (Braine-l’Alleud) JAM"
userName: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c3e131e52311d0e1019053c1f131208130f13521f1311">[email protected]</a>"
}) {
accountIdentifier
name
userName
}
}`
)
expect(actual.data).toMatchObject({
data: {
addAccount: {
accountIdentifier: '7csdcd8-8a5f-49c3-ab9a-0198d42dd253',
name: 'Jake, Bob (Braine-l’Alleud) JAM',
userName: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f5b79a97dbb8948799908cb5969a9b819a869adb969a98">[email protected]</a>',
},
},
})
await defaultConnection.query(`DELETE FROM Account`)
await defaultConnection.close()
})
})
The code snippet for establishing a connection and closing it should be executed before and after all tests, which is why it has been included in globalSetup.ts
and globalTeardown.ts
:
// globalSetup.ts
require('ts-node/register')
import { createConnections } from 'typeorm'
module.exports = async () => {
await createConnections()
}
// globalTeardown.ts
require('ts-node/register')
import { getConnection } from 'typeorm'
module.exports = async () => {
const defaultConnection = getConnection('default')
await defaultConnection.close()
}
// AccountResolver.test.ts
describe('Account entity', () => {
it('add account', async () => {
const defaultConnection = getConnection('default')
await defaultConnection.query(`DELETE FROM Account`)
const actual = await callGraphql(
`mutation {
addAccount(options: {
accountIdentifier: "7csdcd8-8a5f-49c3-ab9a-0198d42dd253"
name: "Jake, Bob (Braine-l’Alleud) JAM"
userName: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20624f420e6d41524c455960434f4e544f532e454a4b">[email protected]</a>"
}) {
accountIdentifier
name
userName
}
}`
)
expect(actual.data).toMatchObject({
data: {
addAccount: {
accountIdentifier: '7csdcd8-8a5f-49c3-ab9a-0198d42dd253',
name: 'Jake, Bob (Braine-l’Alleud) JAM',
userName: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41032e236f0c20332d243801222e2f352e32650c232c2d">[email protected]</a>',
},
},
})
})
})
If the line require('ts-node/register')
is omitted from both files, it results in the following error:
T:\Test\src\it-portal\entity\Account.ts:1 import { ^^^^^^ SyntaxError: Cannot use import statement outside a module
However, including the require
line throws:
FAIL src/resolvers/AccountResolver.test.ts × add account (31 ms) ● Account entity › add account ConnectionNotFoundError: Connection "default" was not found.Account entity
Version
"jest": "^26.0.1",
"ts-jest": "^26.1.0",
"ts-node-dev": "^1.0.0-pre.44",
"typescript": "^3.9.5"
Config
// jest.config.js
module.exports = {
preset: 'ts-jest',
globalSetup: './src/test-utils/config/globalSetup.ts',
globalTeardown: './src/test-utils/config/globalTeardown.ts',
setupFiles: ['./src/test-utils/config/setupFiles.ts'],
moduleDirectories: ['node_modules', 'src'],
globals: {
'ts-jest': {
tsConfig: 'tsconfig.json',
diagnostics: {
warnOnly: true,
},
},
},
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},
coverageReporters: ['json', 'lcov', 'text', 'clover'],
}
Thank you for highlighting my errors. I tried searching for a solution as a newbie, but couldn't determine whether it's my lack of understanding or a tool bug. Came across a similar issue here with a relevant PR.
It seems like the tests run in a completely isolated environment where they cannot access the connection set up in globalSetup
.
Workaround
The only workaround I've discovered so far is to include the following code in each test file:
beforeAll(async () => {
await createConnections()
})
afterAll(async () => {
const defaultConnection = getConnection('default')
await defaultConnection.close()
})