【发布时间】:2020-06-20 01:22:15
【问题描述】:
我遇到了与#5164 和this question 类似的问题。考虑以下工作测试代码:
// 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: "Bob.Marley@contoso.com"
}) {
accountIdentifier
name
userName
}
}`
)
expect(actual.data).toMatchObject({
data: {
addAccount: {
accountIdentifier: '7csdcd8-8a5f-49c3-ab9a-0198d42dd253',
name: 'Jake, Bob (Braine-l’Alleud) JAM',
userName: 'Bob.Marley@contoso.com',
},
},
})
await defaultConnection.query(`DELETE FROM Account`)
await defaultConnection.close()
})
})
创建连接和关闭连接的代码应该在所有测试之前和所有测试完成之后执行,这就是我们将它添加到globalSetup.ts和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: "Bob.Marley@contoso.com"
}) {
accountIdentifier
name
userName
}
}`
)
expect(actual.data).toMatchObject({
data: {
addAccount: {
accountIdentifier: '7csdcd8-8a5f-49c3-ab9a-0198d42dd253',
name: 'Jake, Bob (Braine-l’Alleud) JAM',
userName: 'Bob.Marley@contoso.com',
},
},
})
})
})
从两个文件中省略require('ts-node/register') 行会引发此错误:
T:\Test\src\it-portal\entity\Account.ts:1 进口 { ^^^^^^ SyntaxError: 不能在模块外使用 import 语句
在 throws 中保留 require 行:
FAIL src/resolvers/AccountResolver.test.ts × 添加账户 (31 ms) ● 账户实体 › 添加账户 ConnectionNotFoundError: Connection 未找到“默认”。帐户实体
版本
"jest": "^26.0.1",
"ts-jest": "^26.1.0",
"ts-node-dev": "^1.0.0-pre.44",
"typescript": "^3.9.5"
配置
// 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 与PR 类似的问题。
似乎测试在完全隔离的环境中运行,他们无法访问在globalSetup 中设置的连接。
解决方法
到目前为止,我发现的唯一解决方法是将以下代码添加到每个测试文件中:
beforeAll(async () => {
await createConnections()
})
afterAll(async () => {
const defaultConnection = getConnection('default')
await defaultConnection.close()
})
【问题讨论】:
标签: javascript typescript jestjs typeorm ts-jest