There is a very interesting article that you should not miss:
And this is a quote:
For each test (and them before each / after each hook), the jasmine sets the receiver of each function for an initially empty object. This object, called userContext in the Jasmine source code, can have properties assigned to it and are reset at the end of each test. In an attempt to solve the problems that we had, we recently switched to assigning variables to this object, rather than declaring them in the description, and then assigning them. So, our original code above 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\ title', function() { expect(this.view.$el.find('.cardTitle')).toContainText(this.model.title); });
So what does this mean? Should we use the jasmine arrow function?
And the answer should be - save the arrow functions in your code, except for 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 the arrow - to profit from the Jasmine context represented by this
we can also introduce a global call beforeEach
import * as something from "..."; beforeEach(function() { this.TestContext = {}; });
and now context always exists for us, so we donโt need to recreate 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, it really is so amazing that if the test runner finds some global beforeEach ... it will also run it before each test ... amazing, isn't it?