@@ -5,17 +5,39 @@ import { ExecutorContext, runExecutor, Target } from '@nrwl/devkit';
55// eslint-disable-next-line @typescript-eslint/no-var-requires
66const devkit : { runExecutor : typeof runExecutor } = require ( '@nrwl/devkit' ) ;
77
8+ enum MockFailTargets {
9+ NoSuccess = 'mock-no-success' ,
10+ Error = 'mock-error' ,
11+ }
12+
813describe ( 'Build Executor' , ( ) => {
914 let runExecutorPayloads : Target [ ] = [ ] ;
1015
11- jest . spyOn ( devkit , 'runExecutor' ) . mockImplementation ( ( target : Target ) =>
12- Promise . resolve (
13- ( async function * ( ) {
14- runExecutorPayloads . push ( target ) ;
15- yield { success : true , target } ; // yielding target for debugging purposes
16- } ) ( )
17- )
18- ) ;
16+ jest . spyOn ( devkit , 'runExecutor' ) . mockImplementation ( ( target : Target ) => {
17+ if ( target . target === MockFailTargets . NoSuccess ) {
18+ return Promise . resolve (
19+ ( async function * ( ) {
20+ runExecutorPayloads . push ( target ) ;
21+ yield { success : false , target } ; // yielding target for debugging purposes
22+ } ) ( )
23+ ) ;
24+ } else if ( target . target === MockFailTargets . Error ) {
25+ return Promise . resolve (
26+ // eslint-disable-next-line require-yield
27+ ( async function * ( ) {
28+ runExecutorPayloads . push ( target ) ;
29+ throw new Error ( 'Something went wrong' ) ;
30+ } ) ( )
31+ ) ;
32+ } else {
33+ return Promise . resolve (
34+ ( async function * ( ) {
35+ runExecutorPayloads . push ( target ) ;
36+ yield { success : true , target } ; // yielding target for debugging purposes
37+ } ) ( )
38+ ) ;
39+ }
40+ } ) ;
1941
2042 afterEach ( ( ) => {
2143 runExecutorPayloads = [ ] ;
@@ -33,18 +55,69 @@ describe('Build Executor', () => {
3355 runSequence : [ 'my-app:target1:development' , 'my-app:target2' ] ,
3456 } ;
3557 const iterable = executor ( options , context ) ;
36- await iterable . next ( ) ;
37- expect ( runExecutorPayloads . map ( ( p ) => p . target ) ) . toEqual ( [ 'target1' ] ) ;
38- await iterable . next ( ) ;
39- expect ( runExecutorPayloads . map ( ( p ) => p . target ) ) . toEqual ( [
40- 'target1' ,
41- 'target2' ,
42- ] ) ;
43- const result = await iterable . next ( ) ;
58+ let result = await iterable . next ( ) ;
59+ expect ( result . value ?. success ) . toEqual ( true ) ;
4460 expect ( runExecutorPayloads ) . toEqual ( [
4561 { project : 'my-app' , target : 'target1' , configuration : 'development' } ,
4662 { project : 'my-app' , target : 'target2' , configuration : 'production' } ,
4763 ] ) ;
64+ result = await iterable . next ( ) ;
65+ expect ( result . done ) . toEqual ( true ) ;
66+ } ) ;
67+
68+ it ( 'should stop execution if executor returned "success: false"' , async ( ) => {
69+ const context = {
70+ root : '/root' ,
71+ projectName : 'my-app' ,
72+ targetName : 'build' ,
73+ configurationName : 'production' ,
74+ } as ExecutorContext ;
75+
76+ const target = MockFailTargets . NoSuccess ;
77+
78+ const options : BuildExecutorSchema = {
79+ runSequence : [
80+ 'my-app:target1:development' ,
81+ `my-app:${ target } ` ,
82+ 'my-app:target2' ,
83+ ] ,
84+ } ;
85+ const iterable = executor ( options , context ) ;
86+ let result = await iterable . next ( ) ;
87+ expect ( result . value ?. success ) . toEqual ( false ) ;
88+ expect ( runExecutorPayloads ) . toEqual ( [
89+ { project : 'my-app' , target : 'target1' , configuration : 'development' } ,
90+ { project : 'my-app' , target : target , configuration : 'production' } ,
91+ ] ) ;
92+ result = await iterable . next ( ) ;
93+ expect ( result . done ) . toEqual ( true ) ;
94+ } ) ;
95+
96+ it ( 'should stop execution if unhandled error occurs' , async ( ) => {
97+ const context = {
98+ root : '/root' ,
99+ projectName : 'my-app' ,
100+ targetName : 'build' ,
101+ configurationName : 'production' ,
102+ } as ExecutorContext ;
103+
104+ const target = MockFailTargets . Error ;
105+
106+ const options : BuildExecutorSchema = {
107+ runSequence : [
108+ 'my-app:target1:development' ,
109+ `my-app:${ target } ` ,
110+ 'my-app:target2' ,
111+ ] ,
112+ } ;
113+ const iterable = executor ( options , context ) ;
114+ let result = await iterable . next ( ) ;
115+ expect ( result . value ?. success ) . toEqual ( false ) ;
116+ expect ( runExecutorPayloads ) . toEqual ( [
117+ { project : 'my-app' , target : 'target1' , configuration : 'development' } ,
118+ { project : 'my-app' , target : target , configuration : 'production' } ,
119+ ] ) ;
120+ result = await iterable . next ( ) ;
48121 expect ( result . done ) . toEqual ( true ) ;
49122 } ) ;
50123} ) ;
0 commit comments