Skip to content

Commit

Permalink
fix(middleware): catch errors when loading a module (karma-runner#3605)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjbarton authored Jan 12, 2021
1 parent 3fca456 commit fec972f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ function createKarmaMiddleware (
} else {
const scriptType = (SCRIPT_TYPE[fileType] || 'text/javascript')
const crossOriginAttribute = includeCrossOriginAttribute ? 'crossorigin="anonymous"' : ''
scriptTags.push(`<script type="${scriptType}" src="${filePath}" ${crossOriginAttribute}></script>`)
if (fileType === 'module') {
scriptTags.push(`<script onerror="throw 'Error loading ${filePath}'" type="${scriptType}" src="${filePath}" ${crossOriginAttribute}></script>`)
} else {
scriptTags.push(`<script type="${scriptType}" src="${filePath}" ${crossOriginAttribute}></script>`)
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/e2e/error.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Uncaught Error loading error/import-something-from-somewhere.js
"""
2 changes: 2 additions & 0 deletions test/e2e/support/error/import-something-from-somewhere.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { something } from './somewhere.js'
console.log(something)

0 comments on commit fec972f

Please sign in to comment.