打字稿错误 TS(2304) jest.MockedFunction - jest.MockedFunction 的正确用法是什么?
Typescript Error TS(2304) jest.MockedFunction - What Is The Correct Usage For jest.MockedFunction?
任何人都可以帮助解决我在下面列出的以下代码中收到的 Typescript 错误。
引发错误的代码行是const mockedFunction = createRepo as jest.MockedFunction<typeof create_publish_repository>;
我正在努力学习如何使用 jest.MockedFunction。我在 VSCode 中收到的错误是:
- 找不到名称 createRepo。 ts(2304) 任何
import { PublishRepository, create_publish_repository } from "../../../../repository";
jest.mock("../../../../repository");
describe("/api/publish/[id]", () => {
test("returns a post with published state set to true", async () => {
// this raises an error
const mockedFunction = createRepo as jest.MockedFunction<typeof create_publish_repository>;
}
存储库 ./publish.repo
import { Post } from "@prisma/client"
import { Context } from "../context"
export class PublishRepository {
private context: Context
constructor(context: Context) {
this.context = context
}
async set_published(post_id: string | string[]): Promise<Post> {
return await this.context.prisma.post.update({
where: { id: Number(post_id) },
data: { published: true },
});
}
}
export const create_publish_repository = (context: Context): PublishRepository => {
return new PublishRepository(context)
}
存储库模块 ./index.ts
export { create_publish_repository, PublishRepository } from "./publish.repo";
解决了...我需要重命名我的模拟函数变量以匹配用于模拟的导入函数的名称。
由于没有导入名为 createRepo 的函数,打字稿会引发错误。
没有调用 createRepo 的导入,引发错误
import { PublishRepository, create_publish_repository } from "../../../../repository";
const mockedFunction = createRepo as jest.MockedFunction<typeof create_publish_repository>;
更正了模拟函数名称 create_publish_repository 以匹配导入
import { PublishRepository, create_publish_repository } from "../../../../repository";
const mockedFunction = create_publish_repository as jest.MockedFunction<typeof create_publish_repository>;
任何人都可以帮助解决我在下面列出的以下代码中收到的 Typescript 错误。
引发错误的代码行是const mockedFunction = createRepo as jest.MockedFunction<typeof create_publish_repository>;
我正在努力学习如何使用 jest.MockedFunction。我在 VSCode 中收到的错误是:
- 找不到名称 createRepo。 ts(2304) 任何
import { PublishRepository, create_publish_repository } from "../../../../repository";
jest.mock("../../../../repository");
describe("/api/publish/[id]", () => {
test("returns a post with published state set to true", async () => {
// this raises an error
const mockedFunction = createRepo as jest.MockedFunction<typeof create_publish_repository>;
}
存储库 ./publish.repo
import { Post } from "@prisma/client"
import { Context } from "../context"
export class PublishRepository {
private context: Context
constructor(context: Context) {
this.context = context
}
async set_published(post_id: string | string[]): Promise<Post> {
return await this.context.prisma.post.update({
where: { id: Number(post_id) },
data: { published: true },
});
}
}
export const create_publish_repository = (context: Context): PublishRepository => {
return new PublishRepository(context)
}
存储库模块 ./index.ts
export { create_publish_repository, PublishRepository } from "./publish.repo";
解决了...我需要重命名我的模拟函数变量以匹配用于模拟的导入函数的名称。
由于没有导入名为 createRepo 的函数,打字稿会引发错误。
没有调用 createRepo 的导入,引发错误
import { PublishRepository, create_publish_repository } from "../../../../repository";
const mockedFunction = createRepo as jest.MockedFunction<typeof create_publish_repository>;
更正了模拟函数名称 create_publish_repository 以匹配导入
import { PublishRepository, create_publish_repository } from "../../../../repository";
const mockedFunction = create_publish_repository as jest.MockedFunction<typeof create_publish_repository>;