Skip to content

Commit 5bfcf5f

Browse files
charlessuhsnasirca
andauthoredMar 8, 2021
fix: patch karma to allow loading virtual packages (#3663)
* fix: patch karma to allow loading virtual packages * fix: additional patch to handle SCRIPT_URL_ARRAY Co-authored-by: Shahriyar Nasir <contact@snasir.ca>
1 parent f52a071 commit 5bfcf5f

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed
 

‎lib/middleware/karma.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ function createKarmaMiddleware (
222222
}) : []
223223

224224
return data
225-
.replace('%SCRIPTS%', scriptTags.join('\n'))
225+
.replace('%SCRIPTS%', () => scriptTags.join('\n'))
226226
.replace('%CLIENT_CONFIG%', 'window.__karma__.config = ' + JSON.stringify(client) + ';\n')
227-
.replace('%SCRIPT_URL_ARRAY%', 'window.__karma__.scriptUrls = ' + JSON.stringify(scriptUrls) + ';\n')
228-
.replace('%MAPPINGS%', 'window.__karma__.files = {\n' + mappings.join(',\n') + '\n};\n')
227+
.replace('%SCRIPT_URL_ARRAY%', () => 'window.__karma__.scriptUrls = ' + JSON.stringify(scriptUrls) + ';\n')
228+
.replace('%MAPPINGS%', () => 'window.__karma__.files = {\n' + mappings.join(',\n') + '\n};\n')
229229
.replace('\n%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url))
230230
})
231231
})

‎test/unit/middleware/karma.spec.js

+45
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('middleware.karma', () => {
2828
karma: {
2929
static: {
3030
'client.html': mocks.fs.file(0, 'CLIENT HTML\n%X_UA_COMPATIBLE%%X_UA_COMPATIBLE_URL%'),
31+
'client_with_context.html': mocks.fs.file(0, 'CLIENT_WITH_CONTEXT\n%SCRIPT_URL_ARRAY%'),
3132
'context.html': mocks.fs.file(0, 'CONTEXT\n%SCRIPTS%'),
3233
'debug.html': mocks.fs.file(0, 'DEBUG\n%SCRIPTS%\n%X_UA_COMPATIBLE%'),
3334
'karma.js': mocks.fs.file(0, 'root: %KARMA_URL_ROOT%, proxy: %KARMA_PROXY_PATH%, v: %KARMA_VERSION%')
@@ -214,6 +215,21 @@ describe('middleware.karma', () => {
214215
callHandlerWith('/__karma__/context.html')
215216
})
216217

218+
it('should serve context.html without using special patterns when replacing script tags', (done) => {
219+
includedFiles([
220+
new MockFile('/.yarn/$$virtual/first.js', 'sha123'),
221+
new MockFile('/.yarn/$$virtual/second.dart', 'sha456')
222+
])
223+
224+
response.once('end', () => {
225+
expect(nextSpy).not.to.have.been.called
226+
expect(response).to.beServedAs(200, 'CONTEXT\n<script type="text/javascript" src="/__proxy__/__karma__/absolute/.yarn/$$virtual/first.js?sha123" crossorigin="anonymous"></script>\n<script type="text/javascript" src="/__proxy__/__karma__/absolute/.yarn/$$virtual/second.dart?sha456" crossorigin="anonymous"></script>')
227+
done()
228+
})
229+
230+
callHandlerWith('/__karma__/context.html')
231+
})
232+
217233
it('should serve context.html with replaced link tags', (done) => {
218234
includedFiles([
219235
new MockFile('/first.css', 'sha007'),
@@ -373,6 +389,20 @@ describe('middleware.karma', () => {
373389
callHandlerWith('/__karma__/context.html')
374390
})
375391

392+
it('should inline mappings without using special patterns', (done) => {
393+
fsMock._touchFile('/karma/static/context.html', 0, '%MAPPINGS%')
394+
servedFiles([
395+
new MockFile('/.yarn/$$virtual/abc/a.js', 'sha_a')
396+
])
397+
398+
response.once('end', () => {
399+
expect(response).to.beServedAs(200, "window.__karma__.files = {\n '/__proxy__/__karma__/absolute/.yarn/$$virtual/abc/a.js': 'sha_a'\n};\n")
400+
done()
401+
})
402+
403+
callHandlerWith('/__karma__/context.html')
404+
})
405+
376406
it('should escape quotes in mappings with all served files', (done) => {
377407
fsMock._touchFile('/karma/static/context.html', 0, '%MAPPINGS%')
378408
servedFiles([
@@ -490,4 +520,19 @@ describe('middleware.karma', () => {
490520

491521
callHandlerWith('/__karma__/debug.html')
492522
})
523+
524+
it('should serve client_with_context.html without using special patterns when replacing script urls', (done) => {
525+
includedFiles([
526+
new MockFile('/.yarn/$$virtual/first.js', 'sha123'),
527+
new MockFile('/.yarn/$$virtual/second.dart', 'sha456')
528+
])
529+
530+
response.once('end', () => {
531+
expect(nextSpy).not.to.have.been.called
532+
expect(response).to.beServedAs(200, 'CLIENT_WITH_CONTEXT\nwindow.__karma__.scriptUrls = ["\\\\x3Cscript type=\\"text/javascript\\" src=\\"/__proxy__/__karma__/absolute/.yarn/$$virtual/first.js\\" crossorigin=\\"anonymous\\"\\\\x3E\\\\x3C/script\\\\x3E","\\\\x3Cscript type=\\"text/javascript\\" src=\\"/__proxy__/__karma__/absolute/.yarn/$$virtual/second.dart\\" crossorigin=\\"anonymous\\"\\\\x3E\\\\x3C/script\\\\x3E"];\n')
533+
done()
534+
})
535+
536+
callHandlerWith('/__karma__/client_with_context.html')
537+
})
493538
})

0 commit comments

Comments
 (0)
Please sign in to comment.