How do I change the timeout on a jasmine-node async spec

后端 未结 10 1920
生来不讨喜
生来不讨喜 2020-12-04 20:53

How can I get this test to pass without resorting to runs/waitsFor blocks?

it(\"cannot change timeout\", function(done) {

     request(\"http://localhost:30         


        
相关标签:
10条回答
  • 2020-12-04 21:05

    In Angular, put this outside your describe block:

    jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
    

    This applies to all the tests in the .spec.ts file

    0 讨论(0)
  • 2020-12-04 21:11

    In my case I had multiple tests cases and while I was using the aforementioned solution with was using the:

        beforeEach(function() {
            originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
            jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
        });
    

    the DEFAULT_TIMEOUT_INTERVAL was not updated at the first test case, so I had to add this:

      beforeAll(() => {
        jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
      })
    

    to my code to successfully run all the tests.

    0 讨论(0)
  • 2020-12-04 21:14

    To set the global Jasmine-Node timeout, do this:

    jasmine.getEnv().defaultTimeoutInterval = timeoutYouWouldPrefer;// e.g. 15000 milliseconds
    

    Credit to developer Gabe Hicks for figuring out the .getEnv() part via debugging in spite of misinformation in the README doc which claims it's done by setting jasmine.DEFAULT_TIMEOUT_INTERVAL.

    If you want to set a custom timeout just for one it(), you could try passing the timeout (milliseconds) as a third argument (after the string statement and the function). There's an example of that being done here, but I'm not sure what would happen if the custom timeout was longer than Jasmine's default. I expect it would fail.

    0 讨论(0)
  • 2020-12-04 21:17

    Change j$.DEFAULT_TIMEOUT_INTERVAL to 10000 in following file: npm\node_modules\jasmine-core\lib\jasmine-core

    0 讨论(0)
  • 2020-12-04 21:20

    You can (now) set it directly in the spec, as per Jasmine docs.

    describe("long asynchronous specs", function() {
    
        var originalTimeout;
    
        beforeEach(function() {
            originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
            jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
        });
    
        it("takes a long time", function(done) {
            setTimeout(function() {
                done();
            }, 9000);
        });
    
        afterEach(function() {
            jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
        });
    });
    
    0 讨论(0)
  • 2020-12-04 21:20

    Adding: jasmine.DEFAULT_TIMEOUT_INTERVAL = yourTime; on a helper file worked for me.

    0 讨论(0)
提交回复
热议问题