Error: anticipated {} as a valid function

Encountering an unfamiliar error while using Mocha with Chai for the first time has thrown me off guard. Having prior experience only with Jasmine, I'm struggling to pinpoint what mistake I might be making.

My objective is simple - ensuring that my constructor doesn't cause any errors. However, even after simplifying the code and arriving at this snippet, I still face the same perplexing issue:

class MyLib {
  constructor() {}
}

export default MyLib;

Below is the content of the test file I am working with:

import chai from 'chai';
let { expect } = chai;

import MyLib from '../index';

describe('mylib-js', () => {
  describe('constructor', () => {
    it('should work', () => {
      expect(new MyLib()).to.not.throw();
    })
  });
});

This represents the output generated by Mocha:

d:\path\to\mylib-js>mocha --require babel-core/register

  mylib-js
    constructor
      1) should work

  0 passing (23ms)
  1 failing

  1) mylib-js constructor should work:
     AssertionError: expected {} to be a function
      at Assertion.an (d:\path\to\mylib-js\node_modules\chai\lib\chai\core\assertions.js:169:10)

      ... // Truncated additional error details

The presence of {} in the error message confuses me. What is this indicating or trying to convey?

Answer №1

When the exception message indicates that new MyLib is not a function but rather an object,

You can adjust it to work seamlessly with the throw assertion by doing something like this:

expect( () => new MyLib).not.to.throw()

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

Capture audio using Node.js

Our current setup utilizes Electron to capture the voices of both the speaker (the individual using the Electron application) and the counterpart (the person on the other end of the conversation). While we can easily use "Naudiodon" to record the speaker ...

Stryker score caused the Jenkins build to fail

Is there a way to configure the Jenkins pipeline so that it fails when the stryker score is below X? This is the stryker configuration: config.set({ mutator: "javascript", mutate: [...], testRunner: "jest", jest: { projectType: "n ...

Certain browsers may not trigger the onsubmit event of an HTML form when a hidden submit button and readonly input are involved

Currently, I am in the process of creating a form that should trigger the onsubmit event when the "Enter" key on the keyboard is pressed. However, I have encountered an issue where the event does not fire on certain browsers, depending on the form fields p ...

Add every WebSocket message to a div element in VUE3

Currently, I am facing an issue while attempting to display each trade from Binances Websocket Stream in my VUE3 component. The challenge I'm encountering is that only one line gets rendered and it keeps updating continuously. This is not the desired ...

Displaying specific data points

I am encountering an issue where I want to select multiple values and have each selected value displayed. However, when I make a selection, I only see one value from a single box. I do not wish to use append because it keeps adding onto the existing valu ...

If the number exceeds 1, then proceed with this action

I currently have a variable called countTicked, which holds an integer representing the number of relatedBoxes present on the page. I am in need of an if statement that will perform certain actions when the value stored in countTicked exceeds 1. if (!$(c ...

How to stop an AngularJS controller from running when the element is hidden

I came across this code snippet .state('fooState', { url: '/foo', templateUrl: 'path/to/bar.html', controller:'parentController' }) // Here's the content of bar.html <div class="sample"> ...

Is it possible for node modules to access and utilize protractor parameters?

Is it feasible for a node module to access a protractor parameter? I am in need of defining a parameter in my protractor conf.js file and then running a specific section of the shared node module js file across 5 projects. For instance, in my conf.js: ...

Choose Your Date of Birth from the Drop-Down Menu with ng-options

I've been working on creating a dropdown for Date of Birth using ng-options. I've managed to set it up for day and month, but I'm encountering an issue with the year dropdown. The problem is that the years are displayed in reverse order, sta ...

Utilizing Font Awesome icons within Chart.js labels

I can't seem to display font awesome symbols as labels in my chart.js 1. I have already added fontawesome's css file 2. I have selected the symbols from this link 3. I have updated the chart.js options to set pointLabels.fontFamily to FontAwes ...

Utilize the MaterialUI DataGrid to showcase nested object values in a visually appealing

Hey there, fellow coders! I'm currently working on fetching data from an API and displaying it in a data grid using React.js. Here's the format of the data I'm receiving from the API: {data: Array(200)} data : (200) [{…}, {…}, {…}, { ...

Tips for aligning a dropdown button with the other elements in your navbar

I followed the code outline from a tutorial on We3schools, but I'm having an issue with the button not aligning correctly with the navbar. https://i.sstatic.net/fSRIT.png I attempted to adjust the code for proper alignment before publishing, but was ...

How can I dynamically insert table rows using React JS?

Looking to enhance the registration form design in react js by optimizing the code. Currently, the approach involves using numerous tr and td within the form. Seeking to improve efficiency by incorporating the javascript map function to have each row con ...

Ways to convert all keys to uppercase in an array of objects?

Is there a way to capitalize the first letter of every key in an array of objects? I attempted to achieve this with the code below, but it's not working as expected. Any suggestions or corrections are appreciated. #current code function capitalizeO ...

What is the best way to retrieve a list of unchecked checkboxes in a Razor Page model?

Currently, I am working on a razor page using .NET Core 7. I have encountered an issue where I am unable to pass values of 1, 2, and 3 for unchecked checkboxes from the HTML page to the page model in the post method. When the user clicks the submit button ...

Having trouble implementing pagination for news-api in Vue.js2?

I am currently working on implementing a basic pagination feature in my Vue component specifically for the newsapi.org API. While my initial API call in the created hook is functioning as expected, I am encountering difficulties navigating to different pa ...

Steps for creating a functional counter in AngularJS

I am attempting to create a counter in AngularJS that will be used for some other purpose once it is working with a variable. However, I am facing an issue where the variable is not updating as expected. Since this will eventually become a more complex com ...

Utilize a Chrome Content Script to intercept jQuery delegated event handlers and take control

After developing a Chrome extension that intercepts form submissions in specific circumstances, I encountered an issue with a particular website utilizing jQuery's delegate function. My extension is built with raw JavaScript, excluding jQuery to prev ...

The 'controllerLogin' argument is undefined and not a function in AngularJs, causing an error during Karma Testing

Hello Brilliant Developers, I'm currently diving into the world of AngularJs and running into some bumps with basic testing, resulting in pesky errors. I've scoured the web for solutions to no avail. Any assistance you can provide would be great ...

Easier method for displaying array elements as list items in JavaScript

Working on exercises from a manual that required printing array items into list elements. Here is the code provided: <!doctype html> <html lang="en> <head> <title>Temperatures</title> <meta charset="utf-8"> &l ...