Skip to content

Commit e64280c

Browse files
test: refactor
1 parent bdd2ec5 commit e64280c

File tree

3 files changed

+267
-183
lines changed

3 files changed

+267
-183
lines changed

test/api.test.js

Lines changed: 257 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import middleware from '../src';
99
import getCompiler from './helpers/getCompiler';
1010
import getCompilerHooks from './helpers/getCompilerHooks';
1111
import webpackConfig from './fixtures/webpack.config';
12+
import webpackPublicPathConfig from './fixtures/webpack.public-path.config';
13+
import webpackMultiConfig from './fixtures/webpack.array.config';
1214

1315
// Suppress unnecessary stats output
1416
global.console.log = jest.fn();
@@ -375,51 +377,278 @@ describe.each([
375377
});
376378

377379
describe('getFilenameFromUrl method', () => {
378-
beforeEach((done) => {
379-
compiler = getCompiler(webpackConfig);
380+
describe('should work', () => {
381+
beforeEach((done) => {
382+
compiler = getCompiler(webpackConfig);
380383

381-
instance = middleware(compiler);
384+
instance = middleware(compiler);
382385

383-
app = framework();
384-
app.use(instance);
386+
app = framework();
387+
app.use(instance);
385388

386-
listen = app.listen((error) => {
387-
if (error) {
388-
return done(error);
389+
listen = app.listen((error) => {
390+
if (error) {
391+
return done(error);
392+
}
393+
394+
return done();
395+
});
396+
});
397+
398+
afterEach((done) => {
399+
if (instance.context.watching.closed) {
400+
if (listen) {
401+
listen.close(done);
402+
} else {
403+
done();
404+
}
405+
406+
return;
389407
}
390408

391-
return done();
409+
instance.close(() => {
410+
if (listen) {
411+
listen.close(done);
412+
} else {
413+
done();
414+
}
415+
});
392416
});
393-
});
394417

395-
afterEach((done) => {
396-
if (instance.context.watching.closed) {
397-
if (listen) {
398-
listen.close(done);
399-
} else {
418+
it('should work', (done) => {
419+
instance.waitUntilValid(() => {
420+
expect(instance.getFilenameFromUrl('/bundle.js')).toBe(
421+
path.join(webpackConfig.output.path, '/bundle.js')
422+
);
423+
expect(instance.getFilenameFromUrl('/')).toBe(
424+
path.join(webpackConfig.output.path, '/index.html')
425+
);
426+
expect(instance.getFilenameFromUrl('/index.html')).toBe(
427+
path.join(webpackConfig.output.path, '/index.html')
428+
);
429+
expect(instance.getFilenameFromUrl('/svg.svg')).toBe(
430+
path.join(webpackConfig.output.path, '/svg.svg')
431+
);
432+
expect(
433+
instance.getFilenameFromUrl('/unknown.unknown')
434+
).toBeUndefined();
435+
expect(
436+
instance.getFilenameFromUrl('/unknown/unknown.unknown')
437+
).toBeUndefined();
438+
400439
done();
440+
});
441+
});
442+
});
443+
444+
describe('should work when the "index" option disabled', () => {
445+
beforeEach((done) => {
446+
compiler = getCompiler(webpackConfig);
447+
448+
instance = middleware(compiler, { index: false });
449+
450+
app = framework();
451+
app.use(instance);
452+
453+
listen = app.listen((error) => {
454+
if (error) {
455+
return done(error);
456+
}
457+
458+
return done();
459+
});
460+
});
461+
462+
afterEach((done) => {
463+
if (instance.context.watching.closed) {
464+
if (listen) {
465+
listen.close(done);
466+
} else {
467+
done();
468+
}
469+
470+
return;
401471
}
402472

403-
return;
404-
}
473+
instance.close(() => {
474+
if (listen) {
475+
listen.close(done);
476+
} else {
477+
done();
478+
}
479+
});
480+
});
481+
482+
it('should work', (done) => {
483+
instance.waitUntilValid(() => {
484+
expect(instance.getFilenameFromUrl('/bundle.js')).toBe(
485+
path.join(webpackConfig.output.path, '/bundle.js')
486+
);
487+
// eslint-disable-next-line no-undefined
488+
expect(instance.getFilenameFromUrl('/')).toBe(undefined);
489+
expect(instance.getFilenameFromUrl('/index.html')).toBe(
490+
path.join(webpackConfig.output.path, '/index.html')
491+
);
492+
expect(instance.getFilenameFromUrl('/svg.svg')).toBe(
493+
path.join(webpackConfig.output.path, '/svg.svg')
494+
);
495+
expect(
496+
instance.getFilenameFromUrl('/unknown.unknown')
497+
).toBeUndefined();
498+
expect(
499+
instance.getFilenameFromUrl('/unknown/unknown.unknown')
500+
).toBeUndefined();
405501

406-
instance.close(() => {
407-
if (listen) {
408-
listen.close(done);
409-
} else {
410502
done();
503+
});
504+
});
505+
});
506+
507+
describe('should work with the "publicPath" option', () => {
508+
beforeEach((done) => {
509+
compiler = getCompiler(webpackPublicPathConfig);
510+
511+
instance = middleware(compiler);
512+
513+
app = framework();
514+
app.use(instance);
515+
516+
listen = app.listen((error) => {
517+
if (error) {
518+
return done(error);
519+
}
520+
521+
return done();
522+
});
523+
});
524+
525+
afterEach((done) => {
526+
if (instance.context.watching.closed) {
527+
if (listen) {
528+
listen.close(done);
529+
} else {
530+
done();
531+
}
532+
533+
return;
411534
}
535+
536+
instance.close(() => {
537+
if (listen) {
538+
listen.close(done);
539+
} else {
540+
done();
541+
}
542+
});
543+
});
544+
545+
it('should work', (done) => {
546+
instance.waitUntilValid(() => {
547+
expect(instance.getFilenameFromUrl('/public/path/bundle.js')).toBe(
548+
path.join(webpackPublicPathConfig.output.path, '/bundle.js')
549+
);
550+
expect(instance.getFilenameFromUrl('/public/path/')).toBe(
551+
path.join(webpackPublicPathConfig.output.path, '/index.html')
552+
);
553+
expect(instance.getFilenameFromUrl('/public/path/index.html')).toBe(
554+
path.join(webpackPublicPathConfig.output.path, '/index.html')
555+
);
556+
expect(instance.getFilenameFromUrl('/public/path/svg.svg')).toBe(
557+
path.join(webpackPublicPathConfig.output.path, '/svg.svg')
558+
);
559+
560+
expect(instance.getFilenameFromUrl('/')).toBeUndefined();
561+
expect(
562+
instance.getFilenameFromUrl('/unknown.unknown')
563+
).toBeUndefined();
564+
expect(
565+
instance.getFilenameFromUrl('/unknown/unknown.unknown')
566+
).toBeUndefined();
567+
568+
done();
569+
});
412570
});
413571
});
414572

415-
it('should work', (done) => {
416-
instance.waitUntilValid(() => {
417-
expect(instance.getFilenameFromUrl('/unknown.unknown')).toBeUndefined();
418-
expect(instance.getFilenameFromUrl('/bundle.js')).toBe(
419-
path.join(webpackConfig.output.path, '/bundle.js')
420-
);
573+
describe('should work in multi compiler mode', () => {
574+
beforeEach((done) => {
575+
compiler = getCompiler(webpackMultiConfig);
421576

422-
done();
577+
instance = middleware(compiler);
578+
579+
app = framework();
580+
app.use(instance);
581+
582+
listen = app.listen((error) => {
583+
if (error) {
584+
return done(error);
585+
}
586+
587+
return done();
588+
});
589+
});
590+
591+
afterEach((done) => {
592+
if (instance.context.watching.closed) {
593+
if (listen) {
594+
listen.close(done);
595+
} else {
596+
done();
597+
}
598+
599+
return;
600+
}
601+
602+
instance.close(() => {
603+
if (listen) {
604+
listen.close(done);
605+
} else {
606+
done();
607+
}
608+
});
609+
});
610+
611+
it('should work', (done) => {
612+
instance.waitUntilValid(() => {
613+
expect(instance.getFilenameFromUrl('/static-one/bundle.js')).toBe(
614+
path.join(webpackMultiConfig[0].output.path, '/bundle.js')
615+
);
616+
expect(instance.getFilenameFromUrl('/static-one/')).toBe(
617+
path.join(webpackMultiConfig[0].output.path, '/index.html')
618+
);
619+
expect(instance.getFilenameFromUrl('/static-one/index.html')).toBe(
620+
path.join(webpackMultiConfig[0].output.path, '/index.html')
621+
);
622+
expect(instance.getFilenameFromUrl('/static-one/svg.svg')).toBe(
623+
path.join(webpackMultiConfig[0].output.path, '/svg.svg')
624+
);
625+
expect(
626+
instance.getFilenameFromUrl('/static-one/unknown.unknown')
627+
).toBeUndefined();
628+
expect(
629+
instance.getFilenameFromUrl('/static-one/unknown/unknown.unknown')
630+
).toBeUndefined();
631+
632+
expect(instance.getFilenameFromUrl('/static-two/bundle.js')).toBe(
633+
path.join(webpackMultiConfig[1].output.path, '/bundle.js')
634+
);
635+
expect(
636+
instance.getFilenameFromUrl('/static-two/unknown.unknown')
637+
).toBeUndefined();
638+
expect(
639+
instance.getFilenameFromUrl('/static-two/unknown/unknown.unknown')
640+
).toBeUndefined();
641+
642+
expect(instance.getFilenameFromUrl('/')).toBeUndefined();
643+
expect(
644+
instance.getFilenameFromUrl('/static-one/unknown.unknown')
645+
).toBeUndefined();
646+
expect(
647+
instance.getFilenameFromUrl('/static-one/unknown/unknown.unknown')
648+
).toBeUndefined();
649+
650+
done();
651+
});
423652
});
424653
});
425654
});

test/fixtures/webpack.public-path.config.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@ const path = require('path');
55
module.exports = {
66
mode: 'development',
77
context: path.resolve(__dirname),
8-
entry: './simple.js',
8+
entry: './foo.js',
99
output: {
1010
filename: 'bundle.js',
1111
path: path.resolve(__dirname, '../outputs/public-path'),
1212
publicPath: '/public/path/',
1313
},
14+
module: {
15+
rules: [
16+
{
17+
test: /\.(svg|html)$/,
18+
loader: 'file-loader',
19+
options: { name: '[name].[ext]' },
20+
},
21+
],
22+
},
1423
infrastructureLogging: {
1524
level: 'none'
1625
},

0 commit comments

Comments
 (0)