简体   繁体   中英

When running jasmine tests, how can I know if I am in a describe block, beforeEach block or it block?

I need to throw an exception if a utility is used outside of an 'it' or 'beforeEach' block in my tests. Example -

   describe('some test', function(){

     useUtil();     // should throw exception

     beforeEach(function(){
        useUtil()   // should work
     })

     it('should test something', function(){
        useUtil()   // should work
     }) 
   })

The util creates spies, and I want to make sure they are created in a way that allows Jasmine to clean them after every suite.

You could create a globally accessible variable called isSpecPhase , and set it initially to false .

Then, define a global beforeEach:

beforeEach(function () {
    isSpecPhase = true;
});

Make sure to define the beforeEach before all your other suites, so that it runs before each of your specs. In your util function, you could then check if isSpecPhase === true , and throw an exception otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM