@@ -22,13 +22,25 @@ use(sinonChai);
2222
2323describe ( '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 } ,
0 commit comments