11const EventEmitter = require ( 'events' ) . EventEmitter
2- const expect = require ( 'chai' ) . expect
2+ const chai = require ( 'chai' )
33const fs = require ( 'fs-extra' )
44const path = require ( 'path' )
55const snapshot = require ( 'snap-shot-it' )
6+ const sinon = require ( 'sinon' )
7+ const Bluebird = require ( 'bluebird' )
8+ const sinonChai = require ( 'sinon-chai' )
9+
10+ chai . use ( sinonChai )
11+ const { expect } = chai
612
713const preprocessor = require ( '../../index' )
814
@@ -12,48 +18,91 @@ describe('webpack preprocessor - e2e', () => {
1218 let file
1319 let filePath
1420
15- beforeEach ( ( ) => {
21+ beforeEach ( async ( ) => {
1622 const originalFilePath = path . join ( __dirname , '..' , 'fixtures' , 'example_spec.js' )
1723
1824 filePath = path . join ( __dirname , '..' , '_test-output' , 'example_spec.js' )
1925
2026 preprocessor . __reset ( )
21- fs . removeSync ( path . join ( __dirname , '_test-output' ) )
22- fs . outputFileSync ( filePath , '' )
23- fs . copyFileSync ( originalFilePath , filePath )
27+ await fs . remove ( path . join ( __dirname , '_test-output' ) )
28+ await fs . outputFile ( filePath , '' )
29+ await fs . copyFile ( originalFilePath , filePath )
2430
2531 file = Object . assign ( new EventEmitter ( ) , {
2632 filePath,
2733 outputPath,
2834 } )
2935 } )
3036
37+ afterEach ( async ( ) => {
38+ if ( file . shouldWatch ) {
39+ await new Promise ( ( resolve ) => {
40+ file . emit ( 'close' , resolve )
41+ } )
42+ }
43+ } )
44+
3145 it ( 'correctly preprocesses the file' , ( ) => {
3246 return preprocessor ( ) ( file ) . then ( ( ) => {
3347 snapshot ( fs . readFileSync ( outputPath ) . toString ( ) )
3448 } )
3549 } )
3650
37- it ( 'allows attaching catch later on syntax error without triggering unhandled rejection' , ( done ) => {
51+ it ( 'allows attaching catch later on syntax error without triggering unhandled rejection' , async ( ) => {
3852 process . on ( 'unhandledRejection' , ( err ) => {
3953 // eslint-disable-next-line no-console
4054 console . error ( 'Unhandled Rejection:' , err . stack )
41- done ( 'Should not have trigger unhandled rejection' )
55+ throw new Error ( 'Should not have trigger unhandled rejection' )
4256 } )
4357
4458 file . shouldWatch = true
4559
46- preprocessor ( ) ( file ) . then ( ( ) => {
47- fs . outputFileSync ( filePath , '{' )
60+ await preprocessor ( ) ( file )
61+ await fs . outputFile ( filePath , '{' )
4862
63+ await new Promise ( ( resolve ) => {
4964 setTimeout ( ( ) => {
5065 preprocessor ( ) ( file )
5166 . catch ( ( err ) => {
5267 expect ( err . stack ) . to . include ( 'Unexpected token' )
53- file . emit ( 'close' )
54- done ( )
68+ resolve ( )
5569 } )
5670 } , 1000 )
5771 } )
5872 } )
73+
74+ it ( 'triggers rerun on syntax error' , async ( ) => {
75+ const _emit = sinon . spy ( file , 'emit' )
76+
77+ file . shouldWatch = true
78+
79+ await preprocessor ( ) ( file )
80+
81+ _emit . resetHistory ( )
82+
83+ await fs . outputFile ( filePath , '{' )
84+
85+ await retry ( ( ) => expect ( _emit ) . calledWith ( 'rerun' ) )
86+ } )
5987} )
88+
89+ function retry ( fn , timeout = 1000 ) {
90+ let timedOut = false
91+
92+ setTimeout ( ( ) => timedOut = true , timeout )
93+ const tryFn = ( ) => {
94+ return Bluebird . try ( ( ) => {
95+ return fn ( )
96+ } )
97+
98+ . catch ( ( err ) => {
99+ if ( timedOut ) {
100+ throw err
101+ }
102+
103+ return Bluebird . delay ( 100 ) . then ( ( ) => tryFn ( ) )
104+ } )
105+ }
106+
107+ return tryFn ( )
108+ }
0 commit comments