Skip to content

Commit edd51fc

Browse files
committed
build: avoid 404 warnings in Karma tests
Adds some logic to the Karma config in order to prevent 404s from being logged from some of the tests that need to reference images. Fixes #15113.
1 parent 57aadc2 commit edd51fc

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/lib/icon/icon.spec.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ describe('MatIcon', () => {
4242
let fakePath: string;
4343

4444
beforeEach(async(() => {
45-
fakePath = '/fake-path';
45+
// The $ prefix tells Karma not to try to process the
46+
// request so that we don't get warnings in our logs.
47+
fakePath = '/$fake-path';
4648

4749
TestBed.configureTestingModule({
4850
imports: [HttpClientTestingModule, MatIconModule],
@@ -630,7 +632,7 @@ describe('MatIcon', () => {
630632

631633
// We use a regex to match here, rather than the exact value, because different browsers
632634
// return different quotes through `getAttribute`, while some even omit the quotes altogether.
633-
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/fake-path#blur['"]?\)$/);
635+
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/\$fake-path#blur['"]?\)$/);
634636

635637
tick();
636638
}));
@@ -651,17 +653,18 @@ describe('MatIcon', () => {
651653
fixture.detectChanges();
652654
let circle = fixture.nativeElement.querySelector('mat-icon svg circle');
653655

654-
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/fake-path#blur['"]?\)$/);
656+
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/\$fake-path#blur['"]?\)$/);
655657
tick();
656658
fixture.destroy();
657659

658-
fakePath = '/another-fake-path';
660+
fakePath = '/$another-fake-path';
659661
fixture = TestBed.createComponent(IconFromSvgName);
660662
fixture.componentInstance.iconName = 'fido';
661663
fixture.detectChanges();
662664
circle = fixture.nativeElement.querySelector('mat-icon svg circle');
663665

664-
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/another-fake-path#blur['"]?\)$/);
666+
expect(circle.getAttribute('filter'))
667+
.toMatch(/^url\(['"]?\/\$another-fake-path#blur['"]?\)$/);
665668
tick();
666669
}));
667670

@@ -683,13 +686,13 @@ describe('MatIcon', () => {
683686

684687
// We use a regex to match here, rather than the exact value, because different browsers
685688
// return different quotes through `getAttribute`, while some even omit the quotes altogether.
686-
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/fake-path#blur['"]?\)$/);
689+
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/\$fake-path#blur['"]?\)$/);
687690
tick();
688691

689-
fakePath = '/different-path';
692+
fakePath = '/$different-path';
690693
fixture.detectChanges();
691694

692-
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/different-path#blur['"]?\)$/);
695+
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/\$different-path#blur['"]?\)$/);
693696
}));
694697

695698
});

test/karma.conf.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@ module.exports = config => {
55
config.set({
66
basePath: path.join(__dirname, '..'),
77
frameworks: ['jasmine'],
8+
middleware: ['fake-url'],
89
plugins: [
910
require('karma-jasmine'),
1011
require('karma-browserstack-launcher'),
1112
require('karma-sauce-launcher'),
1213
require('karma-chrome-launcher'),
1314
require('karma-firefox-launcher'),
1415
require('karma-sourcemap-loader'),
16+
{'middleware:fake-url': ['factory', function() {
17+
// Middleware that avoids triggering 404s during tests that need to reference
18+
// image paths. Assumes that the image path will start with `/$`.
19+
return function(request, response, next) {
20+
if (request.url.indexOf('/$') === 0) {
21+
response.writeHead(200);
22+
return response.end();
23+
}
24+
25+
next();
26+
}
27+
}]}
1528
],
1629
files: [
1730
{pattern: 'node_modules/core-js/client/core.min.js', included: true, watched: false},

0 commit comments

Comments
 (0)