Node.js Jest测试-请添加';进口;“反映元数据”';到达入口点的顶部
我正在用Node.js Jest测试-请添加';进口;“反映元数据”';到达入口点的顶部,node.js,typescript,jestjs,tsyringe,Node.js,Typescript,Jestjs,Tsyringe,我正在用tsyringe使用依赖注入开发一个应用程序。这是作为依赖项接收存储库的服务的示例: import { injectable, inject } from 'tsyringe' import IAuthorsRepository from '@domains/authors/interfaces/IAuthorsRepository' @injectable() export default class ListAuthorsService { constructor (
tsyringe
使用依赖注入开发一个应用程序。这是作为依赖项接收存储库的服务的示例:
import { injectable, inject } from 'tsyringe'
import IAuthorsRepository from '@domains/authors/interfaces/IAuthorsRepository'
@injectable()
export default class ListAuthorsService {
constructor (
@inject('AuthorsRepository')
private authorsRepository: IAuthorsRepository
) {}
和依赖项容器:
import { container } from 'tsyringe'
import IAuthorsRepository from '@domains/authors/interfaces/IAuthorsRepository'
import AuthorsRepository from '@domains/authors/infra/typeorm/repositories/AuthorsRepository'
container.registerSingleton<IAuthorsRepository>(
'AuthorsRepository',
AuthorsRepository
)
export default container
但我收到了以下错误:
tsyringe需要反射多边形填充。请添加“导入”
“反射元数据”到入口点的顶部
我的想法是——“在执行测试之前,我可能需要导入reflect元数据包”。因此,我创建了一个jest.setup.ts
,它导入反射元数据
包。但出现了另一个错误:
存储库的实例在某种程度上是未定义的
我想平静地运行我的测试。我在这里遇到了同样的问题,重构测试发现它必须首先导入依赖项,然后导入将要测试的服务类首先在项目的根目录中创建一个
jest.setup.ts
在您的jest.config.js
中,搜索以下行:
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
取消注释,并添加您的jest.setup.ts
文件路径
// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
然后再次运行测试
// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
import 'reflect-metadata';