Skip to content

Commit a8974d8

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 a8974d8

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/lib/icon/icon.spec.ts

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

4444
beforeEach(async(() => {
45-
fakePath = '/fake-path';
45+
fakePath = '/$fake-path';
4646

4747
TestBed.configureTestingModule({
4848
imports: [HttpClientTestingModule, MatIconModule],
@@ -630,7 +630,7 @@ describe('MatIcon', () => {
630630

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

635635
tick();
636636
}));
@@ -651,17 +651,18 @@ describe('MatIcon', () => {
651651
fixture.detectChanges();
652652
let circle = fixture.nativeElement.querySelector('mat-icon svg circle');
653653

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

658-
fakePath = '/another-fake-path';
658+
fakePath = '/$another-fake-path';
659659
fixture = TestBed.createComponent(IconFromSvgName);
660660
fixture.componentInstance.iconName = 'fido';
661661
fixture.detectChanges();
662662
circle = fixture.nativeElement.querySelector('mat-icon svg circle');
663663

664-
expect(circle.getAttribute('filter')).toMatch(/^url\(['"]?\/another-fake-path#blur['"]?\)$/);
664+
expect(circle.getAttribute('filter'))
665+
.toMatch(/^url\(['"]?\/\$another-fake-path#blur['"]?\)$/);
665666
tick();
666667
}));
667668

@@ -683,13 +684,13 @@ describe('MatIcon', () => {
683684

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

689-
fakePath = '/different-path';
690+
fakePath = '/$different-path';
690691
fixture.detectChanges();
691692

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

695696
});

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)