Enzyme examination: Error - anticipate(...).find was not recognized as a function

What is the reason for .find not being recognized as a function in the code snippet below?

import React from 'react';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json';
import { AuthorizedRoutesJest } from './AuthorizedRoutes';

// Components
import {
  Main
} from '../../components';

const wrapper = shallow(<AuthorizedRoutesJest />);

describe('<AuthorizedRoutes /> component', () => {
  it('should render', () => {
    const tree = toJson(wrapper);
    expect(tree).toMatchSnapshot();
    expect(wrapper).toHaveLength(1);
  });

  it('should contain a Main component', () => {
    expect(wrapper).find(Main).toHaveLength(1);
  });
});

Summary of all failing tests FAIL client/containers/Routes/AuthorizedRoutes.test.js

AuthorizedRoutes component › should contain a Main component

TypeError: expect(...).find is not a function

Answer №1

I had been using the .find method incorrectly

Here is a demonstration of how to correctly utilize find:

it('should include a ConnectedRouter component', () => {
  expect(wrapper.find(ConnectedRouter)).toHaveLength(1);
});

it('should contain a Switch component', () => {
  expect(wrapper.find(Switch)).toHaveLength(1);
});

it('should have 7 Route components', () => {
  expect(wrapper.find(Route)).toHaveLength(7);
});

Answer №2

For those looking to locate a component with particular testID properties, here is an additional tip:

it('should display the date when the message was sent', () => {
   expect(chatBubbleComponent.findWhere((node) => node.prop('testID') === 'chat_sent_at')).toHaveLength(1);
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting: Potential Reasons Why Registering Slash Commands with Options is Not Functioning Properly in Discord.js

While working on a Discord bot, I encountered an issue while trying to register a slash command with options for the ban command. The error message I received was: throw new DiscordAPIError(data, res.status, request); ^ DiscordAPIError: ...

Problems with select tag change events

I encountered an issue with select tag onChange events. When I select a value from the select tag, it should display in a textbox that is declared in the script. It works perfectly when I remove the class "input" from the select tag, but I prefer not to re ...

Reactjs error: Invariant Violation - Two different nodes with the matching `data-reactid` of .0.5

I recently encountered a problem while working with Reactjs and the "contentEditable" or "edit" mode of html5. <div contenteditable="true"> <p data-reactid=".0.5">Reactjs</p> </div> Whenever I press Enter or Shift Enter to create ...

How to troubleshoot Path Intellisense in VScode when using jsconfig.json for Next.js

In the structure of my project, it looks like this: jsconfig.json next.config.json components |_atoms |_Button.jsx |_Button.module.scss |_... |_... ... Within the jsconfig.json file, I have specified the following settings: { ...

Difficulty of combining forEach with findById in mongoose

I need to create a Node route that adds properties to objects and pushes them onto an array declared outside a forEach loop. I have noticed that while the array appears to be filled with data when I log it within the loop, it somehow becomes empty when I r ...

Obtain the dimensions (width and height) of a collection of images using Angular and TypeScript

Currently, I am facing an issue with my image upload functionality. My goal is to retrieve the width and height of each image before uploading them. However, I've encountered a problem where my function only provides the dimensions for the first image ...

"Is there a way to retrieve the props that have been passed down to a

I am looking to have custom props created in the root layer of my React app: import React from 'react' import App, { Container } from 'next/app' export default class MyApp extends App { static async getInitialProps({ Component, rout ...

How to customize the arrow color of an expanded parent ExpansionPanel in material-ui

Currently facing a challenge in customizing material-ui themes to achieve the desired functionality. The goal is to have the expansion panels display a different arrow color when expanded for improved visibility. However, this customization should be appl ...

What is the best way to generate conditional test scenarios with Protractor for testing?

Currently, there are certain test cases that I need to run only under specific conditions. it ('user can successfully log in', function() { if(siteAllowsLogin) { ..... } The problem with the above approach is that even when sitesNo ...

Capture and handle JavaScript errors within iframes specified with the srcDoc attribute

My current project involves creating a React component that can render any HTML/JavaScript content within an iframe using the srcDoc attribute. The challenge I am facing is implementing an error handling system to display a message instead of the iframe ...

The getUserMedia() function fails to work properly when being loaded through an Ajax request

When the script navigator.getUserMedia() is executed through regular browser loading (not ajax), it works perfectly: <script> if(navigator.getUserMedia) { navigator.getUserMedia({audio: true}, startUserMedia, function(e) { __ ...

Is it possible to use HTML elements to trigger actions in order to display or conceal divs using JavaScript?

Beginner - I am currently working on a webpage using TinyMCE wysiwyg and I would like to implement a functionality where I can display or hide certain divs upon clicking a link/button. Unfortunately, due to the structure of the page, it seems that I ca ...

Transforming varied JavaScript objects into a serial form

In my application, there is a concept of an interface along with multiple objects that implement this interface in various ways. These objects are created using different factory methods, with the potential for more factories to be added as the application ...

Having trouble handling empty or undefined objects during unit testing with Jest?

As a beginner in unit testing, I am currently testing the length of a navigation bar. The code snippet below shows the nav bar component within a class: <AppBar className={classes.appBar} position="static"> <Toolbar className={classes.too ...

Another option instead of sending back a JavaScript tag in the Ajax response

After making an ajax request in my code, I receive some HTML that contains specific elements requiring custom event handling. For example: <div> <!--some html--> <button id='specialbutton'></button> </div> <scr ...

Are there any distinctions between these two compact React components?

Let's compare these two components: function App1 () { return <button onClick={() => null}>Click me</button> } function App2 () { const fn = () => null; return <button onClick={fn}>Click me</button> } The on ...

Using ngFor in Angular 2-5 without the need for a div container wrapping

Using ngFor in a div to display an object containing HTML from an array collection. However, I want the object with the HTML node (HTMLElement) to be displayed without being wrapped in a div as specified by the ngFor Directive. Below is my HTML code snipp ...

Does expressJS use the express() function as a global function?

Can anyone confirm if the express() function used in the second statement is a global function? I have searched my project folder but couldn't find its declaration. var express = require('express'); var app = express(); var fs = require("fs ...

Please enter a numerical value into the input field in a JavaScript form

<script> function loop() { var input = document.getElementById('inputId').value; for (var i = 0; i < input; i++) { var result = document.getElementById('outputDiv').innerHTML ...

It appears that the lodash throttle function is not being invoked

I recently started utilizing lodash's throttle function to control the number of calls made to an API. However, I am facing difficulties as the call doesn't seem to be triggered successfully. Below is a simplified version of what I have attempte ...