单元测试打字稿类型

Ian*_*son 5 jasmine typescript typescript-typings angular

我的模块之一中有以下导出:

export class Action1 implements Action {}

export class Action2 implements Action {}

export type ActionsUnion =
  | Action1
  | Action2;
Run Code Online (Sandbox Code Playgroud)

我正在尝试找出最好的测试方法,ActionsUnion以确保它是我定义的类型。例如:

it('should have the correct types', () => {
  expect(typeof Action1).toEqual(ActionsUnion);
  expect(typeof Action2).toEqual(ActionsUnion);
});
Run Code Online (Sandbox Code Playgroud)

当然,上面的方法不起作用,因为我用作ActionsUnion变量。关于如何实现上述目标有什么想法吗?

对于上下文,我使用 Angular、ngrx 和 jasmine。

dmc*_*dle -3

我还没有尝试过联合,但你可能想尝试 jasmine.any(Type)。在这种情况下,上面的代码将是:

it('should have the correct types', () => {
  expect(Action1).toEqual(jasmine.any(ActionsUnion));
  expect(Action2).toEqual(jasmine.any(ActionsUnion));
});
Run Code Online (Sandbox Code Playgroud)

详细信息请参见: https://jasmine.github.io/2.0/introduction.html#section-Matching_Anything_with_%3Ccode%3Ejasmine.any%3C/code%3E