我遇到了类似的问题#5164 https://github.com/facebook/jest/issues/5164 and 这个问题 https://stackoverflow.com/questions/61576307/global-typeorm-connection-with-jest。考虑以下工作测试代码:
// 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: "[email protected] /cdn-cgi/l/email-protection"
}) {
accountIdentifier
name
userName
}
}`
)
expect(actual.data).toMatchObject({
data: {
addAccount: {
accountIdentifier: '7csdcd8-8a5f-49c3-ab9a-0198d42dd253',
name: 'Jake, Bob (Braine-l’Alleud) JAM',
userName: '[email protected] /cdn-cgi/l/email-protection',
},
},
})
await defaultConnection.query(`DELETE FROM Account`)
await defaultConnection.close()
})
})
创建连接和关闭连接的代码应该在所有测试之前和所有测试完成之后执行,这就是我们将其添加到globalSetup.ts
and globalTeardown.ts
:
// globalSetup.ts
require('ts-node/register')
import { createConnections } from 'typeorm'
module.exports = async () => {
// console.log('jest setup')
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: "[email protected] /cdn-cgi/l/email-protection"
}) {
accountIdentifier
name
userName
}
}`
)
expect(actual.data).toMatchObject({
data: {
addAccount: {
accountIdentifier: '7csdcd8-8a5f-49c3-ab9a-0198d42dd253',
name: 'Jake, Bob (Braine-l’Alleud) JAM',
userName: '[email protected] /cdn-cgi/l/email-protection',
},
},
})
})
})
省略该行require('ts-node/register')
两个文件都会抛出此错误:
T:\Test\src\it-portal\entity\Account.ts:1
进口 {
^^^^^^
语法错误:无法在模块外部使用 import 语句
保持require
抛出行:
失败 src/resolvers/AccountResolver.test.ts × 添加帐户(31 毫秒) ●
帐户实体 › 添加帐户 ConnectionNotFoundError: Connection
未找到“default”。帐户实体
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'],
}
谢谢你指出我的错误。由于我是新人,我尝试谷歌搜索,但如果这是我不理解该工具或其中的错误,则无法真正找到答案。发现类似问题here https://github.com/kulshekhar/ts-jest/issues/1391 with a PR https://github.com/facebook/jest/pull/8751.
看起来测试是在完全隔离的环境中运行的,他们无法访问内部设置的连接globalSetup
.
解决方法
到目前为止,我发现的唯一解决方法是将以下代码添加到每个测试文件中:
beforeAll(async () => {
await createConnections()
})
afterAll(async () => {
const defaultConnection = getConnection('default')
await defaultConnection.close()
})