'Getting "You are trying to import a file after the Jest environment has been torn down" and "right-hand side of instanceof is not callable" in jest
I'm trying to test a mongodb application but jest is throwing some errors my way here's the code for the file i'm testing I inserted a dummy function: testFunct() that just returns one for sanity purposes
const User = require("./database/database.js");
const client = require("./twitch/twitch.js");
const chalk = require("chalk");
var peopleWatching = new Object();
client.on("join", (channel, username, self) => {
if (!self) {
newUser(username);
}
});
client.on("message", (channel, user, message, self) => {
var name = user["display-name"];
if (peopleWatching[name]) {
peopleWatching[name].messagesSent++;
} else {
newUser(name);
}
});
client.on("part", (channel, name, self) => {
if (peopleWatching[name]) {
data = peopleWatching[name];
updateUser(data, name);
} else {
console.log(
chalk.black.bgKeyword("darkorange")("WARN") +
" untracked user " +
chalk.underline(name) +
" left the channel"
);
}
});
async function newUser(name) {
var newUser = new User({ uName: name });
await newUser
.save()
.then((val) => {
console.log(
chalk.black.bgGreen("NEW") +
" " +
chalk.underline(name) +
" " +
chalk.green("Has been added to the database")
);
})
.catch(async (err) => {
console.log(
chalk.bgRed("ERR!") +
` ${chalk.underline(name)} already in the database`
);
newUser = await User.findOne({ uName: name });
});
if (!peopleWatching[name]) {
peopleWatching[name] = {
user: newUser,
timeJoined: Date.now() / 1000,
messagesSent: 0,
};
} else {
// This person already exists
}
}
async function updateUser(data, name) {
user = data.user;
messages = data.messagesSent;
timeWatched = (Date.now() / 1000 - data.timeJoined)/60;
user.messagesSent += messages;
user.timeWatched += timeWatched;
user.save();
peopleWatching[name] = undefined;
console.log(
chalk.black.bgCyan("UPD") +
" " +
chalk.underline(name) +
" " +
chalk.green("Has been updated")
);
}
function testFunct(){
return 1;
}
module.exports = {testFunct,updateUser,newUser}
and the code for the test. currently i'm only testing my dummy function
const {testFunct,updateUser,newUser} = require('./index.js')
const jestConfig = require('./jest.config.js')
jest.useFakeTimers()
test("First test",()=>{
expect(testFunct()).toBe(1)
})
and finally, the output from npm run test
npm run test
> [email protected] test C:\Users\brand\repos\kamiBot
> jest
PASS ./index.test.js
√ First test (2 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.188 s, estimated 2 s
Ran all test suites.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.
at BufferList.Readable (node_modules/readable-stream/lib/_stream_readable.js:179:22)
at BufferList.Duplex (node_modules/readable-stream/lib/_stream_duplex.js:67:12)
at new BufferList (node_modules/bl/bl.js:33:16)
at new MessageStream (node_modules/mongodb/lib/cmap/message_stream.js:35:21)
at new Connection (node_modules/mongodb/lib/cmap/connection.js:54:28)
C:\Users\brand\repos\kamiBot\node_modules\readable-stream\lib\_stream_readable.js:111
var isDuplex = stream instanceof Duplex;
^
TypeError: Right-hand side of 'instanceof' is not callable
at new ReadableState (C:\Users\brand\repos\kamiBot\node_modules\readable-stream\lib\_stream_readable.js:111:25)
at BufferList.Readable (C:\Users\brand\repos\kamiBot\node_modules\readable-stream\lib\_stream_readable.js:183:25)
at BufferList.Duplex (C:\Users\brand\repos\kamiBot\node_modules\readable-stream\lib\_stream_duplex.js:67:12)
at new BufferList (C:\Users\brand\repos\kamiBot\node_modules\bl\bl.js:33:16)
at new MessageStream (C:\Users\brand\repos\kamiBot\node_modules\mongodb\lib\cmap\message_stream.js:35:21)
at new Connection (C:\Users\brand\repos\kamiBot\node_modules\mongodb\lib\cmap\connection.js:54:28)
at C:\Users\brand\repos\kamiBot\node_modules\mongodb\lib\core\connection\connect.js:36:29
at callback (C:\Users\brand\repos\kamiBot\node_modules\mongodb\lib\core\connection\connect.js:280:5)
at Socket.connectHandler (C:\Users\brand\repos\kamiBot\node_modules\mongodb\lib\core\connection\connect.js:325:5)
at Object.onceWrapper (events.js:421:28)
at Socket.emit (events.js:315:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1127:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `jest`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\brand\AppData\Roaming\npm-cache\_logs\2021-03-26T13_46_29_769Z-debug.log
I made sure to read the mongoose documentation and I made sure to configure jest.config.js just like the docs said to
Solution 1:[1]
Being older and wiser, I now understand that we should avoid side-effects in unit tests. Instead, we should stub functions that have side effects (like network, database or filesystem) using a library like sinon
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Brandon PiƱa |
