Skip to content

Commit 7d152fe

Browse files
cwjwisseclaude
andcommitted
test(cdn-logs-report): purge esmock modules in daily-export specs
The agentic-daily-export and referral-daily-export specs created esmock module mocks on every test but never purged them. Unpurged esmock registrations leak into the global module registry and cause nondeterministic cross-file pollution when the full suite runs in a single process — surfacing in CI as "log.warn is not a function" and reading ".args" of null in these specs. Track each esmocked module and esmock.purge() it in afterEach, matching the existing convention in handler.test.js, cdn-analysis, preflight, and llmo-config-utils specs. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 1644b39 commit 7d152fe

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

test/audits/cdn-logs-report/agentic-daily-export.test.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,25 @@ use(sinonChai);
2222

2323
describe('agentic daily export', () => {
2424
let sandbox;
25+
let mockedModules;
26+
27+
// Wrap esmock so every mocked module is tracked and purged after each test.
28+
// Without purging, esmock's global registrations leak across spec files and
29+
// cause nondeterministic cross-file pollution in the full suite.
30+
async function loadMocked(...args) {
31+
const module = await esmock(...args);
32+
mockedModules.push(module);
33+
return module;
34+
}
2535

2636
beforeEach(() => {
2737
sandbox = sinon.createSandbox();
38+
mockedModules = [];
2839
});
2940

30-
afterEach(() => {
41+
afterEach(async () => {
3142
sandbox.restore();
43+
await Promise.all(mockedModules.map((module) => esmock.purge(module)));
3244
});
3345

3446
function createConfiguration(queueUrl = 'https://sqs.us-east-1.amazonaws.com/123/analytics-queue') {
@@ -71,7 +83,7 @@ describe('agentic daily export', () => {
7183
}),
7284
};
7385

74-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js', {
86+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js', {
7587
'../../../src/cdn-logs-report/utils/report-utils.js': {
7688
loadSql: sandbox.stub().resolves('CREATE DATABASE IF NOT EXISTS test_db'),
7789
},
@@ -205,7 +217,7 @@ describe('agentic daily export', () => {
205217
}),
206218
};
207219

208-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js', {
220+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js', {
209221
'../../../src/cdn-logs-report/utils/report-utils.js': {
210222
loadSql: sandbox.stub().resolves('CREATE DATABASE'),
211223
},
@@ -257,7 +269,7 @@ describe('agentic daily export', () => {
257269
});
258270

259271
it('logs a warning when cleanup after failure also fails', async () => {
260-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js');
272+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js');
261273
const log = {
262274
warn: sandbox.spy(),
263275
};
@@ -282,7 +294,7 @@ describe('agentic daily export', () => {
282294
});
283295

284296
it('guards against dispatch without a queue URL', async () => {
285-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js');
297+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js');
286298

287299
await expect(module.testHelpers.dispatchAnalyticsEvent({
288300
context: {
@@ -302,7 +314,7 @@ describe('agentic daily export', () => {
302314
});
303315

304316
it('dispatches with FIFO MessageGroupId and MessageDeduplicationId', async () => {
305-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js');
317+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js');
306318
const sendMessage = sandbox.stub().resolves();
307319

308320
const result = await module.testHelpers.dispatchAnalyticsEvent({
@@ -332,7 +344,7 @@ describe('agentic daily export', () => {
332344
});
333345

334346
it('requires an importer bucket before running the daily export', async () => {
335-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js');
347+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js');
336348

337349
await expect(module.runDailyAgenticExport({
338350
athenaClient: {
@@ -376,7 +388,7 @@ describe('agentic daily export', () => {
376388
});
377389

378390
it('skips upload and dispatch when there are no traffic rows', async () => {
379-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js', {
391+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js', {
380392
'../../../src/cdn-logs-report/utils/report-utils.js': {
381393
loadSql: sandbox.stub().resolves('CREATE DATABASE'),
382394
},
@@ -449,7 +461,7 @@ describe('agentic daily export', () => {
449461
});
450462

451463
it('fails before Athena or S3 work when the analytics queue is missing', async () => {
452-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js', {
464+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js', {
453465
'../../../src/cdn-logs-report/utils/report-utils.js': {
454466
loadSql: sandbox.stub().resolves('CREATE DATABASE'),
455467
},
@@ -519,7 +531,7 @@ describe('agentic daily export', () => {
519531
});
520532

521533
it('cleans up uploaded files when analytics dispatch fails', async () => {
522-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js', {
534+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js', {
523535
'../../../src/cdn-logs-report/utils/report-utils.js': {
524536
loadSql: sandbox.stub().resolves('CREATE DATABASE'),
525537
},
@@ -610,7 +622,7 @@ describe('agentic daily export', () => {
610622
});
611623

612624
it('cleans up uploaded files when an S3 upload fails', async () => {
613-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js', {
625+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js', {
614626
'../../../src/cdn-logs-report/utils/report-utils.js': {
615627
loadSql: sandbox.stub().resolves('CREATE DATABASE'),
616628
},
@@ -704,7 +716,7 @@ describe('agentic daily export', () => {
704716
});
705717

706718
it('propagates Athena database setup failures without touching S3', async () => {
707-
const module = await esmock('../../../src/cdn-logs-report/agentic-daily-export.js', {
719+
const module = await loadMocked('../../../src/cdn-logs-report/agentic-daily-export.js', {
708720
'../../../src/cdn-logs-report/utils/report-utils.js': {
709721
loadSql: sandbox.stub().resolves('CREATE DATABASE'),
710722
},

test/audits/cdn-logs-report/referral-daily-export.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ use(sinonChai);
2424
describe('referral daily export', function referralDailyExportTests() {
2525
this.timeout(10000);
2626
let sandbox;
27+
let mockedModules;
2728

2829
beforeEach(() => {
2930
sandbox = sinon.createSandbox();
31+
mockedModules = [];
3032
});
3133

32-
afterEach(() => {
34+
afterEach(async () => {
3335
sandbox.restore();
36+
await Promise.all(mockedModules.map((module) => esmock.purge(module)));
3437
});
3538

3639
function createConfiguration(queueUrl = 'https://sqs.us-east-1.amazonaws.com/123/analytics-queue') {
@@ -68,7 +71,7 @@ describe('referral daily export', function referralDailyExportTests() {
6871
}
6972

7073
async function loadModule(classifyStub, reportUtilsOverrides = {}, queryBuilderOverrides = {}) {
71-
return esmock('../../../src/cdn-logs-report/referral-daily-export.js', {
74+
const module = await esmock('../../../src/cdn-logs-report/referral-daily-export.js', {
7275
'@adobe/spacecat-shared-rum-api-client/src/common/traffic.js': {
7376
classifyTrafficSource: classifyStub,
7477
},
@@ -83,6 +86,8 @@ describe('referral daily export', function referralDailyExportTests() {
8386
},
8487
},
8588
});
89+
mockedModules.push(module);
90+
return module;
8691
}
8792

8893
it('uploads CSV and dispatches the analytics event', async () => {

0 commit comments

Comments
 (0)