Skip to content

Commit

Permalink
fix(middleware): catch errors when loading a module
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjbarton committed Dec 24, 2020
1 parent 3c649fa commit 6d7fa54
Show file tree
Hide file tree
Showing 2 changed files with 21 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
"""

0 comments on commit 6d7fa54

Please sign in to comment.