【发布时间】:2018-09-01 23:47:54
【问题描述】:
我目前正在为我的 Angular 应用程序编写一些测试。当我模拟类以检查方法是否被正确调用时,我做了一个模拟实现。当我这样做时,我的代码带有红点下划线,因为我的模拟不尊重类型的真正实现。
这是一个例子。在这里,我想要模拟 HttpLink 类以测试调用了 create 函数。我不介意 HttpLink 对象是如何构造的。所以我模拟了 HttpLink 类并模拟了创建函数。但是当我调用构造函数时,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 代码所做的类型检查,但仅限于测试文件?
【问题讨论】:
标签: unit-testing typescript visual-studio-code jestjs typechecking