question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

new EventEmitter() instanceof Object returns false when run by Jest

See original GitHub issue

Do you want to request a feature or report a bug? bug

What is the current behavior?

On node an instance of EventEmitter are instances of Object

const { EventEmitter } = require('events');
const eventEmitter = new EventEmitter();
eventEmitter instanceof Object; //true

Running the same code inside a Jest run will report that this is false.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

https://repl.it/@Aigeec/Jest-thinks-EventEmitter-is-not-an-object https://repl.it/@Aigeec/Node-thinks-EventEmitter-is-an-object

What is the expected behavior? In a Node env it should evaluate the same as Node.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system. Jest Config:

{
  "testRegex": ".*-test\\.js$",
  "testEnvironment": "node"
}

Using the repl versions: node v7.4 linux/amd64 Jest v20.0.4 node v7.4.0 linux/amd64

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jwbaycommented, Jan 18, 2018

Looks like a duplicate of #2549

1reaction
Aigeeccommented, Jan 17, 2018

In the repl.it you do typeof EventEmiter === ‘object’ but it’s a function in both environments

I think it was a little ‘e’ typeof eventEmitter === 'object' referring to the eventEmitter instance.

In either case EventEmitter instanceof Object or new EventEmitter() instanceof Object will return false in a Jest environment but true in Node.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does instanceof return false for some literals?
As you can see both typeof and instanceof are insufficient to test whether a value is a boolean, a number or a string...
Read more >
How to assert process events with Jest - Johann Pardanaud
on() , you add your listener to an EventEmitter which is not the original one and which will never be called when an...
Read more >
Node.js v19.3.0 Documentation
Creates a new CallTracker object which can be used to track if functions were called a specific number of times. The tracker.verify() must...
Read more >
A Comprehensive Guide To Error Handling In Node.js
The emitCount() function returns a new event emitter that reports both success and failure events in the asynchronous operation.
Read more >
Why is instanceof my object returning false in TypeScript?
When using a package like JSDom, you'll notice that any JavaScript executed within its instantiation gets passed a different window , and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found