1
- import type { CompilerBuildResults , Compiler , Config , DevServer , E2EProcessEnv , OutputTargetWww , Testing , TestingRunOptions } from '@stencil/core/internal' ;
1
+ import type {
2
+ CompilerBuildResults ,
3
+ Compiler ,
4
+ CompilerWatcher ,
5
+ Config ,
6
+ DevServer ,
7
+ E2EProcessEnv ,
8
+ OutputTargetWww ,
9
+ Testing ,
10
+ TestingRunOptions ,
11
+ } from '@stencil/core/internal' ;
2
12
import { getAppScriptUrl , getAppStyleUrl } from './testing-utils' ;
3
13
import { hasError } from '@utils' ;
4
14
import { runJest } from './jest/jest-runner' ;
@@ -20,11 +30,14 @@ export const createTesting = async (config: Config): Promise<Testing> => {
20
30
let doScreenshots = false ;
21
31
let passed = false ;
22
32
let env : E2EProcessEnv ;
33
+ let compilerWatcher : CompilerWatcher = null ;
23
34
const msg : string [ ] = [ ] ;
24
35
25
36
try {
26
37
if ( ! opts . spec && ! opts . e2e ) {
27
- config . logger . error ( `Testing requires either the --spec or --e2e command line flags, or both. For example, to run unit tests, use the command: stencil test --spec` ) ;
38
+ config . logger . error (
39
+ `Testing requires either the --spec or --e2e command line flags, or both. For example, to run unit tests, use the command: stencil test --spec` ,
40
+ ) ;
28
41
return false ;
29
42
}
30
43
@@ -40,7 +53,7 @@ export const createTesting = async (config: Config): Promise<Testing> => {
40
53
env . __STENCIL_SPEC_TESTS__ = 'true' ;
41
54
}
42
55
43
- config . logger . info ( config . logger . magenta ( `testing ${ msg . join ( ' and ' ) } files` ) ) ;
56
+ config . logger . info ( config . logger . magenta ( `testing ${ msg . join ( ' and ' ) } files${ config . watch ? ' (watch)' : '' } ` ) ) ;
44
57
45
58
doScreenshots = ! ! ( opts . e2e && opts . screenshot ) ;
46
59
if ( doScreenshots ) {
@@ -64,20 +77,37 @@ export const createTesting = async (config: Config): Promise<Testing> => {
64
77
} ) ;
65
78
66
79
const doBuild = ! ( config . flags && config . flags . build === false ) ;
80
+ if ( doBuild && config . watch ) {
81
+ compilerWatcher = await compiler . createWatcher ( ) ;
82
+ }
83
+
67
84
if ( doBuild ) {
68
- buildTask = compiler . build ( ) ;
85
+ if ( compilerWatcher ) {
86
+ buildTask = new Promise ( resolve => {
87
+ const removeListener = compilerWatcher . on ( 'buildFinish' , buildResults => {
88
+ removeListener ( ) ;
89
+ resolve ( buildResults ) ;
90
+ } ) ;
91
+ } ) ;
92
+ compilerWatcher . start ( ) ;
93
+ } else {
94
+ buildTask = compiler . build ( ) ;
95
+ }
69
96
}
70
97
71
98
config . devServer . openBrowser = false ;
72
99
config . devServer . gzip = false ;
73
100
config . devServer . reloadStrategy = null ;
74
101
75
- const startupResults = await Promise . all ( [ start ( config . devServer , config . logger ) , startPuppeteerBrowser ( config ) ] ) ;
102
+ const startupResults = await Promise . all ( [
103
+ start ( config . devServer , config . logger ) ,
104
+ startPuppeteerBrowser ( config ) ,
105
+ ] ) ;
76
106
77
107
devServer = startupResults [ 0 ] ;
78
108
puppeteerBrowser = startupResults [ 1 ] ;
79
109
80
- if ( doBuild ) {
110
+ if ( buildTask ) {
81
111
const results = await buildTask ;
82
112
if ( ! results || ( ! config . watch && hasError ( results && results . diagnostics ) ) ) {
83
113
await destroy ( ) ;
@@ -111,6 +141,9 @@ export const createTesting = async (config: Config): Promise<Testing> => {
111
141
passed = await runJest ( config , env ) ;
112
142
}
113
143
config . logger . info ( '' ) ;
144
+ if ( compilerWatcher ) {
145
+ await compilerWatcher . close ( ) ;
146
+ }
114
147
} catch ( e ) {
115
148
config . logger . error ( e ) ;
116
149
}
@@ -170,5 +203,9 @@ function setupTestingConfig(config: Config) {
170
203
}
171
204
} ) ;
172
205
206
+ if ( config . flags . args . includes ( '--watchAll' ) ) {
207
+ config . watch = true ;
208
+ }
209
+
173
210
return config ;
174
211
}
0 commit comments