How do I change what is printed when a Jest test throws an Error? - jestjs

Basically, quite a few of my tests use some autogenerated code. And the autogenerated code often throws an Error with a meaningless message - but it has some other fields on it that are quite meaningful.
By default, when a test in Jest throws an Error, jest seems to print the error message. I'd like to add a different handler for a particular subclass of Error that prints the more meaningful text. This will help me determine why my tests are failing faster.
Any ideas would be great!

Related

mutation fails on initial test run with jest runner

i'm using strykerjs 5.6.0 with jest runner and react-testing-library. When I run the coverage with jest, all my test pass correctly, but when i run the mutation command (pointing to the same jest config file) i'm getting an error on initial test run because one test is getting a different value than the one i'm getting with the jest command in the coverage.
So, in thispicture it can be seen that the test is not getting the same value, this parseJSONString is a custom method to parse string type props to its js type (due to kill mutants i had to add this), its implementation is this, and it looks like the mutation is returning the fallback instead of the actual value of the array received in the coverage run.
The Stryker config is the following, any ideas? the jest version is the 26.6.3 also. I guess the issue must be related with the react testing library, but i do not understand what could be going on...
I could solve it using the waitFor hook, i don't know why, but stryker needs one render more than jest to get the test case properly mounted.

Error mocking a "non-strict" legacy module on Jest

I have some really messy non-strict-compliant legacy code on javascript, running just fine on NodeJs 12, and I'm trying to abstract it away and test the overlaying, new layers of code using Jest/Mocks.
But when I try to run the tests I receive the following error:
Test suite failed to run
SyntaxError: /legacy-path/messy_legacy_code.js: Legacy octal literals are not allowed in strict mode (557:66)
at Parser._raise (node_modules/#babel/parser/src/parser/error.js:60:45)
I'm trying to mock it away first thing on my test code, but still get this error. It seems that Jest is trying to parse it with Babel; it really won't find any compliant code there... It just runs on Node, nothing else.
I already tried mocking the legacy code itself and also tried making a container to "abstract" it away and mocking the container. But it seems Jest still tries to read every bit of noncompliant code behind it.
My modern.test.js code looks like this:
jest.mock('../../../../legacy-path/messy-container')
const { ModernLayer } = require('../../../../modern-path/modern-module');
Any ideas on how I cant completely block Jest from trying to read this noncompliant code and just mock it away?
jest.mock('...') performs auto-mock, unless a mock from __mocks__ is used. It tries to process module exports and fails on syntax error.
The module should be manually mocked instead:
jest.mock('../../../../legacy-path/messy-container', () => ({ ModernLayer: ... }));

Nice Error Messages for "no viable alternative at input '<EOF>'" in ANTLR4

I want to show more beautiful error message to my users.
For example if someone types integer i= the error message no viable alternative at input '<EOF>' appears. That's totally fine and predictable due to my grammar rules but I'm figuring out ways to improve those messages. If the = is missing in the example above the message changes to mismatched input '<EOF>' expecting '='. Again predictable but I can do more stuff on things like this in my code than on a general input error.
Should I catch them in the code and try to evaluate which cases is meant? Or is there a better way to handle this?
Typically you'd create your own error listener and add it to the parser where you can deal with the error yourself. For that remove any existing error listeners (one for the console is automatically registered by default), by calling parser.removeErrorListeners();. Define an own error listener class derived from BaseErrorListener and add an instance of that to your parser via parser.addErrorListener(yourListener);. You can see an example of such a custom error listener in the ANTLR runtime (search for XPathLexerErrorListener). Override the syntaxError method and use the provided info to generate your own error message. There is already a message passed in to this method (in addition to other stuff like line + char position, exception etc.), which you cannot customize as it comes directly from generated code. So best is probably to leave that alone and start from scratch (the passed in exception is your best option you have).

How to handle error in Given/When/Then statements and AfterScenario hook if error pops in both blocks

I got a scenario
#walk
Given a man is standing
When he starts moving
Then he covers 5 meters
[BeforeScenario("walk")]
public void BeforeWalkScenario()
{
SetEnvironmentForWalk();
}
[AfterScenario("walk")]
public void AfetrWalkScenario()
{
ClearWalk();
}
In my test, execution is failing on 'When' statement and error is produced. Also, since I have a AfterScenario as well, it also get's executed after the error.
Unfortunately, my test is again failing in AfterScenario block. Due to this, in test report only AfterScenario error is noted and reported. How can I make sure my testresult report also shows the reason for failure on When statement.
Help would be appreciated. New to BDD. Thanks!
In ScenarioContext.TestError you get the last thrown exception from your bindings.
You can check this in your hooks.
See http://www.specflow.org/documentation/ScenarioContext/

Unit tests with chai and Eshint are not passing

Hope you can help, recently moved from jshint to eslint and Im looking to see how to get my tests to pass wihtout making changes;
Take for exmaple the following test
expect(ctrl.screens).to.not.be.undefined;
eshint complains with the following expection;
error Expected an assignment or function call and instead saw an expression no-unused-expressions
Changing the test to;
expect(ctrl.screens).to.not.be.undefined();
Gives the following error:
TypeError: '[object Object]' is not a function (evaluating 'expect(ctrl.screens).to.not.be.undefined()')
Any ideas what approach to take here? All the tests pass when I remove eshint from the task list so I need for some way to clean up these tests.
J
Testing for undefined is tricky. There are some alternative ways to do it. I normally check for typeof(variable) === 'undefined.
Example:
expect(typeof(ctrl.screens)).to.not.equal('undefined');

Resources