3

I am trying to test my React Native Application with Jest. I am using Contentful as a CMS to hold my backend information. I am currently trying to test that I am initializing the correct client.

Here is the code I used to initialize the client:

var client = contentful.createClient({
  space: 'w20789877',  // whatever the space id is 
  accessToken: '883829200101001047474747737' // some accessToken
})

Here is the code I used to test initializing the client:

describe ('should initialize the correct client', () => {(
   it('should initialize the correct client info from contentful', () =>{
      expect(client).toEqual('w20789877', '883829200101001047474747737')
});
)};

However, I am getting an error message stating that:

Difference: Comparing two different types of values. Expected undefined but received string.

For some reason I am receiving undefined for the space and accessToken but I correctly initialize the client, as I am able to use the space later on. Even when trying to print out the space and accessToken an undefined value prints.

skyboyer
  • 22,209
  • 7
  • 57
  • 64

1 Answers1

0

These are several issues here:

  1. The toEqual matcher receives a single value parameter; you're sending 2 parameters, so effectively only the first one is being used.
  2. The client in this case is a function and you're trying to compare it to a string. Regardless of the fact that the client is not a string, in your case it's also undefined, hence the message "Expected undefined but received string". You are not testing the space or accessToken here, you are testing the client.

I'm not completely sure what you're trying to test here, but this is not specifically related to Contentful.

I'm assuming that the client initialization part is somewhere in the code which you want to unit-test (and not initialized in the test file). I suggest a test that checks that the createClient function of the contentful is being called with your expected parameters when your code is executed; there's no need to test that the client is created - that's Contentful's responsibility to make sure they return a valid client object. What's important is that you pass the correct "space" and "accessToken" parameters required for your app.

Generally speaking, external services should be mocked, and you should only test your own logic and interaction with the external services.

Example

To make it simple, lets say that the code that initializes your client looks like this:

//client.js

var contentful = require('contentful')

export default function initializeClient() {
    var client = contentful.createClient({
      space: 'w20789877',  // whatever the space id is 
      accessToken: '883829200101001047474747737' // some accessToken
    });
}

The test might look something like this:

//client.test.js

describe('contentful client', () => {
    let contentful;
    let initializeClient;

    beforeEach(() => {
        jest.mock('contentful');

        contentful = require('contentful');
        initializeClient = require('./client').default;
    });

    it('should initialize the contentful client with the correct params', async () => {
        initializeClient();
        expect(contentful.createClient).toHaveBeenCalledWith({
            space: 'w20789877',
            accessToken: '883829200101001047474747737'
        });
    });
});

Note: I didn't actually run or test the above code, but this is the general concept.

Artal
  • 8,933
  • 2
  • 27
  • 30
  • Thank you for the help! I was able to run and test the code. However, the test fails stating: `expect(jest.fn()).toHaveBeenCalledWith(expected) Expected mock function to have been called with: [{"accessToken": "883829200101001047474747737", "space": "w20789877"}] But it was not called. ` –  Jul 04 '18 at 14:10
  • As I said, this is just an example. It’s hard to tell. The test fails because the function wasn’t called. is it possible that your implementation is actually not performing the call? – Artal Jul 04 '18 at 16:43
  • Yeah, my implementation is performing the call. –  Jul 05 '18 at 00:20
  • You’re on the right direction, you need to figure out what’s the issue. Try to put some logs in the code and see if the client object is the mock from your test, see that it even gets to the initialization code and there’s no other problem that’s stopping it from reaching the client initialization, for example - maybe something else needs to be mocked – Artal Jul 05 '18 at 06:25
  • I think createClient may need to be mocked??How would I go about doing that?/seeing if that is the issue –  Jul 05 '18 at 13:57
  • Using `jest.mock('contentful')` mocks the whole module, but it's possible that it fails to recognize the `createClient` function for some reason (you can verify that by logging the mock that you get from `require('contentful')` in `beforeEach`). If necessary you can pass a function to `jest,mock` which returns a mock object with specifically mocked functions. Another option is to mock a specific function in the mock object, for example: `contentful.createClient = jest.fn();` – Artal Jul 08 '18 at 19:07