mjackson / expect

Write better assertions
MIT License
2.29k stars 117 forks source link

expect(...).toExist does not work #248

Closed rohan-paul closed 6 years ago

rohan-paul commented 6 years ago

Am facing this failing test issue in my app (FilterItems component).

Here's my simple FilterItems component.

import React, { Component } from 'react';

class FilterItems extends React.Component {
  render () {
    return (
      <div className="filter-items">
        {this.props.data.map(item => (<div className="filter-item">{item.name}</div>))}
      </div>

    )
  }
}

export default FilterItems;

And here's the test I am running on it, which fails - with the error " TypeError: (0 , _expect2.default)(...).toExist is not a function at Context. (src/tests/FilterItems.test.js:57453:40)"

My FilterItems.test.js file below

import React from 'react';
import expect from 'expect';
import FilterItems from '../components/FilterItems';
import ReactTestUtils from 'react-dom/test-utils'; // ES6

describe('FilterItems', () => {
  it('should exist', () => {
    expect(FilterItems).toBeTruthy();
  });
});          

describe('FilterItems', function () {
  it('loads without problems', function () {
    var filterItems = ReactTestUtils.renderIntoDocument(<FilterItems
      data={ [1, 2, 3] }
      />);

  expect(filterItems).toExist();
  });
});

However, if I use toBeTruthy() instead of toExist(), the test run is successful

ljharb commented 6 years ago

What version of expect are you using? v21+ is managed by jest.

rohan-paul commented 6 years ago

Yeah I was using v23+ of expect, moving test suites to jest for my app then. And there its working all good..

viksok commented 5 years ago

if you want to use toExist() you'd probably have to use shallow to render your component.

ljharb commented 5 years ago

enzyme wrappers always exist, so i don’t think that’s the issue here.