11const output = require ( '../src/output' ) ;
2- const deasync = require ( 'deasync' ) ;
32const webpack = require ( 'webpack' ) ;
43const FriendlyErrorsWebpackPlugin = require ( '../src/friendly-errors-plugin' ) ;
54const MemoryFileSystem = require ( 'memory-fs' ) ;
65
7- const syncWebpack = deasync ( function ( config , fn ) {
6+ const webpackPromise = function ( config , ... globalPlugins ) {
87 const compiler = webpack ( config ) ;
98 compiler . outputFileSystem = new MemoryFileSystem ( ) ;
10- compiler . run ( fn ) ;
11- return compiler ;
12- } ) ;
9+ globalPlugins . forEach ( p => compiler . apply ( p ) ) ;
10+
11+ return new Promise ( ( resolve , reject ) => {
12+ compiler . run ( err => {
13+ if ( err ) {
14+ reject ( err )
15+ }
16+ resolve ( )
17+ } ) ;
18+ } ) ;
19+ } ;
1320
14- // Applys plugin directly to compiler to support `MultiCompiler` tests.
15- const syncWebpackWithPlugin = deasync ( function ( config , fn ) {
16- const compiler = webpack ( config ) ;
17- compiler . outputFileSystem = new MemoryFileSystem ( ) ;
18- compiler . apply ( new FriendlyErrorsWebpackPlugin ( ) ) ;
19- compiler . run ( fn ) ;
20- return compiler ;
21- } ) ;
21+ async function executeAndGetLogs ( fixture , ...globalPlugins ) {
22+ try {
23+ output . capture ( ) ;
24+ await webpackPromise ( require ( fixture ) , ...globalPlugins ) ;
25+ return output . capturedMessages ;
26+ } finally {
27+ output . endCapture ( )
28+ }
29+ }
2230
23- it ( 'integration : success' , ( ) => {
31+ it ( 'integration : success' , async ( ) => {
2432
25- const logs = output . captureLogs ( ( ) => {
26- syncWebpack ( require ( './fixtures/success/webpack.config' ) ) ;
27- } ) ;
33+ const logs = await executeAndGetLogs ( './fixtures/success/webpack.config' )
2834
2935 expect ( logs . join ( '\n' ) ) . toMatch ( / D O N E C o m p i l e d s u c c e s s f u l l y i n ( .\d * ) m s / ) ;
3036} ) ;
3137
38+ it ( 'integration : module-errors' , async ( ) => {
3239
33- it ( 'integration : module-errors' , ( ) => {
34-
35- const logs = output . captureLogs ( ( ) => {
36- syncWebpack ( require ( './fixtures/module-errors/webpack.config.js' ) ) ;
37- } ) ;
40+ const logs = await executeAndGetLogs ( './fixtures/module-errors/webpack.config.js' ) ;
3841
3942 expect ( logs ) . toEqual ( [
4043 ' ERROR Failed to compile with 2 errors' ,
@@ -48,11 +51,9 @@ it('integration : module-errors', () => {
4851 ] ) ;
4952} ) ;
5053
51- it ( 'integration : should display eslint warnings' , ( ) => {
54+ it ( 'integration : should display eslint warnings' , async ( ) => {
5255
53- const logs = output . captureLogs ( ( ) => {
54- syncWebpack ( require ( './fixtures/eslint-warnings/webpack.config.js' ) ) ;
55- } ) ;
56+ const logs = await executeAndGetLogs ( './fixtures/eslint-warnings/webpack.config.js' ) ;
5657
5758 expect ( logs ) . toEqual ( [
5859 ' WARNING Compiled with 1 warnings' ,
@@ -71,11 +72,9 @@ it('integration : should display eslint warnings', () => {
7172 ] ) ;
7273} ) ;
7374
74- it ( 'integration : babel syntax error' , ( ) => {
75+ it ( 'integration : babel syntax error' , async ( ) => {
7576
76- const logs = output . captureLogs ( ( ) => {
77- syncWebpack ( require ( './fixtures/babel-syntax/webpack.config' ) ) ;
78- } ) ;
77+ const logs = await executeAndGetLogs ( './fixtures/babel-syntax/webpack.config' ) ;
7978
8079 expect ( logs ) . toEqual ( [
8180 ' ERROR Failed to compile with 1 errors' ,
@@ -94,20 +93,18 @@ it('integration : babel syntax error', () => {
9493 ] ) ;
9594} ) ;
9695
97- it ( 'integration : webpack multi compiler : success' , ( ) => {
96+ it ( 'integration : webpack multi compiler : success' , async ( ) => {
9897
99- const logs = output . captureLogs ( ( ) => {
100- syncWebpackWithPlugin ( require ( './fixtures/multi-compiler-success/webpack.config' ) ) ;
101- } ) ;
98+ // We apply the plugin directly to the compiler when targeting multi-compiler
99+ const logs = await executeAndGetLogs ( './fixtures/multi-compiler-success/webpack.config' , new FriendlyErrorsWebpackPlugin ( ) ) ;
102100
103101 expect ( logs . join ( '\n' ) ) . toMatch ( / D O N E C o m p i l e d s u c c e s s f u l l y i n ( .\d * ) m s / )
104102} ) ;
105103
106- it ( 'integration : webpack multi compiler : module-errors' , ( ) => {
104+ it ( 'integration : webpack multi compiler : module-errors' , async ( ) => {
107105
108- const logs = output . captureLogs ( ( ) => {
109- syncWebpackWithPlugin ( require ( './fixtures/multi-compiler-module-errors/webpack.config' ) ) ;
110- } ) ;
106+ // We apply the plugin directly to the compiler when targeting multi-compiler
107+ const logs = await executeAndGetLogs ( './fixtures/multi-compiler-module-errors/webpack.config' , new FriendlyErrorsWebpackPlugin ( ) ) ;
111108
112109 expect ( logs ) . toEqual ( [
113110 ' ERROR Failed to compile with 2 errors' ,
0 commit comments