Mocha test reporting Object.hasOwn is not a function - node.js

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?

Related

how to stub require function in typescript/node with sinon

I am using sinon version 9.0.3 to test a function that uses the in-built require function to get the contents of a package.json file. The contents of this package.json file continually change between each deployment so I want to stub the result of the require function.
In my src code the require function is used as follows:
const packageJSON = require('../package.json')
In order to stub this, I have tried the following code (note in my test the value of property a is all I'm checking for:
const packageJSONStub = sinon.stub(module, 'require');
packageJSONStub.withArgs('../package.json')resolves( { a : 'b' });
However, I get an error saying TypeError: Cannot stub non-existent property require.
Would anyone know the correct method for carrying out a stub of the in-built require function?
Turns out you cannot stub the require function - instead mock the file that require refers to.

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.

Jasmine.createSpy equivalent in jest

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

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: ... }));

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