用打字稿开玩笑地模拟依赖 [英] Mock dependency in jest with typescript

查看:54
本文介绍了用打字稿开玩笑地模拟依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试在其他文件中具有依赖项的模块时.当将该模块分配为jest.Mock时,打字稿会给出一个错误,指出依赖项不存在方法mockReturnThisOnce(或任何其他jest.Mock方法),这是因为先前已键入该方法.使打字稿从jest.Mock继承类型的正确方法是什么?

When testing a module that has a dependency in a different file. When assigning that module to be jest.Mock typescript gives an error that the method mockReturnThisOnce(or any other jest.Mock method) does not exist on the dependency, this is because it is previously typed. What is the proper way to get typescript to inherit the types from jest.Mock?

这是一个简单的例子.

依赖性

const myDep = (name: string) => name;
export default myDep;

test.ts

import * as dep from '../depenendency';
jest.mock('../dependency');

it('should do what I need', () => {
  //this throws ts error
  // Property mockReturnValueOnce does not exist on type (name: string)....
  dep.default.mockReturnValueOnce('return')
}

我觉得这是一个非常常见的用例,不确定如何正确键入.任何帮助将非常感激!

I feel like this is a very common use case and not sure how to properly type this. Any help would be much appreciated!

推荐答案

您可以使用类型转换,并且您的test.ts应该如下所示:

You can use type casting and your test.ts should look like this:

import * as dep from '../dependency';
jest.mock('../dependency');

const mockedDependency = <jest.Mock<typeof dep.default>>dep.default;

it('should do what I need', () => {
  //this throws ts error
  // Property mockReturnValueOnce does not exist on type (name: string)....
  mockedDependency.mockReturnValueOnce('return');
});

TS transpiler不知道jest.mock('../dependency');会更改dep的类型,因此您必须使用类型转换.由于导入的dep不是类型定义,因此必须使用typeof dep.default来获取其类型.

TS transpiler is not aware that jest.mock('../dependency'); changes type of dep thus you have to use type casting. As imported dep is not a type definition you have to get its type with typeof dep.default.

当导入的元素是一个类时,则不必使用typeof:

When imported element is a class then you don't have to use typeof for example:

import { SomeClass } from './SomeClass';

jest.mock('./SomeClass');

const mockedClass = <jest.Mock<SomeClass>>SomeClass;

当您必须模拟某些节点本机模块时,此解决方案也很有用:

This solution is also useful when you have to mock some node native modules:

import { existsSync } from 'fs';

jest.mock('fs');

const mockedExistsSync = <jest.Mock<typeof existsSync>>existsSync;

如果您不想使用开玩笑的自动模拟功能,而更喜欢创建手动模拟功能

In case you don't want to use jest automatic mock and prefer create manual one

import TestedClass from './TestedClass';
import TestedClassDependency from './TestedClassDependency';

const testedClassDependencyMock = jest.fn<TestedClassDependency>(() => ({
  // implementation
}));

it('Should throw an error when calling playSomethingCool', () => {
  const testedClass = new TestedClass(testedClassDependencyMock());
});

testedClassDependencyMock()创建模拟对象实例 TestedClassDependency可以是类,类型或接口

testedClassDependencyMock() creates mocked object instance TestedClassDependency can be either class or type or interface

这篇关于用打字稿开玩笑地模拟依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆