TypeScript无法识别我开玩笑的模拟模块 [英] TypeScript doesn't recognize my jest mock module
问题描述
假设我有一个index.ts
,它将导入Database.ts
并运行一些查询.为了测试此index.ts
文件,我想模拟Database.ts
,因为我不想连接到任何真实的数据库.
Suppose I have an index.ts
that will import Database.ts
and run some queries. To test this index.ts
file, I want to mock the Database.ts
because I don't want to connect to any real database.
这是我的index.ts
:
import { connect } from './Database'
export async function runSomeQuery() {
const connection = await connect()
const result = await connection.query('SOME QUERY')
return result
}
这是数据库(__mocks__/Database.ts
)的模拟物
and here is the mock of database (__mocks__/Database.ts
)
const mockConnection = {
query: jest.fn()
}
export const connect = jest.fn(
() => Promise.resolve(mockConnection)
)
export function __getMockConnection() {
return mockConnection
}
您会看到我公开了__getMockConnection
,以便可以在测试(index.spec.ts
)中获得mockConnection
(此模式是从
you can see that I expose a __getMockConnection
so that I can get the mockConnection
in my tests (index.spec.ts
) (this pattern is learnt from the official doc):
import { connect, __getMockConnection } from '../Database'
// ^^^^ getting type error here
import { runSomeQuery } from '../index'
jest.mock('../Database')
test('runSomeQuery', async () => {
await runSomeQuery()
const mockConnection = __getMockConnection()
expect(connect).toBeCalled()
expect(mockConnection.query).toBeCalledWith('SOME QUERY')
expect(mockConnection.query).toBeCalledWith('SOME QUERY')
})
测试用例确实通过了预期的测试,但我遇到此TypeScript错误
The test case does pass as expected, but I'm getting this TypeScript error
Module '"/project/path/Database"' has no exported member '__getMockConnection'. [2305]
TypeScript不知道我要从模拟中导入Database
.也是出于这个原因,我必须禁用ts-jest的诊断程序,该程序抱怨同样的问题.
TypeScript doesn't know that I'm importing Database
from the mock. Also for this reason I have to disable the diagnostics of ts-jest which complains the same issue.
我该如何解决?将导入路径更改为'../__mocks__/Database'
不起作用.
How can I solve this? Change the import path to '../__mocks__/Database'
doesn't work.
这是仓库: https://github.com/CodinCat/ts-jest -mock-issue
使用支持TypeScript(如VS Code)的编辑器打开__tests__/index.spec.ts
,您会看到错误.
Open __tests__/index.spec.ts
with an editor that supports TypeScript like VS Code and you will see the error.
推荐答案
由于打字稿不了解jest
模拟,因此只要模拟与实际代码不同,就需要手动键入强制转换:
Since typescript is not aware of jest
mocking, you will need to manually type cast whenever your mocks are different than your actual code:
import * as MockDatabase from "../__mocks__/Database";
import * as Database from "../Database";
import { runSomeQuery } from "../index";
jest.mock("../Database");
// Type cast since Database is automatically mocked by jest
const { connect, __getMockConnection } = Database as typeof MockDatabase;
test("runSomeQuery", async () => {
await runSomeQuery();
const mockConnection = __getMockConnection();
expect(connect).toBeCalled();
expect(mockConnection.query).toBeCalledWith("SOME QUERY");
});
这篇关于TypeScript无法识别我开玩笑的模拟模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!