Load jQuery into Mocha test for React App

我只是一个虾纸丫 提交于 2019-12-12 19:03:23

问题


I've created a Mocha test setup similar to this tutorial outlined here: https://github.com/jesstelford/react-testing-mocha-jsdom.

I'm wondering how I can load in jQuery into this structure. I've included it as require('jquery') test JS file, but when I call $.ajax, it gives an error saying TypeError: Attempted to wrap undefined property ajax as function. I suspect that this is because Node is not running my JavaScript inside a browser. However, I've tried using a window environment, but still no luck.

I know the traditional way is to include jQuery in HTML tags, but I'm not sure how to do that here, since I don't have an HTML file.


回答1:


This is a pattern that has worked for me for certain jquery methods in mocha, I'm not sure it'll work for ajax but give it a shot and let me know.

import jsdom from 'jsdom'
import _$ from 'jquery'

global.document = jsdom.jsdom('<!doctype html><html><body></body></html>')
global.window = global.document.defaultView
const $ = _$(global.window)


来源:https://stackoverflow.com/questions/37869307/load-jquery-into-mocha-test-for-react-app

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