Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mock ndjsonStream or ReadableStream in a jest test case #42

Open
kanteankit opened this issue Sep 20, 2018 · 0 comments
Open

How to mock ndjsonStream or ReadableStream in a jest test case #42

kanteankit opened this issue Sep 20, 2018 · 0 comments
Labels

Comments

@kanteankit
Copy link

kanteankit commented Sep 20, 2018

Hi guys,
Thank you for this awesome library.
I was wondering how I can write a test case that involves ndjsonStream.
Taking the example from the docs and modifying it a bit:

import ndjsonStream from "can-ndjson-stream";
var users = []
var getUsers = ()=>{ return fetch("/users") }

var fetchUsersList = ()=>{
          getUsers()
            .then( ( response ) => {
               return ndjsonStream( response.body ); //ndjsonStream parses the response.body

            } ).then( ( exampleStream ) => {
                const reader = exampleStream.getReader();
                let read;
                reader.read().then( read = ( result ) => {
                if ( result.done ) {
                  return;
                }

               users.push(result.value)  // Each json gets pushed to items array
               reader.read().then( read );
           } );
       } );
}

The code above calls getUsers that returns a Promise object. This Promise is then handled by ndjsonStream. Each JSON read by ndjsonStream is then pushed to the users array.
Now, I want to write a test case using jest that creates a mock function for getUsers and then checks whether the users list gets populated or not. Something like this:

let fakeUsersList = '{"id":"abcd","title":"Random User 1"}\n{"id":"pqrs","title":"Random User 2"}'
import 'isomorphic-fetch'
getUsers = jest.fn(()=>{

     return new Promise((resolve, reject)=>{
          resolve(new Response(fakeUsersList, {
                            status: 200,
                            statusText: 'OK,
                           headers: {
                               'Content-type': 'application/x-ndjson'
                            }
                        })
                   )
           })
})

it('should populate the user list',()=>{
    fetchUsersList()
   expect(users.length).toBe(2)
})

The above code basically says, when fetchUsersList is called, instead of calling the actual getUsers, call the dummy function that responds back with a Promise object. The fake Promise object resolves with a fakeUsersList. Then we test whether the users list has been populated or not

The problem I am facing is this:
I was able to create a dummy response for my API call using a Promise object and using isomorphic-fetch to create Response object.
But, I have no way to create a dummy ReadableStream object. When I run my test case, I get the error ReadableStream is not defined, when this line gets executed
ndjsonStream( response.body)

Is there a way to create a mock/dummy of either the ndjsonStream function itself or the ReadableStream object?
Thanks in advance for any help/suggestions that you guys could provide

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants