jest-chain icon indicating copy to clipboard operation
jest-chain copied to clipboard

Jest 25 causes `TypeError: Cannot read property 'message' of undefined`

Open markwoon opened this issue 5 years ago • 8 comments

Bug

  • package version: 1.1.5
  • node version: 12.14.1
  • npm (or yarn) version: yarn 1.21.1

Relevant code or config

import React from 'react';
import {render} from '@testing-library/react';

function Test({}) {
  return <a>error</a>;
}

describe('Link - broken', () => {
  test('missing props', () => {
    const {asFragment} = render(<Test />);

    const result = asFragment().firstChild;
    console.log(result.textContent);
    expect(result)
      .toHaveTextContent('error')
      .not.toHaveAttribute('href');
  });
});

What you did: What happened (please provide anything you think will help):

This code fails after updating from Jest 24 to Jest 25 with: TypeError: Cannot read property 'message' of undefined

markwoon avatar Feb 03 '20 22:02 markwoon

The problem comes from this line https://github.com/mattphillips/jest-chain/blob/master/src/chain.js#L21, because it always treats error as a match error, and when it isn't it it hides the actual (a TypeError for example).

@markwoon, I think jest-chain itself doesn't produce your error, to see the actual error, disable jest-chain :+1:

I have opened a PR which fixes it #24

tonivj5 avatar Mar 03 '20 19:03 tonivj5

I can confirm @tonivj5's analysis. Disabling jest-chain produced the real error.

markwoon avatar Mar 04 '20 01:03 markwoon

I'm having this same issue using enzyme-matchers for jest, however disabling it doesn't produce any errors.

This works:

expect(SUT.find(".class")).not.toHaveClassName("yes")
expect(SUT.find(".class")).toHaveClassName("no")

This doesn't:

expect(SUT.find(".class"))
        .not.toHaveClassName("yes")
        .toHaveClassName("no")

The error says property 'toHaveClassName doesn't exist on type void.

jeffreymeng avatar Mar 10 '20 05:03 jeffreymeng

I can confirm @tonivj5's analysis as well

nemosmithasf avatar Jul 30 '20 19:07 nemosmithasf

Any update regarding this? I used to use this package before upgrading to Jest 25 and would like to continue using it

imjordanxd avatar Aug 18 '20 22:08 imjordanxd

At the moment, the best solution to this issue is to modify the source code to reflect the changes made in @tonivj5's PR (#24) and run patch-package.

RA80533 avatar Sep 14 '20 13:09 RA80533

@imjordanxd did you figure out a solution or workaround for this?

diegoaguilar avatar Apr 09 '21 04:04 diegoaguilar

@diegoaguilar no unfortunately not. Seems the PR to fix this is still open.

imjordanxd avatar Apr 09 '21 04:04 imjordanxd

Sorry for the delay in coming to this, but should be fixed in: https://www.npmjs.com/package/jest-chain/v/1.1.6

mattphillips avatar Sep 28 '22 16:09 mattphillips