2
2
const expect = require ( 'chai' ) . expect ;
3
3
const proxyquire = require ( 'proxyquire' ) ;
4
4
const sinon = require ( 'sinon' ) ;
5
- const ProcessError = require ( '../../../lib/errors' ) . ProcessError ;
5
+ const { isObservable} = require ( 'rxjs' ) ;
6
+ const { getReadableStream} = require ( '../../utils/stream' ) ;
7
+ const { ProcessError} = require ( '../../../lib/errors' ) ;
6
8
7
9
const modulePath = '../../../lib/utils/yarn' ;
8
10
9
- let yarn ;
11
+ const setup = proxies => proxyquire ( modulePath , proxies ) ;
10
12
11
13
describe ( 'Unit: yarn' , function ( ) {
12
14
let currentEnv ;
13
- let execa ;
14
15
15
16
beforeEach ( function ( ) {
16
17
currentEnv = process . env ;
17
18
process . env = { } ;
18
-
19
- execa = sinon . stub ( ) . resolves ( ) ;
20
- yarn = proxyquire ( modulePath , {
21
- execa : execa
22
- } ) ;
23
19
} ) ;
24
20
25
21
afterEach ( function ( ) {
26
22
process . env = currentEnv ;
27
23
} ) ;
28
24
29
25
it ( 'spawns yarn process with no arguments correctly' , function ( ) {
30
- const promise = yarn ( ) ;
26
+ const execa = sinon . stub ( ) . resolves ( ) ;
27
+ const yarn = setup ( { execa} ) ;
31
28
32
- return promise . then ( function ( ) {
29
+ return yarn ( ) . then ( function ( ) {
33
30
expect ( execa . calledOnce ) . to . be . true ;
34
31
expect ( execa . args [ 0 ] ) . to . be . ok ;
35
32
expect ( execa . args [ 0 ] ) . to . have . lengthOf ( 3 ) ;
@@ -38,9 +35,10 @@ describe('Unit: yarn', function () {
38
35
} ) ;
39
36
40
37
it ( 'spawns yarn process with correct arguments' , function ( ) {
41
- const promise = yarn ( [ 'cache' , 'clear' ] ) ;
38
+ const execa = sinon . stub ( ) . resolves ( ) ;
39
+ const yarn = setup ( { execa} ) ;
42
40
43
- return promise . then ( function ( ) {
41
+ return yarn ( [ 'cache' , 'clear' ] ) . then ( function ( ) {
44
42
expect ( execa . calledOnce ) . to . be . true ;
45
43
expect ( execa . args [ 0 ] ) . to . be . ok ;
46
44
expect ( execa . args [ 0 ] ) . to . have . lengthOf ( 3 ) ;
@@ -49,9 +47,10 @@ describe('Unit: yarn', function () {
49
47
} ) ;
50
48
51
49
it ( 'correctly passes through options' , function ( ) {
52
- const promise = yarn ( [ ] , { cwd : 'test' } ) ;
50
+ const execa = sinon . stub ( ) . resolves ( ) ;
51
+ const yarn = setup ( { execa} ) ;
53
52
54
- return promise . then ( function ( ) {
53
+ return yarn ( [ ] , { cwd : 'test' } ) . then ( function ( ) {
55
54
expect ( execa . calledOnce ) . to . be . true ;
56
55
expect ( execa . args [ 0 ] ) . to . be . ok ;
57
56
expect ( execa . args [ 0 ] ) . to . have . lengthOf ( 3 ) ;
@@ -61,11 +60,11 @@ describe('Unit: yarn', function () {
61
60
} ) ;
62
61
63
62
it ( 'respects process.env overrides but doesn\'t mutate process.env' , function ( ) {
64
- process . env . TESTENV = 'test' ;
65
-
66
- const promise = yarn ( [ ] , { env : { TESTENV : 'override' } } ) ;
63
+ const execa = sinon . stub ( ) . resolves ( ) ;
64
+ const yarn = setup ( { execa} ) ;
67
65
68
- return promise . then ( ( ) => {
66
+ process . env . TESTENV = 'test' ;
67
+ return yarn ( [ ] , { env : { TESTENV : 'override' } } ) . then ( ( ) => {
69
68
expect ( execa . calledOnce ) . to . be . true ;
70
69
expect ( execa . args [ 0 ] [ 2 ] ) . to . be . an ( 'object' ) ;
71
70
expect ( execa . args [ 0 ] [ 2 ] . env ) . to . be . an ( 'object' ) ;
@@ -75,8 +74,8 @@ describe('Unit: yarn', function () {
75
74
} ) ;
76
75
77
76
it ( 'fails gracefully when yarn fails' , function ( ) {
78
- execa . rejects ( new Error ( 'YARN_TO_FAST' ) ) ;
79
- yarn = proxyquire ( modulePath , { execa : execa } ) ;
77
+ const execa = sinon . stub ( ) . rejects ( new Error ( 'YARN_TO_FAST' ) ) ;
78
+ const yarn = setup ( { execa} ) ;
80
79
81
80
return yarn ( ) . then ( ( ) => {
82
81
expect ( false , 'Promise should have rejected' ) . to . be . true ;
@@ -88,105 +87,90 @@ describe('Unit: yarn', function () {
88
87
} ) ;
89
88
90
89
describe ( 'can return observables' , function ( ) {
91
- let stubs ;
92
- beforeEach ( function ( ) {
93
- stubs = {
94
- stdout : sinon . stub ( ) ,
95
- observer : {
96
- next : sinon . stub ( ) ,
97
- complete : sinon . stub ( ) ,
98
- error : sinon . stub ( )
99
- }
100
- } ;
101
-
102
- class TestClass {
103
- constructor ( fn ) {
104
- stubs . proxy = { fn : fn } ;
105
- return ( new Promise ( ( resolve , reject ) => {
106
- stubs . proxy . resolve = resolve ;
107
- stubs . proxy . reject = reject ;
108
- } ) ) ;
109
- }
110
- }
111
-
112
- const execaPromise = new Promise ( ( resolve , reject ) => {
113
- stubs . _execa = {
114
- resolve : resolve ,
115
- reject : reject
116
- } ;
90
+ it ( 'ends properly' , function ( ) {
91
+ const execa = sinon . stub ( ) . callsFake ( ( ) => {
92
+ const promise = Promise . resolve ( ) ;
93
+ promise . stdout = getReadableStream ( ) ;
94
+ return promise ;
117
95
} ) ;
118
- execaPromise . stdout = {
119
- setEncoding : ( ) => true ,
120
- on : stubs . stdout
96
+ const yarn = setup ( { execa} ) ;
97
+
98
+ const res = yarn ( [ ] , { observe : true } ) ;
99
+ expect ( isObservable ( res ) ) . to . be . true ;
100
+
101
+ const subscriber = {
102
+ next : sinon . stub ( ) ,
103
+ error : sinon . stub ( ) ,
104
+ complete : sinon . stub ( )
121
105
} ;
122
- execa . returns ( execaPromise ) ;
123
106
124
- yarn = proxyquire ( modulePath , {
125
- execa : execa ,
126
- rxjs : { Observable : TestClass }
107
+ res . subscribe ( subscriber ) ;
108
+
109
+ return res . toPromise ( ) . then ( ( ) => {
110
+ expect ( execa . calledOnce ) . to . be . true ;
111
+ expect ( subscriber . next . called ) . to . be . false ;
112
+ expect ( subscriber . error . called ) . to . be . false ;
113
+ expect ( subscriber . complete . calledOnce ) . to . be . true ;
127
114
} ) ;
128
115
} ) ;
129
116
130
- it ( 'ends properly' , function ( ) {
117
+ it ( 'ends properly (error)' , function ( ) {
118
+ const execa = sinon . stub ( ) . callsFake ( ( ) => {
119
+ const promise = Promise . reject ( new Error ( 'test error' ) ) ;
120
+ promise . stdout = getReadableStream ( ) ;
121
+ return promise ;
122
+ } ) ;
123
+ const yarn = setup ( { execa} ) ;
124
+
131
125
const res = yarn ( [ ] , { observe : true } ) ;
132
- expect ( stubs . proxy ) . to . be . an ( 'Object' ) ;
126
+ expect ( isObservable ( res ) ) . to . be . true ;
133
127
134
- stubs . proxy . fn ( stubs . observer ) ;
135
- stubs . _execa . resolve ( ) ;
136
- res . then ( ( ) => {
137
- expect ( stubs . observer . complete . calledOnce ) . to . be . true ;
138
- } ) ;
128
+ const subscriber = {
129
+ next : sinon . stub ( ) ,
130
+ error : sinon . stub ( ) ,
131
+ complete : sinon . stub ( )
132
+ } ;
139
133
140
- stubs . proxy . resolve ( ) ;
141
- return res ;
142
- } ) ;
134
+ res . subscribe ( subscriber ) ;
143
135
144
- it ( 'ends properly (error)' , function ( ) {
145
- const res = yarn ( [ ] , { observe : true } ) ;
146
- expect ( stubs . proxy ) . to . be . an ( 'Object' ) ;
147
- stubs . proxy . fn ( stubs . observer ) ;
148
- stubs . _execa . reject ( 'test' ) ;
149
-
150
- res . then ( ( ) => {
151
- expect ( stubs . observer . complete . called ) . to . be . false ;
152
- expect ( stubs . observer . error . calledOnce ) . to . be . true ;
153
- expect ( stubs . observer . error . args [ 0 ] [ 0 ] ) . to . equal ( 'test' ) ;
136
+ return res . toPromise ( ) . catch ( ( error ) => {
137
+ expect ( error . message ) . to . equal ( 'test error' ) ;
138
+ expect ( execa . calledOnce ) . to . be . true ;
139
+ expect ( subscriber . next . called ) . to . be . false ;
140
+ expect ( subscriber . error . calledOnce ) . to . be . true ;
141
+ expect ( subscriber . error . args [ 0 ] [ 0 ] ) . to . be . an . instanceOf ( ProcessError ) ;
142
+ expect ( subscriber . complete . called ) . to . be . false ;
154
143
} ) ;
155
-
156
- stubs . proxy . resolve ( ) ;
157
- return res ;
158
144
} ) ;
159
145
160
- it ( 'proxies data through' , function ( ) {
161
- yarn ( [ ] , { observe : true } ) ;
162
- expect ( stubs . proxy ) . to . be . an ( 'Object' ) ;
163
- stubs . proxy . fn ( stubs . observer ) ;
164
- expect ( stubs . stdout . calledOnce ) . to . be . true ;
165
- expect ( stubs . stdout . args [ 0 ] [ 0 ] ) . to . equal ( 'data' ) ;
166
-
167
- const onFn = stubs . stdout . args [ 0 ] [ 1 ] ;
168
- onFn ( '\n\n\n' ) ;
169
- onFn ( 'test\n' ) ;
170
- onFn ( '\nbest\n' ) ;
171
- onFn ( 'run' ) ;
172
-
173
- const next = stubs . observer . next ;
174
- expect ( next . callCount ) . to . equal ( 4 ) ;
175
- expect ( next . args [ 0 ] [ 0 ] ) . to . equal ( '\n\n' ) ;
176
- expect ( next . args [ 1 ] [ 0 ] ) . to . equal ( 'test' ) ;
177
- expect ( next . args [ 2 ] [ 0 ] ) . to . equal ( '\nbest' ) ;
178
- expect ( next . args [ 3 ] [ 0 ] ) . to . equal ( 'run' ) ;
179
- } ) ;
146
+ it ( 'passes data through' , function ( ) {
147
+ const execa = sinon . stub ( ) . callsFake ( ( ) => {
148
+ const promise = Promise . resolve ( ) ;
149
+ promise . stdout = getReadableStream ( function ( ) {
150
+ this . push ( 'test message\n' ) ;
151
+ this . push ( null ) ;
152
+ } ) ;
153
+ return promise ;
154
+ } ) ;
155
+ const yarn = setup ( { execa} ) ;
180
156
181
- it ( 'cleans up observer error' , function ( ) {
182
157
const res = yarn ( [ ] , { observe : true } ) ;
183
- expect ( stubs . proxy ) . to . be . an ( 'Object' ) ;
184
- stubs . proxy . reject ( ) ;
185
- return res . then ( ( ) => {
186
- expect ( false , 'Promise should have rejected' ) . to . be . true ;
187
- } ) . catch ( ( e ) => {
188
- expect ( e ) . to . be . ok ;
189
- expect ( e ) . to . be . instanceOf ( ProcessError ) ;
158
+ expect ( isObservable ( res ) ) . to . be . true ;
159
+
160
+ const subscriber = {
161
+ next : sinon . stub ( ) ,
162
+ error : sinon . stub ( ) ,
163
+ complete : sinon . stub ( )
164
+ } ;
165
+
166
+ res . subscribe ( subscriber ) ;
167
+
168
+ return res . toPromise ( ) . then ( ( ) => {
169
+ expect ( execa . calledOnce ) . to . be . true ;
170
+ expect ( subscriber . next . calledOnce ) . to . be . true ;
171
+ expect ( subscriber . next . calledWithExactly ( 'test message' ) ) . to . be . true ;
172
+ expect ( subscriber . error . called ) . to . be . false ;
173
+ expect ( subscriber . complete . calledOnce ) . to . be . true ;
190
174
} ) ;
191
175
} ) ;
192
176
} ) ;
0 commit comments