Is running jQuery inside another HTML file loaded via JSDOM testable with Jest? #892
joshbrekke
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is running jQuery inside another HTML file that is loading via JSDOM testable utilizing Jest? I have an older .Net app that needs Jest testing. However, it has jQuery in the script tags, and it's complaining about the jQuery dependency. Does Jest support the ability to load the page via JSDOM and run the jQuery inside? I can't seem to find anything from anyone that has done this.
Here is my current test example I'm having issues with...
I'm trying to run a Jest test (MyPreferences.test.js) that has a jQuery ready function at the end. However, when I run the test, I keep getting the following error...
When I check p_window.window from inside MyPreferences.test.js, it does have the document. And p_window.window1 has the jQuery object. However, I don't know why it can't find the jQuery when the JSDOM loads MyPreferences.aspx from ./src/MyPreferences.js.
Any ideas to what I'm missing that would be causing the jQuery errors?
MyPreferences.aspx
<%@ Page Title="My Preferences" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="MyPreferences.aspx.cs" Inherits="RFS.MyPreferences" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
./src/MyPreferences.js
module.exports = {};
const { JSDOM } = require("jsdom");
const $ = require('jquery');
const path = require("path");
const fs = require("fs");
const html = fs.readFileSync(path.resolve(__dirname, "../../MyPreferences.aspx"));
const window = (new JSDOM(html, { runScripts: "dangerously", resources: 'usable' }).window);
const window1 = (jQuery)(new JSDOM(html, { runScripts: "dangerously", resources: 'usable' }).window);
module.exports.$ = $;
module.exports.window = window;
module.exports.window1 = window1;
./MyPreferences.test.js
let p_window;
describe("MyPreferences UI", () => {
beforeEach(() => {
p_window = require("./src/MyPreferences");
});
Beta Was this translation helpful? Give feedback.
All reactions