开玩笑:TypeError:无法读取未定义的属性 [英] Jest: TypeError: Cannot read property of undefined

查看:212
本文介绍了开玩笑:TypeError:无法读取未定义的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试我的具有import dotnetify from "dotnetify";导入的React类.效果很好,但杰斯特(Jest)说,dotnetify是未定义的.如果我更改为const dotnetify = require("dotnetify");,Jest将通过测试,但这是愚蠢的解决方法.如何解释Jest,dotnetify不是未定义的?

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?

比你提前

推荐答案

Jest无法对此进行解释",它实际上是未定义的.

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

有几种方法可以处理TypeScript中的CommonJS模块.如此答案中所述,只有使用esModuleInterop(allowSyntheticDefaultImports)编译器选项.

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

或使用TypeScript专用语法:

Or with TypeScript-specific syntax:

import dotnetify = require("dotnetify")

这篇关于开玩笑:TypeError:无法读取未定义的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆