Skip to content

Commit 6bdfc54

Browse files
committed
chore: add more tests
1 parent beb675d commit 6bdfc54

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/index.spec.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,70 @@ describe('asyncIteratorToStream', () => {
4949
})
5050
expect(await getChunks(stream)).toEqual(data)
5151
})
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+
})
52118
})

0 commit comments

Comments
 (0)