Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are arrow functions recommended in Jasmine Test cases?

As per mocha documentation the use of arrow functions are discouraged.

https://mochajs.org/#arrow-functions

Is this the same for Jasmine? I couldn't find any pointers on the topic in Jasmine documentation.

like image 969
gordanvij Avatar asked Aug 12 '16 09:08

gordanvij


People also ask

Do Jasmine tests run in parallel?

Jasmine doesn't actually support parallel execution. It runs one spec (or before/after) function at a time.

Which one is used to create a test suite Jasmine?

Creating a test suite In terms of Jasmine, a test consists of one or more suites. A suite is declared with a describe block: describe('Suite description', () => { /* … */ });

How do you check the non desired output which function used in Jasmine?

toEqual() works exactly opposite to toEqual(). not. toEqual() is used when we need to check if the value does not match with the output of any function. We will modify the above example to show how this works.

What is beforeEach in Jasmine?

JasmineJS - beforeEach() Another notable feature of Jasmine is before and after each function. Using these two functionalities, we can execute some pieces of code before and after execution of each spec. This functionality is very useful for running the common code in the application.


1 Answers

There is a really very interesting article you should not miss:

  • Better Jasmine Tests With this

And this is a cite:

The new, better way

For every test (and their beforeEach/afterEach hooks), jasmine sets the receiver of each function to an initially empty object. This object, which is called userContext within Jasmine's source code, can have properties assigned to it, and gets blown away at the end of each test. In an attempt to address the issues we were having, we recently switched over to assigning variables to this object, rather than declaring them within describe and then assigning them. So our original code above now looked something like this:

describe('views.Card', function() {
  'use strict';

  beforeEach(function() {
    this.model = {};
    this.view = new CardView(this.model);
  });

  describe('.render', function() {
    beforeEach(function() {
      this.model.title = 'An Article';
      this.view.render();
    });

    it('creates a "cardTitle" h3 element set to the model\'s title', function() {
      expect(this.view.$el.find('.cardTitle')).toContainText(this.model.title);
    });

So, what does that all mean? Should we use arrow function with jasmine?

And the answer should be - keep arrow functions in your code, except of this combination

// could be arrow
describe("ListModel -", () =>
{
    // local context description
    interface IMyTestContext
    {
        items?: Heroe[];
        ...
    }
    // could be arrow
    describe("Test items ", () =>
    {
        // NOT AN ARROW - profit from Jasmine context passed as 'this'
        beforeEach(function()
        {
            var ctx: IMyTestContext = this.TestContext = {}; 
            // TODO do some defaults with context
            ...
        });

        // NOT AN ARROW - profit from Jasmine context passed as 'this'
        it("should ...", function()
        {
            var ctx: IMyTestContext = this.TestContext;
            // TODO ... test expecations
        ...

So, beforeEach() and it() do NOT use arrow - to profit from Jasmine context represented by this

we can also introduce a global call beforeEach

import * as something from "...";

beforeEach(function()
{
    this.TestContext = {};
});

and now context is always there for us so we do not have to re-create it:

describe("Track Changed items ", () =>
{
    // NOT AN ARROW - profit from Jasmine context passed as 'this'
    beforeEach(function()
    {                                              // created by global beforeEach above
        var ctx: IMyTestContext = this.TestContext;//  = {}; 

Yes, that is really so amazing, that if a test runner will find some global beforeEach ... it will also run it before each test... awesome, is not it?

like image 80
Radim Köhler Avatar answered Sep 18 '22 16:09

Radim Köhler