From e779ee0ad23fcad3978673482841dff2e04c6bd1 Mon Sep 17 00:00:00 2001 From: Alexander Marks Date: Tue, 12 Jun 2018 13:26:22 -0700 Subject: [PATCH] Make esm-amd-loader dynamic require 404 test not flaky. Previously this test used a 1 second timeout to wait to see how many times the error callback was called, which was flaky on CI. Now we capture 404 errors and check callback calls after two 404s. Fixes #507 --- packages/esm-amd-loader/test/src/suite.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/esm-amd-loader/test/src/suite.ts b/packages/esm-amd-loader/test/src/suite.ts index 9c46ce4e3..1aa60615c 100644 --- a/packages/esm-amd-loader/test/src/suite.ts +++ b/packages/esm-amd-loader/test/src/suite.ts @@ -268,19 +268,25 @@ suite('dynamic require', () => { }); test('calls error callback only once on multiple 404s', (done) => { - let numErrors = 0; + let num404s = 0; + let numCallbackCalls = 0; + + window.addEventListener('error', on404, true); + + function on404() { + if (++num404s === 2) { + window.removeEventListener('error', on404); + assert.equal(numCallbackCalls, 1); + done(); + } + } define(['require'], (require: any) => { require( ['./not-found-a.js', './not-found-b.js'], () => assert.fail(), - () => numErrors++); + () => numCallbackCalls++); }); - - setTimeout(() => { - assert.equal(numErrors, 1); - done(); - }, 1000); }); });