如何在单元测试中使用TypeORM全局createConnection()?

问题描述 投票:0回答:2

我有一个node.js application inside TypeORM's createConnection() body

createConnection()

现在,我想使用// index.ts import { createConnection } from "typeorm"; createConnection().then(async connection => { // express app code here }).catch(error => console.log(error)); 编写单元测试用例,并且我希望测试之间的连接可用

jest

我有一些担忧:

  • 这是我第一次使用TypeORM,所以我没有任何动手经验
  • 甚至不起作用并抛出jest
  • 它看起来很丑陋
  • 我只希望在一个位置建立连接代码,而不是在每个测试文件中,但是对于应用程序具有// abc.test.ts createConnection().then(async connection => { describe('ABC controller tests', () => { it('should test abc function1', async () => { // test body does here }); }); }).catch(error => console.log(error)); 的测试,没有入口点
  • 如何在单元测试中使用TypeORM全局创建Connection()?

我在TypeORM的createConnection()主体中有一个node.js应用程序:// index.ts import {createConnection} from“ typeorm”; createConnection()。then(异步连接=> {//表示应用代码...

javascript typescript jestjs tdd typeorm
2个回答
2
投票

您应该使用SyntaxError: Cannot use import statement outside a moduleindex.ts来设置您的状态/上下文/世界。类似于:


1
投票

您应该可以创建

© www.soinside.com 2019 - 2024. All rights reserved.