Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(middleware): throw script tag errors #3585

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function createKarmaMiddleware (
} else {
const scriptType = (SCRIPT_TYPE[fileType] || 'text/javascript')
const crossOriginAttribute = includeCrossOriginAttribute ? 'crossorigin="anonymous"' : ''
scriptTags.push(`<script type="${scriptType}" src="${filePath}" ${crossOriginAttribute}></script>`)
scriptTags.push(`<script type="${scriptType}" onerror="throw new Error('Failed to load ${filePath}')" 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:
"""
Error: Failed to load 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)
18 changes: 12 additions & 6 deletions test/unit/middleware/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ describe('middleware.karma', () => {

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CONTEXT\n<script type="text/javascript" src="/__proxy__/__karma__/absolute/first.js?sha123" crossorigin="anonymous"></script>\n<script type="application/dart" src="/__proxy__/__karma__/absolute/second.dart?sha456" crossorigin="anonymous"></script>')
/* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */
expect(response).to.beServedAs(200, `CONTEXT\n<script type="text/javascript" onerror="throw new Error('Failed to load /__proxy__/__karma__/absolute/first.js?sha123')" src="/__proxy__/__karma__/absolute/first.js?sha123" crossorigin="anonymous"></script>\n<script type="application/dart" onerror="throw new Error('Failed to load /__proxy__/__karma__/absolute/second.dart?sha456')" src="/__proxy__/__karma__/absolute/second.dart?sha456" crossorigin="anonymous"></script>`)
done()
})

Expand Down Expand Up @@ -241,7 +242,8 @@ describe('middleware.karma', () => {

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CONTEXT\n<script type="text/javascript" src="/__proxy__/__karma__/absolute/some/abc/a.js?sha" crossorigin="anonymous"></script>\n<script type="text/javascript" src="/__proxy__/__karma__/base/b.js?shaaa" crossorigin="anonymous"></script>')
/* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */
expect(response).to.beServedAs(200, `CONTEXT\n<script type="text/javascript" onerror="throw new Error('Failed to load /__proxy__/__karma__/absolute/some/abc/a.js?sha')" src="/__proxy__/__karma__/absolute/some/abc/a.js?sha" crossorigin="anonymous"></script>\n<script type="text/javascript" onerror="throw new Error('Failed to load /__proxy__/__karma__/base/b.js?shaaa')" src="/__proxy__/__karma__/base/b.js?shaaa" crossorigin="anonymous"></script>`)
done()
})

Expand Down Expand Up @@ -333,7 +335,8 @@ describe('middleware.karma', () => {
])

response.once('end', () => {
expect(response).to.beServedAs(200, 'CONTEXT\n<script type="text/javascript" src="http://some.url.com/whatever" crossorigin="anonymous"></script>')
/* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */
expect(response).to.beServedAs(200, `CONTEXT\n<script type="text/javascript" onerror="throw new Error('Failed to load http://some.url.com/whatever')" src="http://some.url.com/whatever" crossorigin="anonymous"></script>`)
done()
})

Expand Down Expand Up @@ -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()
})

Expand All @@ -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')
])

Expand All @@ -396,7 +401,8 @@ describe('middleware.karma', () => {

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'DEBUG\n<script type="text/javascript" src="/__proxy__/__karma__/absolute/first.js" crossorigin="anonymous"></script>\n<script type="text/javascript" src="/__proxy__/__karma__/base/b.js" crossorigin="anonymous"></script>')
/* eslint quotes: ["error", "single", { "allowTemplateLiterals": true }] */
expect(response).to.beServedAs(200, `DEBUG\n<script type="text/javascript" onerror="throw new Error('Failed to load /__proxy__/__karma__/absolute/first.js')" src="/__proxy__/__karma__/absolute/first.js" crossorigin="anonymous"></script>\n<script type="text/javascript" onerror="throw new Error('Failed to load /__proxy__/__karma__/base/b.js')" src="/__proxy__/__karma__/base/b.js" crossorigin="anonymous"></script>`)
done()
})

Expand Down