Using the "this" keyword
In Jasmine, we can also initialize and share the variables between beforeEach
, it
, and afterEach
using the this
keyword. In this recipe, we will see how the this
keyword can be used within the beforeEach
or afterEach
functions.
Getting ready
You will learn this recipe with the help of the previous recipe. For more information, refer to the previous recipe, Applying setup and teardown functions to the Jasmine test. In this recipe, we refactor three scenarios for a sales and inventory control system by initializing/putting together common code in the beforeEach
and afterEach
functions.
How to do it…
To apply the this
keyword to your Jasmine test, you need to perform the following steps:
- First, you need to create a spec file (
InventoryStockOptimizeCode_With_this_spec.js
) under the/spec
folder and get the following code from the spec file (InventoryStockOptimizeCode_spec.js
) created in the previous recipe, Adding specs to your Jasmine test:describe("Inventory Stock", function() { var stockinhand_item1, item1; beforeEach(function() { stockinhand_item1=11, item1 = 1; }); afterEach(function() { stockinhand_item1=0, item1 = 0; }); //Scenario - 1 it("Inventory Stock should be updated on sale of an item", function() { expect(stockinhand_item1-item1).toEqual(10); }); it("Inventory Stock should be updated on issue of an item within organization", function() { expect(stockinhand_item1-item1).toEqual(10); }); //Scenario - 2 it("Inventory Stock should be updated on return of an item", function() { expect(stockinhand_item1+item1).toEqual(12); }); //Scenario - 3 it("Inventory Stock should be updated on receiving or procuring new item", function() { expect(stockinhand_item1+item1).toEqual(12); }); });
- Now, apply the
this
keyword by using the following code:describe("Inventory Stock", function() { beforeEach(function() { this.stockinhand_item1=11, this.item1 = 1; console.log("beforeEach: Stock in hand for item1 before spec execution = " + this.stockinhand_item1); }); afterEach(function() { this.stockinhand_item1=0, this.item1 = 0; console.log("afterEach: Stock in hand for item1 once spec executed = " + this.stockinhand_item1); }); //Scenario - 1 it("Inventory Stock should be updated on sale of an item", function() { this.transactionType = "SALE"; expect(this.stockinhand_item1-this.item1).toEqual(10); expect(this.transactionType).toBeDefined(); }); it("Inventory Stock should be updated on issue of an item within organization", function() { expect(this.stockinhand_item1-this.item1).toEqual(10); expect(this.transactionType).toBeUndefined(); }); //Scenario - 2 it("Inventory Stock should be updated on return of an item", function() { expect(this.stockinhand_item1+this.item1).toEqual(12); expect(this.transactionType).toBeUndefined(); }); //Scenario - 3 it("Inventory Stock should be updated on receiving or procuring new item", function() { expect(this.stockinhand_item1+this.item1).toEqual(12); expect(this.transactionType).toBeUndefined(); }); });
- Finally, run the spec file (
InventoryStockOptimizeCode_With_this_spec.js
) using the Jasmine runner (that is,SpecRunner.html
). You should see test execution results, as shown in the following screenshot, indicating that all four tests are passing:
How it works...
In steps 1 to 3, we looked at how the this
keyword is applied to a Jasmine test. In step 2, we removed the variable declaration from the top-level scope (that is, the describe
block) and initialized/reinitialized the variables into the beforeEach
/afterEach
functions using the this
keyword. Also, notice that this.transactionType
is true
only for the spec in which it was defined. For the other specs, the this.transactionType
variable is considered as undefined. In other words, the scope of the this.transactionType
variable is limited to the first spec only (that is, Inventory Stock
should be updated on sale of an item). Conversely, the this.item1
variable is considered defined for all the specs because it is assigned in the beforeEach
function that runs each time.