Jest全局配置中TypeORM默认连接无法找到的问题求助
问题:Jest全局Setup中创建的TypeORM连接无法在测试用例中访问
我遇到了和#5164及相关提问类似的问题。原本在测试用例内创建/关闭TypeORM连接的代码可以正常运行,但我想把连接的创建和关闭移到Jest的globalSetup.ts和globalTeardown.ts中,实现所有测试共用一个连接,结果却遇到了连接找不到的错误。
原本可正常运行的测试代码
// 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() }) })
迁移到全局Setup/Teardown后的代码
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 import { ^^^^^^ SyntaxError: Cannot use import statement outside a module - 保留
require('ts-node/register')时:FAIL src/resolvers/AccountResolver.test.ts × add account (31 ms) ● Account entity › add account ConnectionNotFoundError: Connection "default" was not found.
环境版本
jest: ^26.0.1ts-jest: ^26.1.0ts-node-dev: ^1.0.0-pre.44typescript: ^3.9.5
Jest配置
// 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'], }
目前的临时解决方案
我现在只能在每个测试文件中添加beforeAll和afterAll来处理连接:
beforeAll(async () => { await createConnections() }) afterAll(async () => { const defaultConnection = getConnection('default') await defaultConnection.close() })
我是个新手,搜索后没找到明确答案,不确定是对工具理解有误还是存在Bug。根据找到的类似问题和PR推测,测试环境可能是完全隔离的,无法访问globalSetup中创建的连接,希望能得到解决思路。
内容的提问来源于stack exchange,提问作者DarkLite1
相关产品推荐
相关产品推荐

