33var should = require ( "should" ) ;
44var path = require ( "path" ) ;
55var webpack = require ( "webpack" ) ;
6- var enhancedReqFactory = require ( "enhanced-require" ) ;
76var fs = require ( "fs" ) ;
87var moveModulesDir = require ( "./helpers/moveModulesDir.js" ) ;
98
@@ -17,7 +16,7 @@ describe("less-loader", function() {
1716 test ( "should resolve all imports" , "imports" ) ;
1817 test ( "should resolve all imports from bower_components" , "imports-bower" , {
1918 before : function ( config ) {
20- config . resolve . root . push ( bowerComponents ) ;
19+ config . resolve . modules . push ( bowerComponents ) ;
2120 }
2221 } ) ;
2322 test ( "should resolve all imports from node_modules" , "imports-node" , {
@@ -34,24 +33,37 @@ describe("less-loader", function() {
3433 test ( "should transform urls to files above the current directory" , "folder/url-path" ) ;
3534 test ( "should transform urls to files above the sibling directory" , "folder2/url-path" ) ;
3635 test ( "should generate source-map" , "source-map" , {
37- query : "?sourceMap" ,
36+ options : {
37+ sourceMap : true
38+ } ,
3839 devtool : "source-map"
3940 } ) ;
4041 test ( "should install plugins" , "url-path" , {
41- query : "?config=lessLoaderTest" ,
42- lessPlugins : [
43- { wasCalled : false , install : function ( ) { this . wasCalled = true ; } }
44- ] ,
42+ options : {
43+ lessPlugins : [
44+ { wasCalled : false , install : function ( ) { this . wasCalled = true ; } }
45+ ]
46+ } ,
4547 after : function ( testVariables ) {
46- this . lessPlugins [ 0 ] . wasCalled . should . be . true ;
48+ this . options . lessPlugins [ 0 ] . wasCalled . should . be . true ;
49+ }
50+ } ) ;
51+ // See https://github.com/webpack/loader-utils/issues/56
52+ test ( "should not alter the original options object" , "basic" , {
53+ options : {
54+ lessPlugins : [ ]
55+ } ,
56+ after : function ( ) {
57+ // We know that the loader will add its own plugin, but it should not alter the original array
58+ this . options . lessPlugins . should . have . length ( 0 ) ;
4759 }
4860 } ) ;
4961 it ( "should report error correctly" , function ( done ) {
5062 webpack ( {
5163 entry : path . resolve ( __dirname , "../index.js" ) + "!" +
5264 path . resolve ( __dirname , "./less/error.less" ) ,
5365 output : {
54- path : __dirname + "/ output",
66+ path : path . resolve ( __dirname , " output") ,
5567 filename : "bundle.js"
5668 }
5769 } , function ( err , stats ) {
@@ -70,58 +82,48 @@ function readCss(id) {
7082 return fs . readFileSync ( path . resolve ( __dirname , "./css/" + id + ".css" ) , "utf8" ) . replace ( CR , "" ) ;
7183}
7284
73- function tryMkdirSync ( dirname ) {
74- try {
75- fs . mkdirSync ( dirname ) ;
76- } catch ( e ) {
77- if ( ! e || e . code !== "EEXIST" )
78- throw e ;
79- }
80- }
81-
8285function test ( name , id , testOptions ) {
8386 testOptions = testOptions || { } ;
84- testOptions . query = testOptions . query || "" ;
87+ testOptions . options = testOptions . options || "" ;
8588
8689 it ( name , function ( done ) {
8790 var expectedCss = readCss ( id ) ;
88- var lessFile = "raw!" +
89- pathToLessLoader + testOptions . query + "!" +
90- path . resolve ( __dirname , "./less/" + id + ".less" ) ;
91+ var lessFile = path . resolve ( __dirname , "./less/" + id + ".less" ) ;
9192 var actualCss ;
9293 var config = {
9394 resolve : {
94- root : [
95- __dirname
95+ modules : [
96+ "node_modules"
9697 ]
9798 }
9899 } ;
99- var enhancedReq ;
100100
101101 testOptions . before && testOptions . before ( config ) ;
102102
103- enhancedReq = enhancedReqFactory ( module , config ) ;
104-
105- // run synchronously
106- actualCss = enhancedReq ( lessFile ) ;
107- // writing the actual css to output-dir for better debugging
108- tryMkdirSync ( __dirname + "/output/" ) ;
109- fs . writeFileSync ( __dirname + "/output/" + name + ".sync.css" , actualCss , "utf8" ) ;
110-
111- actualCss . should . eql ( expectedCss ) ;
112-
113103 // run asynchronously
114104 webpack ( {
115105 entry : lessFile ,
106+ context : __dirname ,
116107 devtool : testOptions . devtool ,
117108 resolve : config . resolve ,
118109 output : {
119- path : __dirname + "/ output",
110+ path : path . resolve ( __dirname , " output") ,
120111 filename : "bundle.js" ,
121112 libraryTarget : "commonjs2"
122113 } ,
123- lessLoaderTest : {
124- lessPlugins : testOptions . lessPlugins || [ ]
114+ module : {
115+ rules : [
116+ {
117+ test : / \. l e s s $ / ,
118+ use : [
119+ "raw-loader" ,
120+ {
121+ loader : pathToLessLoader ,
122+ options : testOptions . options
123+ }
124+ ]
125+ }
126+ ]
125127 }
126128 } , function onCompilationFinished ( err , stats ) {
127129 var actualMap ;
@@ -139,16 +141,16 @@ function test(name, id, testOptions) {
139141
140142 actualCss = require ( "./output/bundle.js" ) ;
141143 // writing the actual css to output-dir for better debugging
142- fs . writeFileSync ( __dirname + "/ output/" + name + ".async.css" , actualCss , "utf8" ) ;
144+ fs . writeFileSync ( path . resolve ( __dirname , " output" , name + ".async.css" ) , actualCss , "utf8" ) ;
143145 actualCss . should . eql ( expectedCss ) ;
144146
145147 testOptions . after && testOptions . after ( ) ;
146148
147149 if ( testOptions . devtool === "source-map" ) {
148- actualMap = fs . readFileSync ( __dirname + "/ output/ bundle.js.map", "utf8" ) ;
149- fs . writeFileSync ( __dirname + "/ output/" + name + ".sync.css.map" , actualMap , "utf8" ) ;
150+ actualMap = fs . readFileSync ( path . resolve ( __dirname , " output" , " bundle.js.map") , "utf8" ) ;
151+ fs . writeFileSync ( path . resolve ( __dirname , " output" , name + ".sync.css.map" ) , actualMap , "utf8" ) ;
150152 actualMap = JSON . parse ( actualMap ) ;
151- actualMap . sources . should . containEql ( "webpack:///./test/ less/" + id + ".less" ) ;
153+ actualMap . sources . should . containEql ( "webpack:///./less/" + id + ".less" ) ;
152154 }
153155
154156 done ( ) ;
0 commit comments