@@ -49,4 +49,70 @@ describe('asyncIteratorToStream', () => {
49
49
} )
50
50
expect ( await getChunks ( stream ) ) . toEqual ( data )
51
51
} )
52
+ describe ( 'with generators' , ( ) => {
53
+ it ( 'sync' , async ( ) => {
54
+ const expectedThis = { }
55
+ const expectedArgs = [ 'foo' , 'bar' ]
56
+ const stream = asyncIteratorToStream
57
+ . obj ( function * ( ...args ) {
58
+ // this is forwarded
59
+ expect ( this ) . toBe ( expectedThis )
60
+
61
+ // args are forwarded
62
+ expect ( args ) . toEqual ( expectedArgs )
63
+
64
+ // can yield undefined to ask for the requested size
65
+ expect ( typeof ( yield undefined ) ) . toBe ( 'number' )
66
+
67
+ // can yield a value
68
+ yield 1
69
+
70
+ // can yield a promise to wait for its resolution
71
+ expect ( yield Promise . resolve ( 'foo' ) ) . toBe ( 'foo' )
72
+ try {
73
+ yield Promise . reject ( new Error ( 'bar' ) )
74
+ expect ( false ) . toBe ( true )
75
+ } catch ( error ) {
76
+ expect ( error . message ) . toBe ( 'bar' )
77
+ }
78
+
79
+ yield 2
80
+ } )
81
+ . apply ( expectedThis , expectedArgs )
82
+
83
+ expect ( await getChunks ( stream ) ) . toEqual ( [ 1 , 2 ] )
84
+ } )
85
+ it ( 'async' , async ( ) => {
86
+ const expectedThis = { }
87
+ const expectedArgs = [ 'foo' , 'bar' ]
88
+ const stream = asyncIteratorToStream
89
+ . obj ( async function * ( ...args ) {
90
+ // this is forwarded
91
+ expect ( this ) . toBe ( expectedThis )
92
+
93
+ // args are forwarded
94
+ expect ( args ) . toEqual ( expectedArgs )
95
+
96
+ // can yield undefined to ask for the requested size
97
+ expect ( typeof ( yield undefined ) ) . toBe ( 'number' )
98
+
99
+ // can yield a value
100
+ yield 1
101
+
102
+ // promises are correctly handled
103
+ expect ( await Promise . resolve ( 'foo' ) ) . toBe ( 'foo' )
104
+ try {
105
+ yield Promise . reject ( new Error ( 'bar' ) )
106
+ expect ( false ) . toBe ( true )
107
+ } catch ( error ) {
108
+ expect ( error . message ) . toBe ( 'bar' )
109
+ }
110
+
111
+ yield 2
112
+ } )
113
+ . apply ( expectedThis , expectedArgs )
114
+
115
+ expect ( await getChunks ( stream ) ) . toEqual ( [ 1 , 2 ] )
116
+ } )
117
+ } )
52
118
} )
0 commit comments