Eug*_*gen 10 sequelize.js typescript reactjs jestjs
我确实有一个有两个重载方法的类。
public static create<M extends Model>(
this: ModelStatic<M>,
values?: M['_creationAttributes'],
options?: CreateOptions<M['_attributes']>
): Promise<M>;
public static create<M extends Model>(
this: ModelStatic<M>,
values: M['_creationAttributes'],
options: CreateOptions<M['_attributes']> & { returning: false }
): Promise<void>;
Run Code Online (Sandbox Code Playgroud)
在我的单元测试中,我试图用来jest.spyOn模拟第一个方法,但是 jest 只看到返回的方法Promise<void>。
const mockInsightCreate = jest.spyOn(Insight, "create");
mockInsightCreate.mockReturnValue(Promise.resolve()); // here I need to return an object of type - Insight
Run Code Online (Sandbox Code Playgroud)
有没有办法指示spyOn拾取第一个返回的方法Promise<M>?
import {
Model,
} from "sequelize-typescript";
...
export default class Insight extends Model<Insight> {
Run Code Online (Sandbox Code Playgroud)
Ant*_*rdi 11
I know this question is old, but bellow is my answer for anyone facing the same issue who might stumble across it.
There is currently no way of instructing which overload signature spyOn should use. One solution would be to cast the value to whatever spyOn expects.
mySpy.mockReturnValue(Promise.resolve(myObj) as unknown as void);
mySpy.mockResolveValue(myObj as unknown as void);
Run Code Online (Sandbox Code Playgroud)
However, I personally prefer avoiding coercing the typing system if possible. Another workaround that does not require any typing judo is to mock the entire method implementation.
mySpy.mockImplementation(() => Promise.resolve(myObj));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2446 次 |
| 最近记录: |