Forcing tests

As your test suite starts to grow, it becomes necessary at development time to limit the run of an entire test suite down to one specific test, or to one specific suite of tests. This is generally to find the cause of a specific failure, or to focus on a single suite of tests during the development of the code. Jasmine provides the fdescribe and fit functions to force the execution of tests as follows:

fdescribe("This is a forced suite", () => { 
    it("This is not a forced test", () => { 
        expect(true).toBeFalsy('true should be false'); 
    }); 
    fit("This is a forced test", () => { 
        expect(false).toBeFalsy(); 
    }) 
}); 

Here, we have replaced the describe function with an fdescribe function. Or, more simply, we have inserted an f before the describe. This will cause Jasmine to force this test to run, at the expense of any other tests. In other words, Jasmine will not run any other test suite except this one. This is very handy during development time, as we can limit the test run to a specific suite as we are developing the tests.

Note, too, how we have replaced the it function with a fit function in the second test in this suite. By inserting an f before the it function, we can force only a specific test to run. In this example, the first test will fail, as it is expecting true toBeFalsy. The second test, however, will pass. If we were to run our test suite at this stage, we would find one spec has run, with zero failures.

The fdescribe and fit functions work from the most restrictive to the least restrictive. In other words, if no tests have been marked with fit, then all tests in an fdescribe suite will be run. If multiple tests in an fdescribe suite have been marked with fit, then only those marked with fit will be run. If a suite has not been marked with fdescribe, but a single test has been marked with fit, then only that test will run.

Note that, under no circumstances, should you check in tests marked as fdescribe or fit. This will mean that your build servers will only run a tiny portion of your tests, instead of the entire suite. Jasmine will, in fact, warn you if it finds any tests that were forced, and log this to the console, as can be seen in the following screenshot:

Here, we can see that 1 spec out of 10 specs were run, and that Jasmine has marked the test run as Incomplete.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.1.239