【问题标题】:Simple Typescript Jasmine Test throwing 'expect' error简单的 Typescript Jasmine 测试抛出“预期”错误
【发布时间】:2016-11-08 00:32:55
【问题描述】:

我有以下简单的茉莉花测试...

//test.spec.ts
describe('Sample', function(){
   it('Should do something', () => expect(true).toBe(true));
});

但是当我跑步时,我得到...

Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out

这很好用……

describe('Sample', function(){
   it('Should do something', function(){
      expect(true).toBe(true);
   });
});

【问题讨论】:

    标签: typescript jasmine karma-jasmine


    【解决方案1】:

    检查这个playground

    如果有这两条语句

    describe('Sample', function(){
        it('Should do something',
            () => expect(true).toBe(true));
    });
    
    describe('Sample', function(){
        it('Should do something', () => {
            expect(true).toBe(true));  
       } 
    });
    

    它们导致不同的 JS 代码

    describe('Sample', function () {
        it('Should do something', function () { return expect(true).toBe(true); });
    });
    describe('Sample', function () {
        it('Should do something', function () {
            expect(true).toBe(true);
        });
    });
    

    一个没有包装{}的简单语句被转译成return语句,我们这里不需要

    【讨论】:

      【解决方案2】:

      我很确定您得到它的原因是箭头函数,它对待范围的方式与常规匿名函数不同。

      当你这样做时:

      it('Should do something', function() {
          expect(true).toBe(true);
      });
      

      函数以 this 的规范执行,但是当你使用箭头函数时:

      it('Should do something', () => {
          expect(true).toBe(true);
      });
      

      this 不同。

      容易检查,试试吧:

      it('Should do something', function() {
          console.log("this is: ", this);
          expect(true).toBe(true);
      });
      

      还有:

      it('Should do something', () => {
          console.log("this is: ", this);
          expect(true).toBe(true);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-07
        • 2021-04-02
        • 1970-01-01
        相关资源
        最近更新 更多