11import { join } from 'path' ;
2- import * as rewire from 'rewire' ;
3-
4- const cleanCss = rewire ( './cleancss' ) ;
2+ import * as cleanCss from './cleancss' ;
53
64import * as cleanCssFactory from './util/clean-css-factory' ;
5+ import { CleanCssConfig , getCleanCssInstance } from './util/clean-css-factory'
76import * as config from './util/config' ;
87import * as helpers from './util/helpers' ;
98import * as workerClient from './worker-client' ;
@@ -12,38 +11,36 @@ import * as workerClient from './worker-client';
1211describe ( 'clean css task' , ( ) => {
1312
1413 describe ( 'cleancss' , ( ) => {
15- it ( 'should return when the worker returns' , ( done : Function ) => {
14+ it ( 'should return when the worker returns' , ( ) => {
1615 // arrange
1716 const context = { } ;
1817 const configFile : any = null ;
1918 const spy = spyOn ( workerClient , workerClient . runWorker . name ) . and . returnValue ( Promise . resolve ( ) ) ;
2019 // act
21- ( cleanCss as any ) . cleancss ( context , null ) . then ( ( ) => {
20+ return ( cleanCss as any ) . cleancss ( context , null ) . then ( ( ) => {
2221 // assert
2322 expect ( spy ) . toHaveBeenCalledWith ( 'cleancss' , 'cleancssWorker' , context , configFile ) ;
24- done ( ) ;
2523 } ) ;
2624 } ) ;
2725
28- it ( 'should throw when the worker throws' , ( done : Function ) => {
26+ it ( 'should throw when the worker throws' , ( ) => {
2927 // arrange
3028 const context = { } ;
3129 const errorMessage = 'Simulating an error' ;
3230 spyOn ( workerClient , workerClient . runWorker . name ) . and . returnValue ( Promise . reject ( new Error ( errorMessage ) ) ) ;
3331
3432 // act
35- ( cleanCss as any ) . cleancss ( context , null ) . then ( ( ) => {
33+ return ( cleanCss as any ) . cleancss ( context , null ) . then ( ( ) => {
3634 throw new Error ( 'Should never get here' ) ;
3735 } ) . catch ( ( err : Error ) => {
3836 // assert
39- expect ( err . message ) . toEqual ( errorMessage , `Expected ex.message ${ err . message } to equal ${ errorMessage } ` ) ;
40- done ( ) ;
37+ expect ( err . message ) . toEqual ( errorMessage ) ;
4138 } ) ;
4239 } ) ;
4340 } ) ;
4441
4542 describe ( 'cleancssworker' , ( ) => {
46- it ( 'should throw when reading the file throws' , ( done : Function ) => {
43+ it ( 'should throw when reading the file throws' , ( ) => {
4744 const errorMessage = 'simulating an error' ;
4845 // arrange
4946 const context = { buildDir : 'www' } ;
@@ -53,15 +50,14 @@ describe('clean css task', () => {
5350 spyOn ( helpers , helpers . readFileAsync . name ) . and . returnValue ( Promise . reject ( new Error ( errorMessage ) ) ) ;
5451
5552 // act
56- ( cleanCss as any ) . cleancssWorker ( context , null ) . then ( ( ) => {
53+ return ( cleanCss as any ) . cleancssWorker ( context , null ) . then ( ( ) => {
5754 throw new Error ( 'Should never get here' ) ;
5855 } ) . catch ( ( err : Error ) => {
5956 expect ( err . message ) . toEqual ( errorMessage ) ;
60- done ( ) ;
6157 } ) ;
6258 } ) ;
6359
64- it ( 'should return what writeFileAsync returns' , ( done : Function ) => {
60+ it ( 'should return what writeFileAsync returns' , ( ) => {
6561 // arrange
6662 const context = { buildDir : 'www' } ;
6763 const cleanCssConfig = { sourceFileName : 'sourceFileName' , destFileName : 'destFileName' } ;
@@ -71,26 +67,25 @@ describe('clean css task', () => {
7167 spyOn ( config , config . fillConfigDefaults . name ) . and . returnValue ( cleanCssConfig ) ;
7268 spyOn ( helpers , helpers . readFileAsync . name ) . and . returnValue ( Promise . resolve ( fileContent ) ) ;
7369 spyOn ( helpers , helpers . writeFileAsync . name ) . and . returnValue ( Promise . resolve ( ) ) ;
74-
75- // use rewire to stub this since jasmine is insufficient
76- const spy = jasmine . createSpy ( 'mySpy' ) . and . returnValue ( Promise . resolve ( minifiedContent ) ) ;
77- cleanCss . __set__ ( 'runCleanCss' , spy ) ;
70+ spyOn ( cleanCssFactory , 'getCleanCssInstance' ) . and . returnValue ( {
71+ minify : ( content : string , cb : Function ) => {
72+ cb ( null , { styles : minifiedContent } ) ;
73+ }
74+ } ) ;
7875
7976 // act
80- ( cleanCss as any ) . cleancssWorker ( context , null ) . then ( ( ) => {
77+ return ( cleanCss as any ) . cleancssWorker ( context , null ) . then ( ( ) => {
8178 // assert
8279 expect ( config . generateContext ) . toHaveBeenCalledWith ( context ) ;
8380 expect ( config . fillConfigDefaults ) . toHaveBeenCalledWith ( null , ( cleanCss as any ) . taskInfo . defaultConfigFile ) ;
8481 expect ( helpers . readFileAsync ) . toHaveBeenCalledWith ( join ( context . buildDir , cleanCssConfig . sourceFileName ) ) ;
8582 expect ( helpers . writeFileAsync ) . toHaveBeenCalledWith ( join ( context . buildDir , cleanCssConfig . destFileName ) , minifiedContent ) ;
86- expect ( spy ) . toHaveBeenCalledWith ( cleanCssConfig , fileContent ) ;
87- done ( ) ;
8883 } ) ;
8984 } ) ;
9085 } ) ;
9186
9287 describe ( 'runCleanCss' , ( ) => {
93- it ( 'should reject when minification errors out' , ( done : Function ) => {
88+ it ( 'should reject when minification errors out' , ( ) => {
9489 // arrange
9590 const errorMessage = 'simulating an error' ;
9691 const configFile = { options : { } } ;
@@ -108,16 +103,15 @@ describe('clean css task', () => {
108103 const callback = minifySpy . calls . mostRecent ( ) . args [ 1 ] ;
109104 callback ( new Error ( errorMessage ) , null ) ;
110105
111- promise . then ( ( ) => {
106+ return promise . then ( ( ) => {
112107 throw new Error ( 'Should never get here' ) ;
113108 } ) . catch ( ( err : Error ) => {
114109 // assert
115110 expect ( err . message ) . toEqual ( errorMessage ) ;
116- done ( ) ;
117111 } ) ;
118112 } ) ;
119113
120- it ( 'should reject when minification has one or more errors' , ( done : Function ) => {
114+ it ( 'should reject when minification has one or more errors' , ( ) => {
121115 // arrange
122116 const configFile = { options : { } } ;
123117 const fileContent = 'fileContent' ;
@@ -137,16 +131,15 @@ describe('clean css task', () => {
137131 const callback = minifySpy . calls . mostRecent ( ) . args [ 1 ] ;
138132 callback ( null , minificationResponse ) ;
139133
140- promise . then ( ( ) => {
134+ return promise . then ( ( ) => {
141135 throw new Error ( 'Should never get here' ) ;
142136 } ) . catch ( ( err : Error ) => {
143137 // assert
144138 expect ( err . message ) . toEqual ( minificationResponse . errors [ 0 ] ) ;
145- done ( ) ;
146139 } ) ;
147140 } ) ;
148141
149- it ( 'should return minified content' , ( done : Function ) => {
142+ it ( 'should return minified content' , ( ) => {
150143 const configFile = { options : { } } ;
151144 const fileContent = 'fileContent' ;
152145 let minifySpy : jasmine . Spy = null ;
@@ -166,11 +159,10 @@ describe('clean css task', () => {
166159 const callback = minifySpy . calls . mostRecent ( ) . args [ 1 ] ;
167160 callback ( null , minificationResponse ) ;
168161
169- promise . then ( ( result : string ) => {
162+ return promise . then ( ( result : string ) => {
170163 expect ( result ) . toEqual ( minificationResponse . styles ) ;
171164 expect ( cleanCssFactory . getCleanCssInstance ) . toHaveBeenCalledWith ( configFile . options ) ;
172165 expect ( minifySpy . calls . mostRecent ( ) . args [ 0 ] ) . toEqual ( fileContent ) ;
173- done ( ) ;
174166 } ) ;
175167 } ) ;
176168 } ) ;
0 commit comments