Mocha tests fail when Wifi is disconnected - node.js

I have some mocha tests that complete without errors when connected to Wifi. I am running the tests using command:
./node_modules/.bin/mocha --recursive -R spec path/to/bootstrap.integration.js path/to/testfile.test.js
When I turn off the Wifi connection, the same tests fail in the before bootstrap with this error:
Uncaught Error: ENOENT: no such file or directory, open '/etc/resolv.conf'
I actually connect to the internet via a vpn and when the vpn is disconnected, but the wifi is still turned on and connected, the tests all pass. The firewall only allows outbound connections over the vpn.
Stepping through the code in the debugger, the error is thrown right after I clear out the database (mongodb).
Relevant code that gets called in the bootstrap file before:
function emptyCollection(collectionName, callback) {
debug(`emptying collection: ${collectionName}`);
models[collectionName].deleteMany({}, function(err, result) {
if (err) { // <--- Never reached when Wifi is disconnected
return callback(err);
}
debug(`emptied collection: ${collectionName}`);
return callback(null, result);
});
}
debug('emptying all collections...');
async.map(Object.keys(models), emptyCollection, function(err, results) {
if (err) { // <--- Never reached when Wifi is disconnected
return cb(err);
}
debug('emptied all collections');
return cb();
});
None of the deleteMany callbacks are ever reached when Wifi is disconnected. The async.map callback is never reached either.
I've been trying to find a way to set a breakpoint on when '/etc/resolv.conf' gets opened so I can determine which part of the code is trying to read the file, but I haven't found a way to do that.
I've been looking at this for hours, I'm all out of ideas.
Does any body have any troubleshooting advice?
[Update: I am looking for troubleshooting advice specific to the problem described above]

If your tests are dependent on wifi or any network calls then you're not writing your tests in correct manner. Read this - F.I.R.S.T principle

Related

Mongoose close connection issue

In my node js program, After opening mongo connection I trying to run certain query and finally trying to close connection.
group.find({group_name: "music"}, function (doc) {
// do your stuff
console.log(doc);
mongoose.connection.close();
});
But while I trying to see mongotop log, after closing the connection, its still show as connected
Here finananceManagement is the the database, which I tried to connect, If I restart the mongo service it will disapper from the log, help me to resolve this issue

Google Cloud Function running on nodejs10 test fails

Here are the relevant bits of my function:
// Finally send the JSON data to the browser or requestor
res.status(200).send(output);
} catch (err) {
res.status(500).send(err.message);
} finally {
await closeConnection(page, browser);
}
When I run this locally it works flawlessly and returns my output to the web browser. When I upload it to Google Cloud Functions and test it the res.status(200).send(output); line fails with this message:
Error: function execution failed. Details:
res.status is not a function
Has anyone else seen this behavior? I'm completely puzzled as to why it would work perfectly on my local machine, but fail when I run it as a cloud function.
After digging around a bunch I found the answer. Google Cloud Functions that have a 'background' trigger type do not recognize res.status. Instead they want callback:
https://cloud.google.com/functions/docs/writing/background#function_parameters
// Finally send the JSON data to the browser or requestor
callback(null, output);
} catch (err) {
callback(new Error('Failed'));
} finally {
await closeConnection(page, browser);
}
If you run your local development instance with the --signature-type flag it correctly starts up, but you can no longer test by hitting the port in a web browser:
"start": "functions-framework --target=pollenCount --signature-type=cloudevent",
Documentation on how to send mock pub/sub data into your local instance is here:
https://cloud.google.com/functions/docs/running/calling#background_functions

webpackdevserver listen callback not being called

I have webpack dev server that's always been working and now suddenly it's not. The issue is that the callback for WebpackDevServer.listen never gets called. Nothing crashes and no error is being sent the callback just never gets called. Does anyone know why this would happen? Is there some setup where the callback will be ignored?
const devServer = new WebpackDevServer(compiler, serverConfig);
// Launch WebpackDevServer.
devServer.listen(STATIC_PORT, HOST, err => {
console.log('this never gets called');
if (err) {
return console.log(err);
}
console.log(chalk.cyan('Starting the development server...\n'));
});
Restarting my computer did the trick.. So that's my answer so far. If this happens to you, try restarting pc.
If anyone else has a better explanation of why this happens and if there's a way to avoid it in the future I will mark your answer as correct.

catch never outputs the console even though I know it's failing

I have my mongodb service stopped, so I know that my front end is not connected to my DB. I am using react and express.
Upon my app starting, I want to indicate that to the user somehow the server is offline so I figured if my original get call for users fails, then the server is offline.
I'm doing a simple call:
componentDidMount () {
axios.get ('/api/users')
.then ((res) => this.setState(
{ users : res.data }
))
.catch ((error) => {
//console.error(error);
console.log('error found : offline');
});
}
But nothing happens in situation. I never get the catch call for the console. Am I going about this wrong? I'm new to backend so this is all a learning experience for me.
I was going to set a failed flag and then render a display error for the user and then retry the connection every 1500ms or something (is that bad programming?).

Catching Mocha timeouts

I'm writing a node.js web service which needs to communicate with another server. So its basically server to server communication. I don't have any previous experience of writing web services so I have very limited knowledge. For unit tests I'm using Mocha.
Now, I intend to test the behavior of my service for a particular scenario when this other server doesn't respond to my GET request and the request is actually timed out. For tests I've created a fake client and server around my web service. My web service now takes request from this fake client and then gets information from another fake server that I created which then returns the response in the expected format. To simulate timeout I don't do response.end() from my route handler. The problem is that Mocha judges it to have failed this test case.
Is there a way I could catch this intentional timeout in Mocha and the test is a success?
As mido22 suggested you should use handle the timeout generated by whatever library you use to connect. For instance, with request:
var request = require("request");
it("test", function (done) {
request("http://www.google.com:81", {
timeout: 1000
}, function (error, response, body) {
if (error && error.code === 'ETIMEDOUT') {
done(); // Got a timetout: that's what we wanted.
return;
}
// Got another error or no error at all: that's bad!
done(error || new Error("did not get a timeout"));
});
});

Resources