【问题标题】:Jest external library use require('jquery') but return undefinedJest 外部库使用 require('jquery') 但返回 undefined
【发布时间】:2017-12-31 16:06:00
【问题描述】:

你好,

我正在使用 Jest-Preset-Angular 和 Angular-CLI 来测试一个 Angular 4 应用程序,该应用程序使用依赖于 jQuery 的 Materialize

问题

问题是 Materialize 扩展了 jQuery,为此它调用了 require('jquery'),但是当我使用 Jest 运行测试时它返回 undefined

if (typeof(jQuery) === 'undefined') {
  var jQuery;
  // Check if require is a defined function.
  if (typeof(require) === 'function') {
    jQuery = $ = require('jquery');
  // Else use the dollar sign alias.
  } else {
    jQuery = $;
  }
}

我的实际设置

我正在像这样嘲笑 jQuery...

import * as $ from 'jquery/dist/jquery';

Object.defineProperty(window, '$', { value: $ });
Object.defineProperty(window, 'jQuery', { value: $ });

在像这样模拟 jQuery 之后导入 Materialize...

import 'materialize-css/dist/js/materialize';

我的期望

由于 Materialize 扩展了 jQuery 并且应用程序组件使用了扩展的方法,因此需要正确加载(包括 jQuery)以确保测试不会失败。

非常欢迎任何帮助,并提前感谢您的宝贵时间!

【问题讨论】:

    标签: angular jestjs


    【解决方案1】:

    我觉得很傻,但如果有人遇到同样的问题,对我来说,将 Jest-Preset-Angular 更新到 v2.0.5 并将 Jest-JUnit 更新到 v2.1.0 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2022-11-24
      • 2022-12-07
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      相关资源
      最近更新 更多