简体   繁体   中英

Jest Testing with React Native and Contentful

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM