[Windows] Jest Teardown Unhandled Error. MongoNetworkError: read ECONNRESET
See original GitHub issueHi there,
I’m trying to run a typescript + jest + mongodb-memory-server test environment but I’m getting the following error at teardown:
events.js:189
throw err; // Unhandled 'error' event
^
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({ MongoNetworkError: read ECONNRESET
at Socket.<anonymous> (C:\Plataforma\Estrutura\Final\node_modules\mongodb\lib\core\connection\connection.js:321:24)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read',
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} })
at Connection.emit (events.js:187:17)
at Socket.<anonymous> (C:\Plataforma\Estrutura\Final\node_modules\mongodb\lib\core\connection\connection.js:321:10)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
error Command failed with exit code 1.
Here are my config files:
//jest.config.js
module.exports = {
globalSetup: './setup',
globalTeardown: './teardown.js',
testEnvironment: './mongo-environment.js',
transform: {
'^.+\\.jsx?$': 'babel-jest',
'^.+\\.ts?$': 'ts-jest',
},
roots: ['<rootDir>/test'],
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
preset: 'ts-jest',
};
//setup.js
const path = require('path');
const fs = require('fs');
const MongodbMemoryServer = require('mongodb-memory-server');
const globalConfigPath = path.join(__dirname, 'globalConfig.json');
const mongod = new MongodbMemoryServer.default({
instance: {
dbName: 'StructureGenerator',
debug: true,
},
binary: {
version: '3.2.18',
}
});
module.exports = async function () {
const mongoConfig = {
mongoDBName: 'StructureGenerator',
mongoUri: await mongod.getConnectionString(),
};
// Write global config to disk because all tests run in different contexts.
fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
console.log('Config is written');
// Set reference to mongod in order to close the server during teardown.
global.MONGOD = mongod;
process.env.MONGO_URL = mongoConfig.mongoUri;
};
//teardown.js
module.exports = async function () {
await global.MONGOD.stop();
};
//mongo-environment.js
const NodeEnvironment = require('jest-environment-node');
const path = require('path');
const fs = require('fs');
const globalConfigPath = path.join(__dirname, 'globalConfig.json');
module.exports = class MongoEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}
async setup() {
console.log('Setup MongoDB Test Environment');
await super.setup();
const globalConfig = JSON.parse(fs.readFileSync(globalConfigPath, 'utf-8'));
this.global.MONGO_URI = globalConfig.mongoUri;
this.global.MONGO_DB_NAME = globalConfig.mongoDBName;
}
async teardown() {
console.log('Teardown MongoDB Test Environment');
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
};
I tried to find help on google but I didn’t manage to find any similar issues. I’m not sure if the problem is related to this package or some of the other ones, but I’m hoping someone can shed me some light on this issue.
Thanks
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9
Top Results From Across the Web
Issue at teardown with Jest + mongodb-memory-server
I'm trying to run a typescript + jest + mongodb-memory-server test environment but I'm getting the following error at teardown:
Read more >What does “Error: read ECONNRESET” mean? - Quora
It means that your application dropped it's TCP connection. For example, if you lost your wifi signal while running your app you would...
Read more >NodeJS + MongoDB Error: read ECONNRESET Issue
I know my network is little unstable, but i want to make my mongo driver retry forever how can i make that change?...
Read more >Npm test - M220JS: MongoDB for Javascript Developers
When I run npm test -t projection, it fails, below is the error message. ... read property 'close' of undefined 17 | 18...
Read more >Setup and Teardown - Jest
If you have some work you need to do repeatedly for many tests, you can use beforeEach and afterEach hooks. For example, let's...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I fix it here by disconnecting Mongoose in every test file and after all tests run:
Full example: https://github.com/DiegoVictor/omnistack-8/blob/master/api/__tests__/integration/developer.test.js
I discovered it after enable the debug option. After the tests has been finished the shell showed there were still opened connections.
I have also this same error. Turns out it could be an issue on Windows machines only (we tested locally on Windows and OSX). I forked this repo and tried the tests and there are a few tests failing on Windows with the same message:
Test Code with debug log