Monday, October 8, 2018

Jasmine with helpers

Leave a Comment

I'm new on Jasmine testing, i need to test a nodejs express application. I do not found any documentation about jasmine helpers else that are called before all tests.

Just tryning i found that adding

beforeAll(async()=>{    ... }); afterAll(async()=>{    ... }); 

into my /spec/helpers/myhelper.js these function are executed after and before all code, but i did not found documentation about this behavior into a helper. Is it a standard behavior?

Is it possible to create my helper function into myhelper.js and call this function durng test? how?

my actual /spec/helpers/myhelper.js is :

let server = require("../../app"); console.log('server started before tests....');  function testMethod(){     console.log("test helper called"); } 

How to call my test helper method from my tests?

i'm using jasmine version 3.2.1

1 Answers

Answers 1

Jasmine test cases are inside describe block.

  • Each describe block has its own beforeAll, afterAll, beforeEach, afterEach.
  • There can be describe inside another describe block.

Typically, I have one spec file which includes one describe block for one unit under test. The setup and teardown of test cases for this unit under test will be in those 4 functions of this describe.

As far as I know, if you want to separate your helper function to the new file, you can just import it normally and execute it in setup and teardown of target describe. But I have never done it since I never encounter any scenario that some classes have same setup or teardown processes.

But here's how you can achieve that:

Create server in helper function

function setupServer() {   let server = require("../../app");   console.log('server started before tests....');   console.log("test helper called");   return server; }  module.exports = { setupServer }; 

In spec file:

const { setupServer } = require('/myhelper');  describe('some unit', () => {     let server;     beforeEach(() => {         server = setupServer();     });      it('some test', () => {}); }); 

Or if you don't need return at all. It can be as short as:

beforeEach(setupServer); 

Hope this helps :)

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment