Extension Developers: tip for unit testing with turbine

Avatar

Avatar
Publish 1
MVP
yuhuisg
MVP

Likes

200 likes

Total Posts

606 posts

Correct reply

121 solutions
Top badges earned
Publish 1
Affirm 100
Springboard
Bedrock
Validate 1
View profile

Avatar
Publish 1
MVP
yuhuisg
MVP

Likes

200 likes

Total Posts

606 posts

Correct reply

121 solutions
Top badges earned
Publish 1
Affirm 100
Springboard
Bedrock
Validate 1
View profile
yuhuisg
MVP

26-01-2021

This tip is for extension developers (most likely applicable for web extensions only, since I haven't tried this with mobile extensions).

TL;DR create a "global.turbine" variable to mock the turbine free variable, before running your unit tests.

I use jasmine (https://jasmine.github.io/index.html) for testing my extension. One difficulty that I had was figuring out how to test the "turbine" free variable (https://experienceleague.adobe.com/docs/launch/using/extension-dev/turbine.html).

After numerous trials-and-errors, I finally stumbled on this solution:

describe("description of the test suite", function() {
  // create the turbine free variable with "logger"
  global.turbine = {
    logger: jasmine.createSpyObj('', ['debug', 'info', 'warn', 'alert', 'error']),
  };

  // rest of test suite

  it("description of unit test that logs to the logger", function() {
    // run the test
    expect(global.turbine.logger.info).toHaveBeenCalled();
  });
});

Hope this works for you too!