11process . env . VUE_LOADER_TEST = true
22
3- var fs = require ( 'fs' )
43var path = require ( 'path' )
54var webpack = require ( 'webpack' )
5+ var MemoryFS = require ( 'memory-fs' )
66var jsdom = require ( 'jsdom' )
77var expect = require ( 'chai' ) . expect
88var rimraf = require ( 'rimraf' )
@@ -11,66 +11,61 @@ var SourceMapConsumer = require('source-map').SourceMapConsumer
1111var ExtractTextPlugin = require ( "extract-text-webpack-plugin" )
1212var compiler = require ( 'vue-template-compiler' )
1313
14- function assertRenderFn ( options , template ) {
15- var compiled = compiler . compile ( template )
16- expect ( options . render . toString ( ) ) . to . equal ( 'function (){' + compiled . render + '}' )
14+ var loaderPath = 'expose?vueModule!' + path . resolve ( __dirname , '../' )
15+ var mfs = new MemoryFS ( )
16+ var globalConfig = {
17+ output : {
18+ path : '/' ,
19+ filename : 'test.build.js'
20+ } ,
21+ module : {
22+ loaders : [
23+ {
24+ test : / \. v u e $ / ,
25+ loader : loaderPath
26+ }
27+ ]
28+ }
1729}
1830
19- describe ( 'vue-loader' , function ( ) {
20- var testHTML = '<!DOCTYPE html><html><head></head><body></body></html>'
21- var outputDir = path . resolve ( __dirname , './output' )
22- var loaderPath = 'expose?vueModule!' + path . resolve ( __dirname , '../' )
23- var globalConfig = {
24- output : {
25- path : outputDir ,
26- filename : 'test.build.js'
27- } ,
28- module : {
29- loaders : [
30- {
31- test : / \. v u e $ / ,
32- loader : loaderPath
33- }
34- ]
31+ function bundle ( options , cb ) {
32+ var config = Object . assign ( { } , globalConfig , options )
33+ var webpackCompiler = webpack ( config )
34+ webpackCompiler . outputFileSystem = mfs
35+ webpackCompiler . run ( function ( err , stats ) {
36+ expect ( err ) . to . be . null
37+ if ( stats . compilation . errors . length ) {
38+ stats . compilation . errors . forEach ( function ( err ) {
39+ console . error ( err . message )
40+ } )
3541 }
36- }
37-
38- beforeEach ( function ( done ) {
39- rimraf ( outputDir , done )
42+ expect ( stats . compilation . errors ) . to . be . empty
43+ cb ( mfs . readFileSync ( '/test.build.js' ) . toString ( ) )
4044 } )
45+ }
4146
42- function getFile ( file , cb ) {
43- fs . readFile ( path . resolve ( outputDir , file ) , 'utf-8' , function ( err , data ) {
44- expect ( err ) . to . be . not . exist
45- cb ( data )
46- } )
47- }
48-
49- function test ( options , assert ) {
50- var config = Object . assign ( { } , globalConfig , options )
51- webpack ( config , function ( err , stats ) {
52- if ( stats . compilation . errors . length ) {
53- stats . compilation . errors . forEach ( function ( err ) {
54- console . error ( err . message )
55- } )
47+ function test ( options , assert ) {
48+ bundle ( options , function ( code ) {
49+ jsdom . env ( {
50+ html : '<!DOCTYPE html><html><head></head><body></body></html>' ,
51+ src : [ code ] ,
52+ done : function ( err , window ) {
53+ if ( err ) {
54+ console . log ( err [ 0 ] . data . error . stack )
55+ expect ( err ) . to . be . null
56+ }
57+ assert ( window )
5658 }
57- expect ( stats . compilation . errors ) . to . be . empty
58- getFile ( 'test.build.js' , function ( data ) {
59- jsdom . env ( {
60- html : testHTML ,
61- src : [ data ] ,
62- done : function ( err , window ) {
63- if ( err ) {
64- console . log ( err [ 0 ] . data . error . stack )
65- expect ( err ) . to . be . null
66- }
67- assert ( window )
68- }
69- } )
70- } )
7159 } )
72- }
60+ } )
61+ }
62+
63+ function assertRenderFn ( options , template ) {
64+ var compiled = compiler . compile ( template )
65+ expect ( options . render . toString ( ) ) . to . equal ( 'function (){' + compiled . render + '}' )
66+ }
7367
68+ describe ( 'vue-loader' , function ( ) {
7469 it ( 'basic' , function ( done ) {
7570 test ( {
7671 entry : './test/fixtures/basic.vue'
@@ -164,30 +159,26 @@ describe('vue-loader', function () {
164159 entry : './test/fixtures/basic.vue' ,
165160 devtool : 'source-map'
166161 } )
167- webpack ( config , function ( err ) {
168- expect ( err ) . to . be . null
169- getFile ( 'test.build.js.map' , function ( map ) {
170- var smc = new SourceMapConsumer ( JSON . parse ( map ) )
171- getFile ( 'test.build.js' , function ( code ) {
172- var line
173- var col
174- var targetRE = / ^ \s + m s g : ' H e l l o f r o m C o m p o n e n t A ! ' /
175- code . split ( / \r ? \n / g) . some ( function ( l , i ) {
176- if ( targetRE . test ( l ) ) {
177- line = i + 1
178- col = l . length
179- return true
180- }
181- } )
182- var pos = smc . originalPositionFor ( {
183- line : line ,
184- column : col
185- } )
186- expect ( pos . source . indexOf ( 'basic.vue' ) > - 1 )
187- expect ( pos . line ) . to . equal ( 9 )
188- done ( )
189- } )
162+ bundle ( config , function ( code ) {
163+ var map = mfs . readFileSync ( '/test.build.js.map' ) . toString ( )
164+ var smc = new SourceMapConsumer ( JSON . parse ( map ) )
165+ var line
166+ var col
167+ var targetRE = / ^ \s + m s g : ' H e l l o f r o m C o m p o n e n t A ! ' /
168+ code . split ( / \r ? \n / g) . some ( function ( l , i ) {
169+ if ( targetRE . test ( l ) ) {
170+ line = i + 1
171+ col = l . length
172+ return true
173+ }
190174 } )
175+ var pos = smc . originalPositionFor ( {
176+ line : line ,
177+ column : col
178+ } )
179+ expect ( pos . source . indexOf ( 'basic.vue' ) > - 1 )
180+ expect ( pos . line ) . to . equal ( 9 )
181+ done ( )
191182 } )
192183 } )
193184
@@ -203,7 +194,7 @@ describe('vue-loader', function () {
203194 } )
204195
205196 it ( 'extract CSS' , function ( done ) {
206- webpack ( Object . assign ( { } , globalConfig , {
197+ bundle ( Object . assign ( { } , globalConfig , {
207198 entry : './test/fixtures/extract-css.vue' ,
208199 vue : {
209200 loaders : {
@@ -214,12 +205,10 @@ describe('vue-loader', function () {
214205 plugins : [
215206 new ExtractTextPlugin ( 'test.output.css' )
216207 ]
217- } ) , function ( err ) {
218- expect ( err ) . to . be . null
219- getFile ( 'test.output.css' , function ( data ) {
220- expect ( data ) . to . contain ( 'h1 {\n color: #f00;\n}\n\n\n\n\n\n\nh2 {\n color: green;\n}' )
221- done ( )
222- } )
208+ } ) , function ( ) {
209+ var css = mfs . readFileSync ( '/test.output.css' ) . toString ( )
210+ expect ( css ) . to . contain ( 'h1 {\n color: #f00;\n}\n\n\n\n\n\n\nh2 {\n color: green;\n}' )
211+ done ( )
223212 } )
224213 } )
225214
0 commit comments