茉莉花语境+打字稿 [英] Jasmine context + Typescript

查看:51
本文介绍了茉莉花语境+打字稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Jasmine与Typescript一起使用,最近我们开始在beforeEachit中使用this上下文.

I'm using Jasmine with Typescript and recently we started using the this context in beforeEach and it.

示例:

beforeEach(() => {
  this.fixture = TestBed.createComponent(blablabla);
});

it('should do something', () => {
   expect(this.fixture).toBeTruthy();
});

问题在于TypeScript不够聪明,无法确定beforeEach中的thisit中的this完全相同. 有人知道这个事实的简单提示提示"吗?

The problem is that TypeScript is not smart enough to figure out that this inside beforeEach is exactly the same this as in it. Does anyone know an easy way to 'hint' typescript about this fact?

这有可能吗?

推荐答案

您可以 typehint this 在函数中.实际上,我想,如果仅使用箭头功能(在describebeforeEachit中),则上下文this将是最外部的全局上下文.由于无法注释该注释,因此建议将常规的老式函数传递给最外面的describe:

You can typehint this in functions. Actually, if you only use arrow functions (in describe, beforeEach and it), the context this will be the outermost global context, I suppose. Since that one cannot be annotated, I suggest passing a regular old-style function to the outermost describe:

// Dummy-type Jasmine functions (only for this MWE)
declare const describe: (msg: string, fun: () => void) => void;
declare const it: (msg: string, fun: () => void) => void;
declare const beforeEach: (fun: () => void) => void;

class A {
    aProperty: string;
}

interface TestSuiteContext {
  myObj: A;
}

describe('Test suite', function (this: TestSuiteContext) {
    beforeEach(() => {
        this.myObj = new A();
    });

    it('should do something', () => {
        const message: string = this.myObj.aProperty;
    });
});

这篇关于茉莉花语境+打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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