Skip to content

Commit

Permalink
fix(middleware): throw script tag errors
Browse files Browse the repository at this point in the history
Include failing path. This is important for module tags which otherwise
simply give 404 at the server.

Fixes karma-runner#3572
  • Loading branch information
johnjbarton committed Dec 15, 2020
1 parent 3fed0bc commit 4f81f85
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
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 'Error loading ${filePath}'" src="${filePath}" ${crossOriginAttribute}></script>`)
}
}

Expand Down
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 'Error loading /__proxy__/__karma__/absolute/first.js?sha123'" src="/__proxy__/__karma__/absolute/first.js?sha123" crossorigin="anonymous"></script>\n<script type="application/dart" onerror="throw 'Error loading /__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 'Error loading /__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 'Error loading /__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 'Error loading 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 'Error loading /__proxy__/__karma__/absolute/first.js'" src="/__proxy__/__karma__/absolute/first.js" crossorigin="anonymous"></script>\n<script type="text/javascript" onerror="throw 'Error loading /__proxy__/__karma__/base/b.js'" src="/__proxy__/__karma__/base/b.js" crossorigin="anonymous"></script>`)
done()
})

Expand Down

0 comments on commit 4f81f85

Please sign in to comment.