FoxPro
FoxPro

Reputation: 2324

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.

Upvotes: 10

Views: 28794

Answers (1)

Estus Flask
Estus Flask

Reputation: 223204

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")

Upvotes: 6

Related Questions