gordanvij gordanvij - 3 months ago 11
TypeScript Question

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.

Answer

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

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?

Comments