简体   繁体   中英

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

I'm using: NestJS and I have a class exported using namespace. 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'

Without @Injectable the test runs without problems, but I need Injectable.

Class with Injectable

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

Removing @Injectable the test works without problem, but I need to use it.

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.

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.

Configuration with @swc/jest not working Archive: jest.config.ts

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

Configuration with @SW/jest that work Archive: jest.config.ts

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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