简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM