Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest: TypeError: Cannot read property of undefined

Im trying to test my React class, that has import dotnetify from "dotnetify"; import. That works fine, but Jest says, that dotnetify is undefined. If i change to const dotnetify = require("dotnetify");, Jest passes test, but this is silly workaround. How to explain Jest, that dotnetify is not undefined?

Than you in advance.

like image 337
FoxPro Avatar asked May 19 '18 16:05

FoxPro


People also ask

Is dotnetify defined or undefined in jest?

That works fine, but Jest says, that dotnetify is undefined. If i change to const dotnetify = require ("dotnetify");, Jest passes test, but this is silly workaround.

What is the default import for CommonJS in jest?

This cannot be 'explained' to Jest, it's really undefined. There are several ways to handle CommonJS modules in TypeScript. As explained in this answer, there will be default import in CommonJS packge only if synthetic imports were enabled with esModuleInterop ( allowSyntheticDefaultImports) compiler option.

Is it possible to use jest from unit testing in React-Native?

we were using jest from unit testing in react-native. It was working well. We just deleted npm and installed it again and then when we tried to run unit test (npm test) we are getting following error and we are not able to run test cases.

Should jest not depend on jest-CLI?

Maybe jest should not depend on jest-cli, so that users are forced to add an explicit dependency to their project (at the correct version) if they want to use it? Including the appropriate peerDependencies should at least provide a warning of this kind of problem.


1 Answers

This cannot be 'explained' to Jest, it's really undefined.

There are several ways to handle CommonJS modules in TypeScript. As explained in this answer, there will be default import in CommonJS packge only if synthetic imports were enabled with esModuleInterop (allowSyntheticDefaultImports) compiler option.

Otherwise it should be done like:

import * as dotnetify from "dotnetify";

Or with TypeScript-specific syntax:

import dotnetify = require("dotnetify")
like image 58
Estus Flask Avatar answered Sep 28 '22 14:09

Estus Flask