Jasmine.createSpy equivalent in jest - jestjs

I am migrating from jasmine to jest in my application. I have the following line to test
JSON.parse(window.document.querySelector(SELECTOR).innerHTML)
In my test I used jasmine.
document.querySelector = jasmine.createSpy('HTML Element').and.returnValue(dummyEl)
But now with jest I get the following error
TypeError: Cannot read property 'innerHTML' of null
Can you help me?

I think you need a combination of jest.fn and .mockReturnValue.
I am not good with jest but I think it can be:
document.querySelector = jest.fn().mockReturnValue(dummyEl);
Check out the documentation here:
https://jestjs.io/docs/jest-object#jestfnimplementation
https://jestjs.io/docs/mock-function-api#mockfnmockimplementationfn

Related

Mocha test reporting Object.hasOwn is not a function

I am using mocha and sinon to test and I am using node 16.19.0
I am using Object.hasOwn in my code and the test is currently failing with:
"Object.hasOwn is not a function"
I'm not really sure why is doesn't exist in the first place.
I have tried to stub it
Sinon.stub(Object, 'hasOwn').returns({});
but I get the error:
Cannot stub non-existent property hasOwn
If I monkey patch it the issue goes away.
Object.hasOwn = Sinon.stub().returns({});
What is the correct approach here?

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.

Does Jest have a way to conditionally return a value based on input?

I'm used to do mocking (actually, faking, but, whatever) with Sinon.js by using withArgs extensively to conditionally return the fake value. And now I'm trying to migrate to Jest and I'm can't find anything similar in the mock API.
What I want:
const t = sinon.stub();
t.withArgs('foo').returns('bar');
console.log(t('foo')); // 'bar'
console.log(t('qux')); // undefined
It seems that the only way to do that in Jest is by using mockImplementation, which means that I must write the arg matcher by hand.
Is there a better way of doing this in Jest?
So far, there isn't any Jest builtin method.
There is an issue opened here and a feature proposal here.
Based on this question, there is a third party library that does something similar: https://github.com/timkindberg/jest-when

mocha: Cannot read property 'be' of undefined

I'm writing unit test for my project. But I always meet this problem
Cannot read property 'be' of undefined.
I have a test suite called model-xxx, and I wanna try each method in this model. So each method I write a sub test suite in model-xxx. like this:
enter image description here
And each method is related with mongoose, so I hope these sub suite will be async. so each method I write done() in before and after and it
but none of these suite passed. error like this:
enter image description here
and like this:
Cannot read property 'not' of undefined.
why this occurs? does this mean something wrong with my should.js? but it doesn't make sense
hope for solutions.
Chai's should is a function that needs to be called before you can use should-style assertions:
var should = require('chai').should();
See the documentation.

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