Cannot convert undefined or null to object at Function.keys

Viewed 3251

I'm using the matchFeature example from NodeJS but when i try to test the following error occurs in the const cv = require('opencv4nodejs'); line from the example.

test/matchFeature.test.js

● Test suite failed to run

TypeError: Cannot convert undefined or null to object at Function.keys ()

at Object. (node_modules/opencv4nodejs/lib/cv.js:63:8)
at Object. (node_modules/opencv4nodejs/lib/opencv4nodejs.js:11:79)

I think is a problem when loading the module, but i cant fix it. I'm using Jest for testing.

If i run the file with NodeJS, works without problems:

https://prnt.sc/sq41s9

And if i run test with jest, gives the error above:

https://prnt.sc/sq42mb

The images are from the file cv.js running the file and the test

EDIT: Screenshot from jest index.js (Module null)

https://prnt.sc/sq5rxl

1 Answers

I was able to solve it with a global setup script which loads the module.

// setup.js
module.exports = async () => {
    const cv = require('opencv4nodejs');
    console.log('cv.version', cv.version);
};

And in the jest Config in package.json:

"jest": {
        ...
        "testEnvironment": "node",
        "globalSetup": "./setup.js"
    }
Related