From 0aacff501d5ae7898a2b4ec7322c80712a6a8b0e Mon Sep 17 00:00:00 2001 From: johnjbarton Date: Mon, 14 Dec 2020 17:19:30 -0800 Subject: [PATCH] fix(middleware): throw script tag errors Include failing path. This is important for module tags which otherwise simply give 404 at the server. Add e2e test to verify the message. Fixes #3572 --- lib/middleware/karma.js | 2 +- test/e2e/error.feature | 16 ++++++++++++++++ .../error/import-something-from-somewhere.js | 2 ++ test/unit/middleware/karma.spec.js | 18 ++++++++++++------ 4 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 test/e2e/support/error/import-something-from-somewhere.js diff --git a/lib/middleware/karma.js b/lib/middleware/karma.js index c48332d12..e9a95ae9d 100644 --- a/lib/middleware/karma.js +++ b/lib/middleware/karma.js @@ -193,7 +193,7 @@ function createKarmaMiddleware ( } else { const scriptType = (SCRIPT_TYPE[fileType] || 'text/javascript') const crossOriginAttribute = includeCrossOriginAttribute ? 'crossorigin="anonymous"' : '' - scriptTags.push(``) + scriptTags.push(``) } } diff --git a/test/e2e/error.feature b/test/e2e/error.feature index 0b327110e..7f67227a8 100644 --- a/test/e2e/error.feature +++ b/test/e2e/error.feature @@ -40,3 +40,19 @@ Feature: Error Display """ SyntaxError: Unexpected token '}' """ + + Scenario: Missing module Error in a test file + Given a configuration with: + """ + files = [{pattern: 'error/import-something-from-somewhere.js', type: 'module'}]; + browsers = ['ChromeHeadlessNoSandbox']; + plugins = [ + 'karma-jasmine', + 'karma-chrome-launcher' + ]; + """ + When I start Karma + Then it fails with: + """ + Error: Failed to load error/import-something-from-somewhere.js + """ \ No newline at end of file diff --git a/test/e2e/support/error/import-something-from-somewhere.js b/test/e2e/support/error/import-something-from-somewhere.js new file mode 100644 index 000000000..1a56d4654 --- /dev/null +++ b/test/e2e/support/error/import-something-from-somewhere.js @@ -0,0 +1,2 @@ +import { something } from './somewhere.js' +console.log(something) diff --git a/test/unit/middleware/karma.spec.js b/test/unit/middleware/karma.spec.js index 16242d09d..b9286665e 100644 --- a/test/unit/middleware/karma.spec.js +++ b/test/unit/middleware/karma.spec.js @@ -207,7 +207,8 @@ describe('middleware.karma', () => { response.once('end', () => { expect(nextSpy).not.to.have.been.called - expect(response).to.beServedAs(200, 'CONTEXT\n\n') + /* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */ + expect(response).to.beServedAs(200, `CONTEXT\n\n`) done() }) @@ -241,7 +242,8 @@ describe('middleware.karma', () => { response.once('end', () => { expect(nextSpy).not.to.have.been.called - expect(response).to.beServedAs(200, 'CONTEXT\n\n') + /* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */ + expect(response).to.beServedAs(200, `CONTEXT\n\n`) done() }) @@ -333,7 +335,8 @@ describe('middleware.karma', () => { ]) response.once('end', () => { - expect(response).to.beServedAs(200, 'CONTEXT\n') + /* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */ + expect(response).to.beServedAs(200, `CONTEXT\n`) done() }) @@ -366,7 +369,8 @@ describe('middleware.karma', () => { ]) response.once('end', () => { - expect(response).to.beServedAs(200, "window.__karma__.files = {\n '/__proxy__/__karma__/absolute/some/abc/a.js': 'sha_a',\n '/__proxy__/__karma__/base/b.js': 'sha_b',\n '/__proxy__/__karma__/absolute\\\\windows\\\\path\\\\uuu\\\\c.js': 'sha_c'\n};\n") + /* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */ + expect(response).to.beServedAs(200, `window.__karma__.files = {\n '/__proxy__/__karma__/absolute/some/abc/a.js': 'sha_a',\n '/__proxy__/__karma__/base/b.js': 'sha_b',\n '/__proxy__/__karma__/absolute\\\\windows\\\\path\\\\uuu\\\\c.js': 'sha_c'\n};\n`) done() }) @@ -376,7 +380,8 @@ describe('middleware.karma', () => { it('should escape quotes in mappings with all served files', (done) => { fsMock._touchFile('/karma/static/context.html', 0, '%MAPPINGS%') servedFiles([ - new MockFile("/some/abc/a'b.js", 'sha_a'), + /* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */ + new MockFile(`/some/abc/a'b.js`, 'sha_a'), new MockFile('/base/path/ba.js', 'sha_b') ]) @@ -396,7 +401,8 @@ describe('middleware.karma', () => { response.once('end', () => { expect(nextSpy).not.to.have.been.called - expect(response).to.beServedAs(200, 'DEBUG\n\n') + /* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */ + expect(response).to.beServedAs(200, `DEBUG\n\n`) done() })