Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest basics: Testing function from component

I have a basic function:

components/FirstComponent:

sayMyName = (fruit) => {
    alert("Hello, I'm " + fruit);
    return fruit;
}

When I try to test it with Jest inside FirstComponent.test.js:

import FirstComponent from '../components/FirstComponent';

describe('<FirstComponent />', () => {
    it('tests the only function', () => {
        FirstComponent.sayMyName = jest.fn();
        const value = FirstComponent.sayMyName('orange');
        expect(value).toBe('orange');
    });
});

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

Apparently I'm not importing the function to test the right way?

I was not smart enough to understand the Jest documents how to test functions from components..

Is there some simple way to import function from component and test it?

Edit: This works now using the 'react-test-renderer'

import FirstComponent from '../components/FirstComponent';
import renderer from 'react-test-renderer';

describe('<FirstComponent /> functions', () => {
        it('test the only function', () => {
            const wrapper = renderer.create(<FirstComponent />);
            const inst = wrapper.getInstance();
            expect(inst.sayMyName('orange')).toMatchSnapshot();
        });
})
like image 663
Jack M. Avatar asked Aug 03 '17 10:08

Jack M.


People also ask

How do you write a Jest test case for a function?

To create a test case in Jest we use the test() function. It takes a test name string and handler function as the first two arguments. The test() function can also be called under the alias - it() .

How do I test component render in Jest?

Step 1: You will start a new project using create-react-app so open your terminal and type. Step 2: Switch to the jest-testing folder using the following command. Step 6: Create a Button. js file and Button.

How do you test a method in React component?

There are a few ways to test React components. Broadly, they divide into two categories: Rendering component trees in a simplified test environment and asserting on their output. Running a complete app in a realistic browser environment (also known as “end-to-end” tests).


1 Answers

You have stubbed the function with one that does not return anything. FirstComponent.sayMyName = jest.fn();

To test the function, typically you can just do

// if static etc
import { sayMyName } from '../foo/bar';

describe('bar', () => {
  it('should do what I like', () => {
    expect(sayMyName('orange')).toMatchSnapshot();
  });
})

This will store the output ("orange") and assert that every time you run this test, it should return orange. If your function stops doing that or returns something else, snapshot will differ and test will fail.

the direct comparison .toBe('orange') will still be possible but the really useful thing about jest is snapshot testing so you don't need to duplicate logic and serialise/deep compare structures or jsx.

if it's a component method, you need to render it first, getInstance() and then call it.

like image 97
Dimitar Christoff Avatar answered Nov 06 '22 19:11

Dimitar Christoff