Note
Segment has paused maintenance on this project, but may return it to an active status in the future. Issues and pull requests from external contributors are not being considered, although internal contributions may appear from time to time. The project remains available under its open source license for anyone to use.
An plugin to easily test an Analytics.js integration.
$ npm install @segment/analytics.js-integration-tester
Imagine you had an integration that looked like this:
module.exports = createIntegration('Custom')
.readyOnInitialize()
.global('_custom')
.option('apiKey', '')
.option('track', false);
You can easily assert that all of those properties are there with a familiar API, like so:
var Analytics = require('@segment/analytics.js-core').constructor;
var tester = require('@segment/analytics.js-integration-tester');
var plugin = require('./integration');
var Custom = plugin.Integration;
var analytics = new Analytics();
var custom = new Custom;
analytics.use(tester);
analytics.add(custom);
var Test = createIntegration('Custom')
.readyOnInitialize()
.global('_custom')
.option('apiKey', '')
.option('track', false);
analytics.validate(Custom, Test);
You can also use the tracking and spying methods to quickly test cases:
analytics.stub(window, '_custom');
analytics.track('Event', { property: true });
analytics.called(window._custom, 'Event', { property: true });
The general pattern we use is to stub
the methods in a beforeEach
test block, then call an analytics
method (track, page, identify, etc.), and then check if some other internal method got called with the correct arguments using analytics.called(method, args...)
.
Assert that an integration is correctly defined, that it matches the testIntegration
's options, globals, configuration, and name.
Spy on a method
of a host object
.
Stub a method
of a host object
. Stubs are different than spies in that they won't pass the arguments through to the original method.
Assert that a spy
was called, optionally with args...
.
Assert that a spy
was not called, optional with args...
.
Assert that a spy
was called and returned value
.
Assert that a spy
was called and did not return value
.
Call initialize
on the integration.
Reset the tester. This will call reset
on the integration, as well as restore all existing spies.
Assert that the integration load
method can load the library, and that loaded
properly checks for the libraries existence, then callback(err)
.
Call one of the core analytics methods on the integration with args...
. The methods include: alias
, identify
, group
, page
, and track
.
Assert that an actual
value is truthy.
Assert that an actual
value is equal to an expected
value.
Assert that an actual
value is not equal to an expected
value.
Assert that an actual
value is deeply equal to an expected
value.
Assert that an actual
value is not deeply equal to an expected
value.
Assert that an actual
value is strict (===
) equal to an expected
value.
Assert that an actual
value is not strict (===
) equal to an expected
value.
Assert that a fn
throws an error.
Assert than a fn
does not throw an error.
Waits for all script elements to finish loading before calling callback
. Often scripts loaded onto the page will add other scripts in order to support their functionality. These scripts can reference globals that we clear after each test, causing them to fail if they load too late.
afterEach(function(done) {
analytics.waitForScripts(function() {
myIntegration.reset();
analytics.reset();
done();
});
});