Skip to content

Commit

Permalink
Multiple modules: attempt to reduce test flakiness, improved logging …
Browse files Browse the repository at this point in the history
…for XHR mock race conditions (#8466)

* Datablocks bid adapter: do not send metrics during tests

* Log contents of XHR mock on test failure

* Disable ajax for analytics adapters during tests

* Do not assume that test setup did not fail
  • Loading branch information
dgirardi authored May 25, 2022
1 parent 80430aa commit 8aaf01b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/AnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { ajax } from './ajax.js';
import { logMessage, _each } from './utils.js';
import * as events from './events.js'

export const _internal = {
ajax
};

const {
EVENTS: {
AUCTION_INIT,
Expand Down Expand Up @@ -61,7 +65,7 @@ export default function AnalyticsAdapter({ url, analyticsType, global, handler }
}

function _callEndpoint({ eventType, args, callback }) {
ajax(url, callback, JSON.stringify({ eventType, args }));
_internal.ajax(url, callback, JSON.stringify({ eventType, args }));
}

function _enqueue({ eventType, args }) {
Expand Down
13 changes: 13 additions & 0 deletions test/mocks/analyticsStub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {_internal} from '../../src/AnalyticsAdapter.js';

before(() => {
// stub out analytics networking to avoid random events polluting the global xhr mock
disableAjaxForAnalytics();
})

export function disableAjaxForAnalytics() {
sinon.stub(_internal, 'ajax').callsFake(() => null);
}
export function enableAjaxForAnalytics() {
_internal.ajax.restore();
}
22 changes: 22 additions & 0 deletions test/mocks/xhr.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {getUniqueIdentifierStr} from '../../src/utils.js';

export let server = sinon.createFakeServer();
export let xhr = global.XMLHttpRequest;
Expand All @@ -7,3 +8,24 @@ beforeEach(function() {
server = sinon.createFakeServer();
xhr = global.XMLHttpRequest;
});

const bid = getUniqueIdentifierStr().substring(4);
let fid = 0;

/* eslint-disable */
afterEach(function () {
if (this?.currentTest?.state === 'failed') {
const prepend = (() => {
const preamble = `[Failure ${bid}-${fid++}]`;
return (s) => s.split('\n').map(s => `${preamble} ${s}`).join('\n');
})();


console.log(prepend(`XHR mock state after failure (for test '${this.currentTest.fullTitle()}'): ${server.requests.length} requests`))
server.requests.forEach((req, i) => {
console.log(prepend(`Request #${i}:`));
console.log(prepend(JSON.stringify(req, null, 2)));
})
}
});
/* eslint-enable */
4 changes: 4 additions & 0 deletions test/spec/AnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai';
import * as events from 'src/events.js';
import CONSTANTS from 'src/constants.json';
import { server } from 'test/mocks/xhr.js';
import {disableAjaxForAnalytics, enableAjaxForAnalytics} from '../mocks/analyticsStub.js';

const REQUEST_BIDS = CONSTANTS.EVENTS.REQUEST_BIDS;
const BID_REQUESTED = CONSTANTS.EVENTS.BID_REQUESTED;
Expand All @@ -25,6 +26,9 @@ FEATURE: Analytics Adapters API
AND an \`example\` instance of \`AnalyticsAdapter\`\n`, () => {
let adapter;

before(enableAjaxForAnalytics);
after(disableAjaxForAnalytics);

beforeEach(function () {
adapter = new AnalyticsAdapter(config);
});
Expand Down
17 changes: 9 additions & 8 deletions test/spec/modules/datablocksBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,15 @@ let bid_request = {
}

describe('DatablocksAdapter', function() {
before(() => {
// stub out queue metric to avoid it polluting the global xhr mock during other tests
sinon.stub(spec, 'queue_metric').callsFake(() => null);
});

after(() => {
spec.queue_metric.restore();
});

describe('All needed functions are available', function() {
it(`isBidRequestValid is present and type function`, function () {
expect(spec.isBidRequestValid).to.exist.and.to.be.a('function')
Expand Down Expand Up @@ -377,14 +386,6 @@ describe('DatablocksAdapter', function() {
});
})

describe('queue / send metrics', function() {
it('Should return true', function() {
expect(spec.queue_metric({type: 'test'})).to.be.true;
expect(spec.queue_metric('string')).to.be.false;
expect(spec.send_metrics()).to.be.true;
});
})

describe('get_viewability', function() {
it('Should return undefined', function() {
expect(spec.get_viewability()).to.equal(undefined);
Expand Down
1 change: 1 addition & 0 deletions test/test_deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ window.process = {
require('test/helpers/prebidGlobal.js');
require('test/mocks/adloaderStub.js');
require('test/mocks/xhr.js');
require('test/mocks/analyticsStub.js');

0 comments on commit 8aaf01b

Please sign in to comment.