-
Hello! These are truncated examples, but I have a service: import { Injectable } from '@tsed/common'
import { CHAPTER_REPOSITORY } from '../repository/ChapterRepository'
import { Inject } from '@tsed/di'
import { Chapter } from '../entity/Chapter'
@Injectable()
export class ChapterService {
@Inject(CHAPTER_REPOSITORY)
protected repository: CHAPTER_REPOSITORY
async findChapters(bookId?: number): Promise<Chapter[]> {
return await this.repository.findChapters(bookId)
}
} That is injected into a controller: import { Controller } from '@tsed/di'
import { Get } from '@tsed/schema'
import { ChapterService } from '../service/ChapterService'
import { QueryParams } from '@tsed/common'
import { Chapter } from '../entity/Chapter'
@Controller('/chapter')
export class ChapterController {
constructor(private chapterService: ChapterService) {
}
@Get('/')
async findChapters(@QueryParams('bookId') bookId?: number): Promise<Chapter[]> {
return await this.chapterService.findChapters(bookId)
}
} I'm trying to supertest this controller: import { PlatformTest } from '@tsed/common'
import SuperTest from 'supertest'
import { Server } from '../../Server'
import { Chapter } from '../../entity/Chapter'
const data = {
id: 2,
bookId: 4,
timestamp: 1650996201,
name: 'First Day At Work',
}
const entity = new Chapter()
Object.assign(entity, data)
jest.mock('../../service/ChapterService', () => ({
findChapters: jest.fn().mockResolvedValue([ entity ])
}))
describe('ChapterController', () => {
let request: SuperTest.SuperTest<SuperTest.Test>
beforeAll(PlatformTest.bootstrap(Server))
beforeAll(async() => {
request = SuperTest(PlatformTest.callback())
})
afterAll(PlatformTest.reset)
describe('GET /rest/chapter', () => {
it('Get All Chapters', async() => {
const response = await request.get('/rest/chapter').expect(200)
expect(typeof response.body).toEqual('object')
})
})
}) This fails with: FAIL src/controller/__tests__/ChapterController.ts (5.83 s)
ChapterController
GET /rest/chapter
✕ Get All Chapters
● ChapterController › GET /rest/chapter › Get All Chapters
UNDEFINED_TOKEN_ERROR: Unable to inject dependency. Given token is undefined. Have you enabled emitDecoratorMetadata in your tsconfig.json or decorated your class with @Injectable, @Service, ... decorator ?
ChapterController->constructor(chapterService: undefined) emitDecoratorMetadata is set true in tsconfig.json How can I get this working? Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
Romakita
Apr 28, 2022
Replies: 1 comment 1 reply
-
Hello @tkroll It's because, you return a plain object javascript and not a class. So the DI doesn't resolve the correct symbol and it explain why you have this message because it's similar to not have theses compilation options. I suggest you to not use jest.mock. Here is the solution: import { PlatformTest } from '@tsed/common'
import SuperTest from 'supertest'
import { Server } from '../../Server'
import { Chapter } from '../../entity/Chapter'
const entity = new Chapter()
Object.assign(entity, {
id: 2,
bookId: 4,
timestamp: 1650996201,
name: 'First Day At Work',
})
describe('ChapterController', () => {
let request: SuperTest.SuperTest<SuperTest.Test>
beforeAll(PlatformTest.bootstrap(Server))
beforeAll(async() => {
const service = PlatformTest.get(ChapterService)
jest.spyOn(service, "findChapters").mockResolvedValue([ entity ])
request = SuperTest(PlatformTest.callback())
})
afterAll(PlatformTest.reset)
describe('GET /rest/chapter', () => {
it('Get All Chapters', async() => {
const response = await request.get('/rest/chapter').expect(200)
expect(typeof response.body).toEqual('object')
})
})
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
tkroll
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @tkroll
It's because, you return a plain object javascript and not a class. So the DI doesn't resolve the correct symbol and it explain why you have this message because it's similar to not have theses compilation options.
I suggest you to not use jest.mock.
Here is the solution: