@@ -50,6 +50,52 @@ Object.defineProperty(loaderContextMock, 'options', {
50
50
implementations . forEach ( ( implementation ) => {
51
51
const [ implementationName ] = implementation . info . split ( '\t' ) ;
52
52
53
+ function readCss ( ext , id ) {
54
+ return fs
55
+ . readFileSync (
56
+ path . join ( __dirname , ext , 'spec' , implementationName , `${ id } .css` ) ,
57
+ 'utf8'
58
+ )
59
+ . replace ( CR , '' ) ;
60
+ }
61
+
62
+ function runWebpack ( baseConfig , loaderOptions , done ) {
63
+ const webpackConfig = merge (
64
+ {
65
+ mode : 'development' ,
66
+ output : {
67
+ path : path . join ( __dirname , 'output' ) ,
68
+ filename : 'bundle.js' ,
69
+ libraryTarget : 'commonjs2' ,
70
+ } ,
71
+ module : {
72
+ rules : [
73
+ {
74
+ test : / \. s [ a c ] s s $ / ,
75
+ use : [
76
+ { loader : 'raw-loader' } ,
77
+ {
78
+ loader : pathToSassLoader ,
79
+ options : merge ( { implementation } , loaderOptions ) ,
80
+ } ,
81
+ ] ,
82
+ } ,
83
+ ] ,
84
+ } ,
85
+ } ,
86
+ baseConfig
87
+ ) ;
88
+
89
+ webpack ( webpackConfig , ( webpackErr , stats ) => {
90
+ const err =
91
+ webpackErr ||
92
+ ( stats . hasErrors ( ) && stats . compilation . errors [ 0 ] ) ||
93
+ ( stats . hasWarnings ( ) && stats . compilation . warnings [ 0 ] ) ;
94
+
95
+ done ( err || null ) ;
96
+ } ) ;
97
+ }
98
+
53
99
describe ( implementationName , ( ) => {
54
100
syntaxStyles . forEach ( ( ext ) => {
55
101
function execTest ( testId , loaderOptions , webpackOptions ) {
@@ -405,7 +451,7 @@ implementations.forEach((implementation) => {
405
451
}
406
452
) ;
407
453
} ) ;
408
- it ( 'should not swallow errors when trying to load node- sass' , ( done ) => {
454
+ it ( 'should not swallow errors when trying to load sass implementation ' , ( done ) => {
409
455
mockRequire . reRequire ( pathToSassLoader ) ;
410
456
// eslint-disable-next-line global-require
411
457
const module = require ( 'module' ) ;
@@ -414,7 +460,7 @@ implementations.forEach((implementation) => {
414
460
415
461
// eslint-disable-next-line no-underscore-dangle
416
462
module . _resolveFilename = function _resolveFilename ( filename ) {
417
- if ( ! filename . match ( / n o d e - s a s s / ) ) {
463
+ if ( ! filename . match ( / ^ ( n o d e - s a s s | s a s s ) $ / ) ) {
418
464
// eslint-disable-next-line prefer-rest-params
419
465
return originalResolve . apply ( this , arguments ) ;
420
466
}
@@ -520,54 +566,63 @@ implementations.forEach((implementation) => {
520
566
}
521
567
) ;
522
568
} ) ;
523
- } ) ;
524
- } ) ;
525
569
526
- function readCss ( ext , id ) {
527
- return fs
528
- . readFileSync (
529
- path . join ( __dirname , ext , 'spec' , implementationName , `${ id } .css` ) ,
530
- 'utf8'
531
- )
532
- . replace ( CR , '' ) ;
533
- }
534
-
535
- function runWebpack ( baseConfig , loaderOptions , done ) {
536
- const webpackConfig = merge (
537
- {
538
- mode : 'development' ,
539
- output : {
540
- path : path . join ( __dirname , 'output' ) ,
541
- filename : 'bundle.js' ,
542
- libraryTarget : 'commonjs2' ,
543
- } ,
544
- module : {
545
- rules : [
546
- {
547
- test : / \. s [ a c ] s s $ / ,
548
- use : [
549
- { loader : 'raw-loader' } ,
550
- {
551
- loader : pathToSassLoader ,
552
- options : merge ( { implementation } , loaderOptions ) ,
553
- } ,
554
- ] ,
555
- } ,
556
- ] ,
557
- } ,
558
- } ,
559
- baseConfig
560
- ) ;
570
+ const [ implName ] = implementation . info . trim ( ) . split ( / \s / ) ;
571
+
572
+ it ( `should load ${ implName } ` , ( done ) => {
573
+ mockRequire . reRequire ( pathToSassLoader ) ;
574
+ // eslint-disable-next-line global-require
575
+ const module = require ( 'module' ) ;
576
+ // eslint-disable-next-line no-underscore-dangle
577
+ const originalResolve = module . _resolveFilename ;
578
+
579
+ // eslint-disable-next-line no-underscore-dangle
580
+ module . _resolveFilename = function _resolveFilename ( filename ) {
581
+ if ( implName === 'node-sass' && filename . match ( / ^ s a s s $ / ) ) {
582
+ const err = new Error ( 'Some error' ) ;
561
583
562
- webpack ( webpackConfig , ( webpackErr , stats ) => {
563
- const err =
564
- webpackErr ||
565
- ( stats . hasErrors ( ) && stats . compilation . errors [ 0 ] ) ||
566
- ( stats . hasWarnings ( ) && stats . compilation . warnings [ 0 ] ) ;
584
+ err . code = 'MODULE_NOT_FOUND' ;
567
585
568
- done ( err || null ) ;
586
+ throw err ;
587
+ }
588
+
589
+ if ( implName === 'dart-sass' && filename . match ( / ^ n o d e - s a s s $ / ) ) {
590
+ const err = new Error ( 'Some error' ) ;
591
+
592
+ err . code = 'MODULE_NOT_FOUND' ;
593
+
594
+ throw err ;
595
+ }
596
+
597
+ // eslint-disable-next-line prefer-rest-params
598
+ return originalResolve . apply ( this , arguments ) ;
599
+ } ;
600
+
601
+ const pathToFile = path . resolve ( __dirname , './scss/simple.scss' ) ;
602
+
603
+ runWebpack (
604
+ {
605
+ entry : pathToFile ,
606
+ } ,
607
+ { implementation : null } ,
608
+ ( err ) => {
609
+ // eslint-disable-next-line no-underscore-dangle
610
+ module . _resolveFilename = originalResolve ;
611
+
612
+ if ( implName === 'node-sass' ) {
613
+ mockRequire . reRequire ( 'node-sass' ) ;
614
+ }
615
+
616
+ if ( implName === 'dart-sass' ) {
617
+ mockRequire . reRequire ( 'sass' ) ;
618
+ }
619
+
620
+ done ( err ) ;
621
+ }
622
+ ) ;
623
+ } ) ;
569
624
} ) ;
570
- }
625
+ } ) ;
571
626
} ) ;
572
627
} ) ;
573
628
0 commit comments