Skip to content
Advertisement

Conditional test case in jasmine

I am writing Jasmine. I want that when the response from the site is ok (site uploaded- pending 200).

Run the it’s (test cases) in spec and when the site is failed to load the site the it’s (test cases) will not run.

I check the response from site in before all function.

And now in each it that make different thing I check the condition if the response (saved in global var) is true

How can I do it in global fun like before each?

let response;

describe('', ()=>{

beforeAll (async () => {

//this function return the statusCode of http request
response= await sendQuery('Http://...');
})

beforeEach(async(), =>{
});


it('', async()=> {
if (response = 200){
//do somsing 1...
}
it('', async()=> {
if (response = 200){
//do somsing 2...
}
it('', async()=> {
if (response = 200){
//do somsing 3...
}
v
it('', async()=> {
if (response = 200){
//do somsing 4...
}
it('', async()=> {
if (response = 200){
//do somsing 5...
}
it('', async()=> {
if (response = 200){
//do somsing 6...
}

Advertisement

Answer

something like this in your config

async onPrepare() {

global.response = await sendQuery('Http://...');
    if (global.response !== 200) {
      throw new Error(`Status is ${response}`); // may not be needed actually, but I'll leave it
      await browser.close();
      await process.exit(1);
    }
}

global.response will be available in your specs

Advertisement