简体   繁体   English

开玩笑错误:预期为“{”,得到“命名空间”...使用命名空间和@Injectable

[英]Jest Error: Expected '{', got 'namespace' ... Using namespace and @Injectable

I'm using: NestJS and I have a class exported using namespace.我正在使用:NestJS,我有一个使用命名空间导出的 class。 Right after the export namespace I have the NestJS @Injectable decorator and I get the following error when I try to run the test: × Expected '{', got 'namespace'在导出命名空间之后,我有 NestJS @Injectable装饰器,当我尝试运行测试时出现以下错误: × Expected '{', got 'namespace'

Without @Injectable the test runs without problems, but I need Injectable.没有@Injectable ,测试运行没有问题,但我需要Injectable。

Class with Injectable Class 可注射

export namespace SearchCase {
    @Injectable()
    export class SearchCase {

        constructor(private casesRepository: InterfaceRepository<Case.Case>) { }

        async execute(request: RequestSearchCase): Promise<ResponseSearchCase> {
            const searchResult = await this.casesRepository.search(request.search);

            return {
                searchResult,
            };
        }
    }
}

Test测试

describe('Search Case', () => {
    it('should be able return a case with substring', async () => {
        const casesRepository = new InMemoryCaseRepository();

        const searchCase = new SearchCase.SearchCase(casesRepository);

        const createCase = new Case.Case({
            utente: 'utente test',
            caseOrigin: 'case origin test',
            reportingDate: new Date(),
            reporterName: 'Emanuela Xavier',
            disease: 'disease test',
        })

        await casesRepository.create(createCase);
        
        const response = await searchCase.execute({
            search: 'Ema'
        });

        expect(response.searchResult.length).toBe(1);
        expect(response.searchResult[0].reporterName).toContain('Ema');
    });
});

ERROR Error shown when I run the test ERROR运行测试时显示的错误

Removing @Injectable the test works without problem, but I need to use it.删除 @Injectable 测试工作没有问题,但我需要使用它。

I was using SWC/Jest instead of ts-jest, when I switched to ts-jest in my jest.config.ts the tests came back working even though I was using @Injectable.我使用的是 SWC/Jest 而不是 ts-jest,当我在我的 jest.config.ts 中切换到 ts-jest 时,即使我使用的是 @Injectable,测试也会恢复正常。

I still don't understand why with @swc/jest it's failing, but for now it's working, when I have time I'll research more to find out the error.我仍然不明白为什么@swc/jest它会失败,但现在它正在工作,当我有时间时我会研究更多以找出错误。

Configuration with @swc/jest not working Archive: jest.config.ts @swc/jest 的配置不起作用存档:jest.config.ts

"transform": { 
    "^.+\\.(t|j)s$": ["@swc/jest"]
}

Configuration with @SW/jest that work Archive: jest.config.ts使用 @SW/jest 的配置有效存档:jest.config.ts

"transform": { 
    "^.+\\.(t|j)s$": "ts-jest"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM