@@ -5,17 +5,39 @@ import { ExecutorContext, runExecutor, Target } from '@nrwl/devkit';
5
5
// eslint-disable-next-line @typescript-eslint/no-var-requires
6
6
const devkit : { runExecutor : typeof runExecutor } = require ( '@nrwl/devkit' ) ;
7
7
8
+ enum MockFailTargets {
9
+ NoSuccess = 'mock-no-success' ,
10
+ Error = 'mock-error' ,
11
+ }
12
+
8
13
describe ( 'Build Executor' , ( ) => {
9
14
let runExecutorPayloads : Target [ ] = [ ] ;
10
15
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
+ } ) ;
19
41
20
42
afterEach ( ( ) => {
21
43
runExecutorPayloads = [ ] ;
@@ -33,18 +55,69 @@ describe('Build Executor', () => {
33
55
runSequence : [ 'my-app:target1:development' , 'my-app:target2' ] ,
34
56
} ;
35
57
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 ) ;
44
60
expect ( runExecutorPayloads ) . toEqual ( [
45
61
{ project : 'my-app' , target : 'target1' , configuration : 'development' } ,
46
62
{ project : 'my-app' , target : 'target2' , configuration : 'production' } ,
47
63
] ) ;
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 ( ) ;
48
121
expect ( result . done ) . toEqual ( true ) ;
49
122
} ) ;
50
123
} ) ;
0 commit comments