简体   繁体   English

使用React Native和Contentful进行Jest测试

[英]Jest Testing with React Native and Contentful

I am trying to test my React Native Application with Jest. 我试图用Jest测试我的React Native Application。 I am using Contentful as a CMS to hold my backend information. 我使用Contentful作为CMS来保存我的后端信息。 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. 由于某种原因,我收到空间和accessToken未定义但我正确初始化客户端,因为我以后可以使用该空间。 Even when trying to print out the space and accessToken an undefined value prints. 即使在尝试打印出空间和accessToken时,也会打印出未定义的值。

These are several issues here: 这里有几个问题:

  1. The toEqual matcher receives a single value parameter; toEqual匹配器接收单个值参数; you're sending 2 parameters, so effectively only the first one is being used. 您正在发送2个参数,因此只有第一个参数被有效使用。
  2. The client in this case is a function and you're trying to compare it to a string. 在这种情况下, client是一个函数,您正在尝试将其与字符串进行比较。 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". 无论客户端不是字符串,在您的情况下它也是undefined ,因此消息“预期未定义但接收到字符串”。 You are not testing the space or accessToken here, you are testing the client. 你没有在这里测试空间或accessToken,你正在测试客户端。

I'm not completely sure what you're trying to test here, but this is not specifically related to Contentful. 我不完全确定你在这里要测试的是什么,但这与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; 我建议一个测试,检查在执行代码时是否使用您期望的参数调用contentfulcreateClient函数; 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. 没有必要测试客户端是否已创建 - Contentful负责确保它们返回有效的客户端对象。 What's important is that you pass the correct "space" and "accessToken" parameters required for your app. 重要的是您传递了应用程序所需的正确“space”和“accessToken”参数。

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. 注意 :我实际上并没有运行或测试上面的代码,但这是一般概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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