如何将CDN中的外部JS注入到Jest单元测试中? [英] How to inject external JS from CDN to Jest unit testing?

查看:302
本文介绍了如何将CDN中的外部JS注入到Jest单元测试中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用npm和webpack对应用程序进行了反应.我正在尝试向其中添加单元测试.

I have react application with npm and webpack. I'm trying to add unit testing to it.

我正在使用index.html中包含的CDN中的jQuery,而不是使用节点模块.

I'm using jQuery from CDN included in my index.html rather than using node module.

我在组件Test1中使用了jQuery,并向其中添加了单元测试用例.

I'm using jQuery in a Component Test1 to which I added Unit test cases.

现在,当我执行测试用例时,我得到了错误提示 ReferenceError: $ is not defined 我知道Jest无法解决它,因为我没有在Component中导入jQuery import.

Now, when I'm executing test cases I'm getting error saying ReferenceError: $ is not defined I understand that Jest is not able to resolve it as I didn't import jQuery import in Component.

问题是,如何让Jest知道外部JS?

Question is, How to let Jest know about external JS?

推荐答案

我们可以使用"setupFiles" 配置,以允许Jest执行开始执行测试之前所需的所有脚本/库/模块.

We can make use of "setupFiles" configuration to allow Jest to execute all scripts / libraries / modules required before start executing tests.

创建一个文件并将其添加到"setupFiles":["path/to/setup.js"],并将您的库包含在其中.

Create and add a file to "setupFiles": ["path/to/setup.js"] and include your library into that.

例如, global.jQuery = global.$ = require('path/to/jQuery')

For example, global.jQuery = global.$ = require('path/to/jQuery')

请找到我发布在Jest github存储库上的查询.

Please find my query posted on Jest github repo.

这篇关于如何将CDN中的外部JS注入到Jest单元测试中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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