jQuery require 返回一个对象而不是一个函数

问题描述

我在其中一项测试中尝试将 jQueryjsdom 结合使用,但我无法让它工作。当我不传递 $ 对象时,jQuery window 的 require 语句返回一个函数,但当我传递时,$一个对象而不是函数

我使用的库版本,

Node.JS (14.7.0)、Jest (26.6.3)、jsdom (16.6.0)、jQuery (3.6.0)。

const {JSDOM} = require("jsdom");
const {window} = new JSDOM("<!DOCTYPE html><p>Hello world</p>",{
  url: 'http://localhost/',});
const $ = require("jquery")(window);

test("jsdom and jquery are setup",() => {
  expect($("p").textContent).toBe("Hello world")
});
TypeError: $ is not a function

    at Object.<anonymous> (.../src/canary.test.js:8:10)
    at Object.asyncJestTest (.../node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)
    at .../node_modules/jest-jasmine2/build/queueRunner.js:45:12
    at new Promise (<anonymous>)
    at mapper (.../node_modules/jest-jasmine2/build/queueRunner.js:28:19)
    at .../node_modules/jest-jasmine2/build/queueRunner.js:75:41
    at processticksAndRejections (internal/process/task_queues.js:93:5)

尝试了其他 SO 帖子中的各种答案,但没有帮助。任何帮助表示赞赏:)

解决方法

我无法与 Jest 一起使用。所以,我继续使用摩卡咖啡。