Javascript jasmine 2 - 在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时内未调用异步回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29218981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:00:09  来源:igfitidea点击:

jasmine 2 - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

javascriptjasmine

提问by lfender6445

Having trouble with jasmine 2 and getting async specs wired up:

在 jasmine 2 和获取异步规范方面遇到问题:

define(['foo'], function(foo) {
  return describe('foo', function() {
    beforeEach(function(done) {
      window.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
      return setTimeout((function() {
        console.log('inside timeout');
        return done();
      }), window.jasmine.DEFAULT_TIMEOUT_INTERVAL);
    });
    return it('passes', function() {
      return expect({}).toBeDefined();
    });
  });
});

When I run via karma, I get back

当我通过业力奔跑时,我会回来

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

错误:超时 - 在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时内未调用异步回调。

and then the specs fail. I have attempted to override the default timeout but I can't get past the error

然后规格失败。我试图覆盖默认超时,但我无法克服错误

回答by Eitan Peer

You are using the same timeout interval as Jasmine is using to fail tests on timeout, i.e. your timeout is triggered to fire with Jasmine's default interval, which fails the test.

您使用的超时间隔与 Jasmine 用于使超时测试失败的超时间隔相同,即您的超时被触发以使用 Jasmine 的默认间隔触发,这会导致测试失败。

If you set your timeout to be less than jasmine default timeout the test passes.

如果您将超时设置为小于 jasmine 默认超时,则测试通过。

describe('foo', function () {
    beforeEach(function (done) {
        window.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
        setTimeout(function () {
            console.log('inside timeout');
            done();
        }, 500);
    });
    it('passes', function () {
        expect({}).toBeDefined();
    });
});

See fiddle here

在这里看到小提琴

回答by Tyagi Akhilesh

My 2 Cents. I also got this error mentioned in the question in another scenario.

我的 2 美分。我也在另一个场景中的问题中提到了这个错误。

I had a very simple spec like this:

我有一个非常简单的规范,如下所示:

describe('login feature', function() {
    it('should show the logged in user name after successful login', function(done) {
        expect({}).toBeDefined();
        //done(); // if you don't call this done here, then also above error comes
    });
});

See the commented out //done() function in 'it'

查看“it”中注释掉的 //done() 函数

回答by AJ Zane

Another option that might work for you is to use async

可能对您有用的另一种选择是使用 async

The async function is one of the Angular testing utilities and has to be imported... It takes a parameterless function and returns a function which becomes the true argument to the beforeEach

The body of the async argument looks much like the body of a synchronous beforeEach. There is nothing obviously asynchronous about it. For example, it doesn't return a promise and there is no done function to call as there would be in standard Jasmine asynchronous tests. Internally, async arranges for the body of the beforeEach to run in a special async test zone that hides the mechanics of asynchronous execution.

async 函数是 Angular 测试实用程序之一,必须导入......它接受一个无参数函数并返回一个函数,该函数成为 beforeEach 的真正参数

异步参数的主体看起来很像同步 beforeEach 的主体。没有什么明显的异步。例如,它不返回承诺,也没有像在标准 Jasmine 异步测试中那样调用的 done 函数。在内部,异步安排 beforeEach 的主体在一个特殊的异步测试区域中运行,该区域隐藏了异步执行的机制。

See: https://angular.io/docs/ts/latest/guide/testing.html#!#async-in-before-each

请参阅:https: //angular.io/docs/ts/latest/guide/testing.html#!#async-in-before-each

describe('Component: MyComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      providers: [
        {provide: MyService, useValue: MyServiceMock()},
      ]
    })
    // Using webpack through Angular Cli. You may need ".compileComponents()"
    fixture = TestBed.createComponent(MyComponent)
    component = fixture.componentInstance
    // Initialize the component
    component.ngOnInit()
    fixture.detectChanges()
  }))

  it('should show the logged in user name after successful login',() {
      expect({}).toBeDefined()
  })
})