Mocha testing difference between done() and done as paramter to function - node.js

please explain difference between done() method and done keyword passed as a parameter to a function?
it("qwerty",function(done){
------
------
done();
});
it('qwerty', function(done){
----------
----------
.expect(404, done);
})
Please explain the differences and how many times i can call done() in loop
if i am calling like 15 times i am getting an error "multiple times done() called"

In the first example, you call it explicitly. In the second one, you pass done function as a callback to expect. It checks the response status (I assume you use supertest library) and if it equals to 404, calls done function without parameter (no error). Otherwise it calls done with something like an instance of assertation error, so mocha knows that this is a failed test.
As a regular callback, done is not supposed to be called multiple times. It should be called only once, denoting the end of some action, the test in your case. If you are looking for the ability to fail the test, just throw it.

Related

Someone knows why mocha throw me error even if everything passed in tests?

I get this error even when everything still right in test
Here the part of code
And someone knows how to ignore this erros?
You are calling done() before all tests have been evaluated, in fact even before you get the result for the requested URL. You need to move the done() call inside the .end callback. I would show you how if you pasted your code. But perhaps it's already clear from this description.

jest method done() is not defined

I started using jest and I now need to test callbacks.
To know when a callback was called, the done() is supposed to be used accourding to the documentation: https://jestjs.io/docs/en/asynchronous.html
However the done() is not recognized beeing undefined and is consequently throwing this Error:
Test suite failed to run
TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
pathToErrorFile:line - error TS2304: Cannot find name 'done'.
63 done();
~~~~
//code to reproduce:
test('test', ()=>{
fkt(param, ()=>{
done();
});
});
I have setup jest with node and angular and in both projects this function does not exist.
So what I want to know is, where this function is even coming from and how I can troubleshoot it.
Note that everything else (test, describe, etc. ) is working fine done() as its exception.
done is not defined as a global var. You get it passed to the test function.
test('test', done => {
fkt(param, () => {
done();
});
});
Note that if you specify done parameter, jest will detect it and will fail the test on timeout, if the done function is not called after the test has finished.
If done() is never called, the test will fail (with timeout error), which is what you want to happen.
Then, you have to call done even if the test fails - otherwise you won't see the error.
If we want to see in the test log why it failed, we have to wrap expect in a try block and pass the error in the catch block to done. Otherwise, we end up with an opaque timeout error that doesn't show what value was received by expect(data).
See Jest - Testing Asynchronous Code

When a nodejs callback will be called an unknown number of times and you need to do something after the last one

As I understand it, the node-netstat package parses the output of the netstat command and it calls the callback I supply, once per line of data it parses.
I could do with knowing when it's made its last call, so I know to callback the function that was supplied to my function by elsewhere, but I'm not really sure how to do this..
this.myfunc = function(callback){
netstat(null, function(data){
//netstat will call this function X times. I'd like to accumulate data
});
callback( ..data from netstat.. );
}
If netstat's callback only fired once, with all the data, then I could probably have called callback at the end of function(data), but the multi-calls is confounding that. What do we do in situations like this? (Note also, it's a really prehistoric version of node: 0.10.24)
You can pass an option object to netstat(options, handler) function.
In option object, there is a done field which you can pass a callback function.
More information about option object can be found here

nodeJS callback error parameter

I'm learning node now and I'm confused about the err parameter.
I thought it's supposed to be the first argument of a callback function but I don't see it in many call back functions. Can anyone explain it to me? Thanks!
There's many different kinds of functions and callback functions in particular. The Node.js standard for callback functions is those of the form:
function(err, arg1, arg2, ...)
Where arg1 and so forth are only present if relevant but the err argument is always first. This is the reverse of a lot of historical JavaScript code where errors would be the last argument.
The Node.js method of forcing the error as the first argument even if there's no error makes ignoring errors harder, you rarely forget to declare that argument, and makes their location predictable.
Now this only applies in the case of a general-purpose callback. That is, there are occasions where calling a function will trigger a singular callback at some point in the future. You'll see them used like this:
doStuff(function(err, successValue) { ... });
There's also the style popularized by jQuery where one or more of your callbacks will be triggered depending on the outcome of the operation:
doStuff({
success: function(successValue) { ... },
error: function(err) { ... },
timeout: function() { ... }
});
Note that in this case you may have both the error and timeout callbacks being fired. You're not obligated to populate all of these, either.
The downside to this approach is the unpredictability of which ones get called and the risk of handling something twice inadvertently.
The error parameter is usually for asynchronous code.
node errors
Most asynchronous methods that accept a callback function will accept an Error object passed as the first argument to that function. If that first argument is not null and is an instance of Error, then an error occurred that should be handled.
app.get() sends get request and return an error like a 404
and you could do something like this res.status(404).render( in app.get()
Express error handling
error-handling functions have four arguments instead of three: (err, req, res, next)
The reason why some code uses err as the first parameter is because some code like fs.readFileis programmed to check if there was an error and to handle it. The author of the API specifically wrote code to check the first argument for an error and handle it.
That's why it is available to you for some methods an unavailable for other methods.
First: a callback is just a function. Different callbacks serve different purposes.
In general, a function that performs an asynchronous action and should "return" a value gets passed a callback function that will take (at least) two arguments: the first is used to pass errors (if any), the second (and following) are used to pass the value(s) that should be returned to the caller.
You noticed that net.createServer() will also take a callback function, but that function only has one argument.
That's because, in this case, the callback isn't used to pass errors and/or values. Instead, it's a function that gets called when a new connection is made to the server.
It's really a bit of a shortcut. This code:
var server = net.createServer(function(connection) {
...
});
Is short for this code:
var server = net.createServer();
server.on('connection', function(connection) {
...
});

nodejs async.forEach callback was already called

I'm using the async library to help me with my control flow. I have a collection over which I want to iterate, for each element execute 1 asynchronous task and when all are done, call the callback.
I've decided to use an async.forEach loop, on each loop I call my asynchronous task but I get an error: callback was already called, but shouldn't the callback be called only when all callbacks are called? And I even wanted to understand properly how to handle errors, it is highly probable that some task will fail and others will succeed, I don't need to know which elements fail, but I would like, how can I do this?
This is my code:
async.forEach(fonts, function(font, callback) {
ftpm_module.installOsFont(font, callback);
}, function() {
console.log("finished");
});
EDIT: the error occurs only if I pass 2 or more fonts.

Resources