Jest: TypeError: Cannot read property of undefined

倖福魔咒の 提交于 2019-12-23 15:44:41

问题


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 import dotnetify = require("dotnetify");, Jest passes test, but this is silly workaround. How to explain Jest, that dotnetify is not undefined?

Than you in advance.


回答1:


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:

import dotnetify = require("dotnetify")


来源:https://stackoverflow.com/questions/50427196/jest-typeerror-cannot-read-property-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!