Skip to content

Commit

Permalink
Multiple modules: clean up unit tests (#11630)
Browse files Browse the repository at this point in the history
* Test chunking

* update some bidder eid tests

* split eid tests into each userId submodule

* cleanup userId_spec

* add TEST_PAT config

* fix idx, lmp

* clean up userId_spec

* fix double run, invibes, intentIq

* small fixes

* undo package-lock changes

* update colors, remove empty test

* 8pod analytics: clean up interval handler
  • Loading branch information
dgirardi committed Jun 3, 2024
1 parent 1e820bf commit fca4691
Show file tree
Hide file tree
Showing 47 changed files with 1,397 additions and 2,210 deletions.
31 changes: 10 additions & 21 deletions karma.conf.maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,12 @@ function setBrowsers(karmaConf, browserstack) {
module.exports = function(codeCoverage, browserstack, watchMode, file, disableFeatures) {
var webpackConfig = newWebpackConfig(codeCoverage, disableFeatures);
var plugins = newPluginsArray(browserstack);

var files = file ? ['test/test_deps.js', file, 'test/helpers/hookSetup.js'].flatMap(f => f) : ['test/test_index.js'];
// This file opens the /debug.html tab automatically.
// It has no real value unless you're running --watch, and intend to do some debugging in the browser.
if (watchMode) {
files.push('test/helpers/karma-init.js');
if (file) {
file = Array.isArray(file) ? ['test/pipeline_setup.js', ...file] : [file]
}

var files = file ? ['test/test_deps.js', ...file, 'test/helpers/hookSetup.js'].flatMap(f => f) : ['test/test_index.js'];

var config = {
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',
Expand All @@ -127,15 +125,15 @@ module.exports = function(codeCoverage, browserstack, watchMode, file, disableFe
},
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['es5-shim', 'mocha', 'chai', 'sinon'],
frameworks: ['es5-shim', 'mocha', 'chai', 'sinon', 'webpack'],

files: files,
// test files should not be watched or they'll run twice after an update
// (they are still, in fact, watched through autoWatch: true)
files: files.map(fn => ({pattern: fn, watched: false, served: true, included: true})),

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/test_index.js': ['webpack', 'sourcemap']
},
preprocessors: Object.fromEntries(files.map(f => [f, ['webpack', 'sourcemap']])),

// web server port
port: 9876,
Expand All @@ -148,7 +146,7 @@ module.exports = function(codeCoverage, browserstack, watchMode, file, disableFe
logLevel: karmaConstants.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
autoWatch: watchMode,

reporters: ['mocha'],

Expand All @@ -175,15 +173,6 @@ module.exports = function(codeCoverage, browserstack, watchMode, file, disableFe
plugins: plugins
};

// To ensure that, we are able to run single spec file
// here we are adding preprocessors, when file is passed
if (file) {
config.files.forEach((file) => {
config.preprocessors[file] = ['webpack', 'sourcemap'];
});
delete config.preprocessors['test/test_index.js'];
}

setReporters(config, codeCoverage, browserstack);
setBrowsers(config, browserstack);
return config;
Expand Down
92 changes: 83 additions & 9 deletions karmaRunner.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,97 @@
const karma = require('karma');
const process = require('process');
const karmaConfMaker = require('./karma.conf.maker.js');
const glob = require('glob');
/**
* Environment variables:
*
* TEST_CHUNKS: number of chunks to split tests into, or MAX to run each test suite in isolation
* TEST_CHUNK: run only this chunk (e.g. TEST_CHUNKS=4 TEST_CHUNK=2 gulp test) will run only the second quarter
* TEST_ALL: set to continue running remaining chunks after a previous chunk failed
* TEST_PAT: test file pattern (default is *_spec.js)
*/

process.on('message', function(options) {
try {
let cfg = karmaConfMaker(options.coverage, options.browserstack, options.watch, options.file, options.disableFeatures);
process.on('message', function (options) {
function info(msg) {
// eslint-disable-next-line no-console
console.log('\x1b[46m\x1b[30m%s\x1b[0m', msg);
}

function error(msg) {
// eslint-disable-next-line no-console
console.log('\x1b[41m\x1b[37m%s\x1b[0m', msg);
}

function chunkDesc(chunk) {
return chunk.length > 1 ? `From ${chunk[0]} to ${chunk[chunk.length - 1]}` : chunk[0];
}

const failures = [];

function quit(fail) {
// eslint-disable-next-line no-console
console.log('');
failures.forEach(([chunkNo, chunkTot, chunk]) => {
error(`Chunk ${chunkNo + 1} of ${chunkTot} failed: ${chunkDesc(chunk)}`);
fail = true;
});
process.exit(fail ? 1 : 0);
}

process.on('SIGINT', () => quit());

function runKarma(file) {
let cfg = karmaConfMaker(options.coverage, options.browserstack, options.watch, file, options.disableFeatures);
if (options.browsers && options.browsers.length) {
cfg.browsers = options.browsers;
}
if (options.oneBrowser) {
cfg.browsers = [cfg.browsers.find((b) => b.toLowerCase().includes(options.oneBrowser.toLowerCase())) || cfg.browsers[0]]
cfg.browsers = [cfg.browsers.find((b) => b.toLowerCase().includes(options.oneBrowser.toLowerCase())) || cfg.browsers[0]];
}
cfg = karma.config.parseConfig(null, cfg);
new karma.Server(cfg, (exitCode) => {
process.exit(exitCode);
}).start();
return new Promise((resolve, reject) => {
new karma.Server(cfg, (exitCode) => {
exitCode ? reject(exitCode) : resolve(exitCode);
}).start();
});
}

try {
let chunks = [];
if (options.file) {
chunks.push([options.file]);
} else {
const chunkNum = process.env['TEST_CHUNKS'] ?? 1;
const pat = process.env['TEST_PAT'] ?? '*_spec.js'
const tests = glob.sync('test/**/' + pat).sort();
const chunkLen = chunkNum === 'MAX' ? 0 : Math.floor(tests.length / Number(chunkNum));
chunks.push([]);
tests.forEach((fn) => {
chunks[chunks.length - 1].push(fn);
if (chunks[chunks.length - 1].length > chunkLen) chunks.push([]);
});
chunks = chunks.filter(chunk => chunk.length > 0);
if (chunks.length > 1) {
info(`Splitting tests into ${chunkNum} chunks, ${chunkLen + 1} suites each`);
}
}
let pm = Promise.resolve();
chunks.forEach((chunk, i) => {
if (process.env['TEST_CHUNK'] && Number(process.env['TEST_CHUNK']) !== i + 1) return;
pm = pm.then(() => {
info(`Starting chunk ${i + 1} of ${chunks.length}: ${chunkDesc(chunk)}`);
return runKarma(chunk);
}).catch(() => {
failures.push([i, chunks.length, chunk]);
if (!process.env['TEST_ALL']) quit();
}).finally(() => {
info(`Chunk ${i + 1} of ${chunks.length}: done`);
});
});
pm.then(() => quit());
} catch (e) {
// eslint-disable-next-line
console.error(e);
process.exit(1);
error(e);
quit(true);
}
});
2 changes: 1 addition & 1 deletion modules/ceeIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const ceeIdSubmodule = {
* performs action to obtain id and return a value
* @function
* @returns {(IdResponse|undefined)}
*/
*/
getId() {
const ceeIdToken = readId();

Expand Down
15 changes: 14 additions & 1 deletion modules/eightPodAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {getStorageManager} from '../src/storageManager.js';
const analyticsType = 'endpoint';
const MODULE_NAME = `eightPod`;
const MODULE = `${MODULE_NAME}AnalyticProvider`;
let interval;

/**
* Custom tracking server that gets internal events from EightPod's ad unit
Expand Down Expand Up @@ -64,7 +65,9 @@ let eightPodAnalytics = Object.assign(adapter({ analyticsType }), {
});

// Send queue of event every 10 seconds
setInterval(sendEvents, 10_000);
if (!interval) {
interval = setInterval(sendEvents, 10_000);
}
},
resetQueue() {
queue = [];
Expand Down Expand Up @@ -175,6 +178,16 @@ eightPodAnalytics.enableAnalytics = function (config) {
logInfo(MODULE, 'init', config);
};

eightPodAnalytics.disableAnalytics = (function (orig) {
return function (...args) {
if (interval) {
clearInterval(interval);
interval = null;
}
return orig.apply(this, args);
};
})(eightPodAnalytics.disableAnalytics);

/**
* Register Analytics Adapter
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/invibesBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const CONSTANTS = {
DISABLE_USER_SYNC: true
};

const storage = getStorageManager({bidderCode: CONSTANTS.BIDDER_CODE});
export const storage = getStorageManager({bidderCode: CONSTANTS.BIDDER_CODE});

export const spec = {
code: CONSTANTS.BIDDER_CODE,
Expand Down
2 changes: 1 addition & 1 deletion modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,6 @@ function updateSubmodules() {
const submoduleConfig = find(configs, j => j.name && (j.name.toLowerCase() === i.name.toLowerCase() ||
(i.aliasName && j.name.toLowerCase() === i.aliasName.toLowerCase())));
if (submoduleConfig && i.name !== submoduleConfig.name) submoduleConfig.name = i.name;
i.findRootDomain = findRootDomain;
return submoduleConfig ? {
submodule: i,
config: submoduleConfig,
Expand Down Expand Up @@ -1117,6 +1116,7 @@ export function requestDataDeletion(next, ...args) {
* @param {Submodule} submodule
*/
export function attachIdSystem(submodule) {
submodule.findRootDomain = findRootDomain;
if (!find(submoduleRegistry, i => i.name === submodule.name)) {
submoduleRegistry.push(submodule);
GDPR_GVLIDS.register(MODULE_TYPE_UID, submodule.name, submodule.gvlid)
Expand Down
5 changes: 5 additions & 0 deletions test/helpers/cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function clearAllCookies() {
document.cookie.split(';').forEach(function (c) {
document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/');
});
}
6 changes: 0 additions & 6 deletions test/helpers/karma-init.js

This file was deleted.

21 changes: 21 additions & 0 deletions test/pipeline_setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[it, describe].forEach((ob) => {
ob.only = function () {
[
'describe.only and it.only are disabled unless you provide a single spec --file,',
'because they can silently break the pipeline tests',
// eslint-disable-next-line no-console
].forEach(l => console.error(l))
throw new Error('do not use .only()')
};
});

[it, describe].forEach((ob) => {
ob.skip = function () {
[
'describe.skip and it.skip are disabled,',
'because they pollute the pipeline test output',
// eslint-disable-next-line no-console
].forEach(l => console.error(l))
throw new Error('do not use .skip()')
};
});
24 changes: 24 additions & 0 deletions test/spec/modules/33acrossIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import * as utils from 'src/utils.js';

import { server } from 'test/mocks/xhr.js';
import { uspDataHandler, coppaDataHandler, gppDataHandler } from 'src/adapterManager.js';
import {createEidsArray} from '../../../modules/userId/eids.js';
import {expect} from 'chai/index.mjs';
import {attachIdSystem} from '../../../modules/userId/index.js';

describe('33acrossIdSystem', () => {
describe('name', () => {
Expand Down Expand Up @@ -817,4 +820,25 @@ describe('33acrossIdSystem', () => {
});
});
});
describe('eid', () => {
before(() => {
attachIdSystem(thirthyThreeAcrossIdSubmodule);
})
it('33acrossId', function() {
const userId = {
'33acrossId': {
envelope: 'some-random-id-value'
}
};
const newEids = createEidsArray(userId);
expect(newEids.length).to.equal(1);
expect(newEids[0]).to.deep.equal({
source: '33across.com',
uids: [{
id: 'some-random-id-value',
atype: 1
}]
});
});
})
});
13 changes: 5 additions & 8 deletions test/spec/modules/adfBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,14 @@ describe('Adf adapter', function () {
let validBidRequests = [{
bidId: 'bidId',
params: {},
userIdAsEids: createEidsArray({
tdid: 'TTD_ID_FROM_USER_ID_MODULE',
pubcid: 'pubCommonId_FROM_USER_ID_MODULE'
})
userIdAsEids: [
{ source: 'adserver.org', uids: [ { id: 'TTD_ID_FROM_USER_ID_MODULE', atype: 1, ext: { rtiPartner: 'TDID' } } ] },
{ source: 'pubcid.org', uids: [ { id: 'pubCommonId_FROM_USER_ID_MODULE', atype: 1 } ] }
]
}];

let request = JSON.parse(spec.buildRequests(validBidRequests, { refererInfo: { page: 'page' } }).data);
assert.deepEqual(request.user.ext.eids, [
{ source: 'adserver.org', uids: [ { id: 'TTD_ID_FROM_USER_ID_MODULE', atype: 1, ext: { rtiPartner: 'TDID' } } ] },
{ source: 'pubcid.org', uids: [ { id: 'pubCommonId_FROM_USER_ID_MODULE', atype: 1 } ] }
]);
assert.deepEqual(request.user.ext.eids, validBidRequests[0].userIdAsEids);
});

it('should send currency if defined', function () {
Expand Down
22 changes: 22 additions & 0 deletions test/spec/modules/adqueryIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {adqueryIdSubmodule, storage} from 'modules/adqueryIdSystem.js';
import {server} from 'test/mocks/xhr.js';
import sinon from 'sinon';
import {attachIdSystem} from '../../../modules/userId/index.js';
import {createEidsArray} from '../../../modules/userId/eids.js';
import {expect} from 'chai/index.mjs';

const config = {
storage: {
Expand Down Expand Up @@ -58,4 +61,23 @@ describe('AdqueryIdSystem', function () {
expect(callbackSpy.lastCall.lastArg).to.deep.equal('testqid');
});
});
describe('eid', () => {
before(() => {
attachIdSystem(adqueryIdSubmodule);
});
it('qid', function() {
const userId = {
qid: 'some-random-id-value'
};
const newEids = createEidsArray(userId);
expect(newEids.length).to.equal(1);
expect(newEids[0]).to.deep.equal({
source: 'adquery.io',
uids: [{
id: 'some-random-id-value',
atype: 1
}]
});
});
})
});
Loading

0 comments on commit fca4691

Please sign in to comment.