Shippable / support

Shippable SaaS customers can report issues and feature requests in this repository
100 stars 28 forks source link

My jest tests are failing on shippable, due some networks requests even on single react render #5184

Closed raelgc closed 3 years ago

raelgc commented 3 years ago

When I run my npm tests locally (with npm test -- -u) on my Ubuntu box, all the tests are green.

On shippable, they're failing due a network error. But what is odd is the failing tests is just a react component without any network request:

FAIL __tests__/unit/components/ContentReadMore.spec.js
  ● Components / ContentReadMore › matches snapshot

    Network Error

      at createError (../node_modules/axios/lib/core/createError.js:16:15)
      at XMLHttpRequest.handleError (../node_modules/axios/lib/adapters/xhr.js:83:14)
      at XMLHttpRequest.el.addEventListener.event (../node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:33:32)
      at innerInvokeEventListeners (../node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:316:27)
      at invokeEventListeners (../node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:267:3)
      at XMLHttpRequestEventTargetImpl._dispatch (../node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:214:9)
      at fireAnEvent (../node_modules/jsdom/lib/jsdom/living/helpers/events.js:17:36)
      at requestErrorSteps (../node_modules/jsdom/lib/jsdom/living/xhr-utils.js:121:3)
      at Object.dispatchError (../node_modules/jsdom/lib/jsdom/living/xhr-utils.js:51:3)
      at Request.client.on.err (../node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:675:20)
      at Request.onRequestError (../node_modules/request/request.js:877:8)

The component is a simple render:

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { isNonEmptyStr } from '../utils';

const ContentReadMore = ({ children, className, expanded }) => {
    const componentClassName = classNames({
        'content-read-more': true,
        'content-read-more--expanded': expanded,
        [className]: isNonEmptyStr(className)
    });

    return (
        <div className={componentClassName}>
            {children}
        </div>
    );
};

ContentReadMore.propTypes = {
    children: PropTypes.oneOfType([
        PropTypes.node,
        PropTypes.arrayOf(PropTypes.node)
    ]).isRequired,
    className: PropTypes.string,
    expanded: PropTypes.bool
};

ContentReadMore.defaultProps = {
    className: null,
    expanded: false
};

export default ContentReadMore;
raelgc commented 3 years ago

Locally, my version of npm is 6.13.4.

raelgc commented 3 years ago

Ok, as I have no idea why a component test was firing a network error on shippable, I added a mock and it worked:

import axios from 'axios';
// other imports here

jest.mock('axios');

const children = [<span key={1}>Lorem ipsum dolor sit amet</span>];

describe('Components / ContentReadMore', () => {

    it('displays children', () => {
        axios.get.mockResolvedValue({ data: 'foo' });
        const { baseElement } = render(
            <ContentReadMore>
                {children}
            </ContentReadMore>
        );
        const node = baseElement.querySelector('.content-read-more');
        expect(node.children.length).toBe(children.length);
    });

});