Created
June 23, 2021 13:11
-
-
Save mpontus/b1da0e688c71233ac66f32e4adf0469e to your computer and use it in GitHub Desktop.
Custom Jest matcher for AsyncIterable contents equality
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare global { | |
namespace jest { | |
interface Matchers<R> { | |
toEqualAsyncIterator<T, TReturn>(expected: AsyncIterator<T, TReturn>): R; | |
} | |
} | |
} | |
expect.extend({ | |
async toEqualAsyncIterator<T, TReturn>( | |
got: AsyncIterator<T, TReturn>, | |
expected: AsyncIterator<T, TReturn> | |
) { | |
const [expectedValues, actualValues] = await Promise.all( | |
[got, expected].map(async function collect(iter) { | |
const { value, done } = await iter.next(); | |
return [value].concat(done ? [] : await collect(iter)); | |
}) | |
); | |
expect(expectedValues).toEqual(actualValues); | |
return { | |
pass: true, | |
message: () => `Expected ${got} to equal ${expected}`, | |
}; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment