我目前正在为我的angular应用程序编写一些测试。当我模拟类以检查方法是否被正确调用时,我创建了一个模拟实现。当我这样做的时候,我的代码会用红色的圆点加下划线,因为我的mock没有考虑类型的真正实现。
下面是一个例子。这里我想模拟HttpLink类,以便测试调用了create函数。我不关心HttpLink对象是如何构造的。所以我模拟了HttpLink类,模拟了create函数。但visual studio代码在我调用构造函数时会放红点,因为它不尊重真正的HttpLink实现:
import { Apollo } from 'apollo-angular'
jest.mock('apollo-angular', () => {
return {
Apollo: () => {
return {
create: jest.fn()
}
}
}
})
import { HttpLink } from 'apollo-angular-link-http'
jest.mock('apollo-angular-link-http', () => {
return {
HttpLink: () => {
return {
create: jest.fn()
}
}
}
})
import { GraphqlService } from './graphql.service'
describe('GraphqlService', () => {
let apollo = new Apollo()
let httpLink = new HttpLink() // <====== This is red because type checking see this class need one argument but I mocked the constructor.
let graphqlService
beforeAll(() => {
graphqlService = new GraphqlService(apollo, httpLink)
})
it('should be created', () => {
expect(graphqlService).toBeTruthy()
})
it('should create apollo client correctly', () => {
expect(apollo.create).toHaveBeenCalled()
})
})是否有一种方法可以像visual studio代码那样取消激活类型检查,但只针对测试文件?
发布于 2018-03-23 18:24:55
您可以转到首选项,然后选择设置
在用户设置中,您必须更新以下内容:
"typescript.validate.enable": true,至
"typescript.validate.enable": false,希望它能帮助你!!
https://stackoverflow.com/questions/49447307
复制相似问题