Using node's assert module how can I test the message of an error?
throw new Error('Email is required!');
I'm using assert.throws to check if an error was thrown:
assert.throws(myFunction, Error);
But this does not provide the ability to check the message.
You can pass a regular expression as the second argument.
assert.throws(myFunction, /Email is required/);
You can assert a full error object, including the message without using regular expressions:
assert.throws(myFunction, new Error("Email is required"));
This way it also asserts the correct error name (class).
Related
I am using nodejs and I return my response as follows:
res.status(503).json({"error": {"retry": true}}).end();
In the UI I see
However this is not what I want. I need {"retry": true} be part of the error attribute. Is there a way I can do it?
This is how you create custom errors in javascript:
const err = new Error("some message")
err.retry = true;
In the response you can just attach the err variable to the error prop like this:
res.status(503).json({ error: err }).end()
EDIT
I believe the default props for an Error object are name, message and stack.
I am unit testing a User model. The user name shouldn't be empty. The unit test is the following:
it('should reject no name', async () => {
name = '';
expect(await User.create(payload())).toThrow();
});
when payload has all the data except with empty name, the unit test throw the following error:
SequelizeValidationError: Validation error: Invalid validator function: unique,
Validation error: Invalid validator function: msg,
Validation error: column "cell" does not exist
However the assertion here .toThrow is not right and the test case still fails. Tried toBeUndefined with no avail. What is the correct assertion for this type of error?
Assuming User.create returns a Promise that rejects with the thrown error:
await expect(User.create(payload())).rejects.toThrow(); // SUCCESS
Note that toThrow was fixed for promises with PR 4884 so if you are using an older version of Jest (before 22.0.0) you will need to use something like toEqual:
await expect(User.create(payload())).rejects.toEqual(expect.any(Error)); // SUCCESS
you need to do this.
await expect( User.create(payload())).to.be.rejectedWith(Error)
Now, test will pass if user.create throws error, & fail if it didn't throw the error.
I'm using constructor functions with prototypical inheritance to build a small library. I'm trying to test that one of the methods throws an error.
Sample code:
function Thing() {
this.x = 'ok'
}
Thing.prototype.trySomething = function() {
throw new Error('test error')
}
const thing = new Thing()
tap.throws(function() { throw new Error('before')}) // this is fine
tap.throws(thing.trySomething()) // urgh
tap.throws(function() { throw new Error('after')}) // never runs
Looks like tap is just throwing the error from trySomething. Running the tests gives me the following results:
ok 1 - expected to throw
not ok 2 - test error
And test 3 doesn't run.
I'm using node-tap (https://github.com/tapjs/node-tap).
t.throws(fn, [expectedError], message, extra)
Expect the function to throw an error. If an expected error is provided, then also verify that the thrown error matches the expected error.
but the same thing seems to happen with native assert.
Any idea what is going on here and how to write a passing test?
The tap.throws assert function accepts a fn/function
Just do this
tap.throws(thing.trySomething, {})
remove the (), it will work fine
I tried to check if a mandatory request parameter present with
if (blank(param("some_parameter_name"))) {
// throw SomeException
}
and it failed with NPE because:
in HTTPSupport#blank(String ... names) there is a call to if(Util.blank(param(name)))
when calling param(name) it returns RequestUtils.param(name);
in RequestUtils.param(name) it fails when calling if(name.equals("id")) because name parameter is null. Should I open an issue for that ?
This is an incorrect use of the API
Please, use like this:
if (blank("param1", "param2",...)) {
// throw SomeException
}
In other words, the method blank() expects names of parameters to check.
Check out docs: HttpSupport.html#blank
I'm trying to use the "readCollection" function, but I get an Error:
The "options" parameter must be of type "object". Actual type is:
"function".
my code:
docDbClient.readCollection(docUrl, function (err, collection) {
console.log(collection);
}, function(err){
console.log(err);
});
The docUrl var is equal to my document._self path.
This code was working before, but for some reason it not anymore without me making any changes.
From what the error says the 'option' parameter needs to be an object instead of a function, but from what I read on the documentation the 'option' parameter is optional which I don't have it with in my function.
I'm also getting the same error when I use the replaceDocument function.
http://azure.github.io/azure-documentdb-node/DocumentClient.html#readCollection
The problem is that you have an error handling function in your parameter list, which causes it to think that the second parameter is options. Remove the error handling function, and add code inside your main handler to process it if err does not come back null.