【问题标题】:Problems to run any Jest tests with jQuery and jQuery UI使用 jQuery 和 jQuery UI 运行任何 Jest 测试的问题
【发布时间】:2019-08-03 07:46:57
【问题描述】:

所以我有一个名为 Angular-Slickgrid 的开源库,它还没有测试,我正在尝试使用 Jest,但真的很难使用它。该库是旧 jQuery 数据网格库 (SlickGrid) 的包装器,它也使用 jQuery UI。我想我已经部​​分解决了 jQuery 问题(甚至不确定),但 jQuery UI 仍然抱怨。另请注意,我是 Jest 和 Angular 单元测试的新手,但我真的希望它能够正常工作并使我的 lib 更安全。

您可以在 GitHub 上看到我为尝试使用我的开源库实现 Jest 所做的所有代码更改的提交。提交是here。如果这更容易,请随意创建 PR。我使用以前版本的 Jest (23.6.0) 而不是最新版本,因为我在使用最新版本时遇到了其他类型的问题。

这是我目前遇到的错误

FAIL  src/app/modules/angular-slickgrid/components/angular-slickgrid.component.spec.ts
Test suite failed to run 
TypeError: Cannot read property 'ui' of undefined
  at node_modules/jquery-ui-dist/jquery-ui.js:18:10
  at Object.<anonymous>.$.ui (node_modules/jquery-ui-dist/jquery-ui.js:14:3)
  at Object.<anonymous> (node_modules/jquery-ui-dist/jquery-ui.js:16:2)
  at Object.<anonymous> (src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts:11193:1)
  at Object.<anonymous> (src/app/modules/angular-slickgrid/components/angular-slickgrid.component.spec.ts:7:37)

我尝试使用unmock('jquery')unmock('jquery-ui'),但这似乎没有帮助。这是失败的测试

jest.unmock('jquery');
jest.unmock('jquery-ui');
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { AngularSlickgridComponent } from './angular-slickgrid.component';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AngularSlickgridComponent
      ],
      providers: [],
      imports: [RouterTestingModule]
    }).compileComponents();
  }));

  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AngularSlickgridComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));
});

还有我的jest.config.js

module.exports = {
  globals: {
    'ts-jest': {
      tsConfigFile: './src/tsconfig.spec.json',
    },
    __TRANSFORM_HTML__: true,
  },
  testMatch: ['**/__tests__/**/*.+(ts|js)', '**/+(*.)+(spec|test).+(ts|js)'],
  setupFiles: ['<rootDir>/test-env.ts'],
  setupTestFrameworkScriptFile: '<rootDir>/node_modules/@angular-builders/jest/src/jest-config/setup.js',
  transform: {
    '^.+\\.(ts|html)$': '<rootDir>/node_modules/jest-preset-angular/preprocessor.js',
  },
  transformIgnorePatterns: ['node_modules/(?!@ngrx)'],
  moduleDirectories: [
    "node_modules",
    "src/app",
  ],
  collectCoverage: true,
  moduleFileExtensions: [
    'ts',
    'json',
    'js'
  ],
  testResultsProcessor: 'jest-sonar-reporter',
  moduleNameMapper: {
    "app/(.*)": "<rootDir>/src/app/$1",
    "@common/(.*)": "<rootDir>/src/app/common/$1",
  }
};

最后是一个为 Jest 全局导入 jQuery 的测试设置

import jQuery from 'jquery';
declare var window: any;
declare var global: any;
window.$ = window.jQuery = jQuery;
global.$ = global.jQuery = jQuery;

我希望完成的是至少测试我的 Angular 服务和组件创建,这将是一个好的开始,但我无法通过 jQuery 和 jQuery UI 问题,即使我不想测试任何的核心库(SlickGrid),既不是 jQuery,也不是 jQuery UI。

编辑

感谢@brian-lives-outdoors 对jQueryjQuery-UI 的回答,我走得更远了。现在我有另一个小问题,@Inject() 直接在Constructor 中使用(即将配置传递给我的组件库),我不知道如何解决它,如果有人知道如何请帮助。

constructor(
  private elm: ElementRef,
  // ... more Services import
  //
  @Inject('config') private forRootConfig: GridOption
) {}

错误是

StaticInjectorError(DynamicTestModule)[config]:
  StaticInjectorError(Platform: core)[config]:
    NullInjectorError: No provider for config!

at NullInjector.get (../packages/core/src/di/injector.ts:43:13)
at resolveToken (../packages/core/src/di/injector.ts:346:20)
...

回答上一个编辑问题

我找到了修复Constructor@Inject() 的方法,我可以在beforeEach 中使用overrideComponent() 进行修复,如下所示

beforeEach(async(() => {
  TestBed.configureTestingModule({
    declarations: [
      AngularSlickgridComponent,
      SlickPaginationComponent
    ],
    providers: [
      // ... all Services
    ],
    imports: [
      RouterTestingModule,
      TranslateModule.forRoot()
    ]
  })
  // THIS LINE is for the @Inject('config')
  .overrideComponent(AngularSlickgridComponent, {
    set: { providers: [{ provide: 'config', useValue: {} }] },
  })
  .compileComponents();
}));

最后我现在可以说我有 Jest 运行了!!!

【问题讨论】:

  • 还有一个问题。你为什么包括jQuery。等 Angular.json 文件中的脚本,同时使用相同文件的模块导入?
  • 我非常简要地查看了您的存储库,并且您已正确完成所有操作。我同意,我的回答不是很有帮助。我会删除它,直到我可以提供进一步的帮助
  • 为了提供一些概述,在尝试使用 Jest 之前,一切都使用 import,包括 jQuery 和 jQuery-UI,但我在某处读到 Jest 与 require 配合得更好,所以我改变了它们,但是我宁愿将它们保留为import,Jest 现在可能会更好。我真的只是想让这件事继续下去,但我尝试了很多事情,但运气不佳

标签: jquery angular jestjs slickgrid angular-test


【解决方案1】:

问题是 jQuery 是这样导入的:

import jQuery from 'jquery';

...它无法正常工作并导致jQuery 成为undefined

因为jQuery 被导入为undefined,所以全局$ 设置为undefined,当jQuery UI 尝试加载时会引发错误。


这个问题很奇怪,因为导入 jQuery 的语法出现了很多,甚至在官方 TypeScript 文档中作为 import 语法的示例。


无论如何,您都可以通过使用以下语法导入 jQuery 来解决问题:

import * as jQuery from 'jquery';


将您的 test-env.ts 更改为:

import * as jQuery from 'jquery';
declare var window: any;
declare var global: any;
window.$ = window.jQuery = jQuery;
global.$ = global.jQuery = jQuery;

...将angular-slickgrid.components.ts 的顶部更改为:

// import 3rd party vendor libs
// only import the necessary core lib, each will be imported on demand when enabled (via require)
import 'jquery-ui-dist/jquery-ui';
import 'slickgrid/lib/jquery.event.drag-2.3.0';
import 'slickgrid/slick.core';
import 'slickgrid/slick.grid';
import 'slickgrid/slick.dataview';

// ...then everything else...
import { AfterViewInit, Component, ElementRef, EventEmitter, Inject, Injectable, Input, Output, OnDestroy, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { GlobalGridOptions } from './../global-grid-options';
// ...

...并将您的 angular-slickgrid.component.spec.ts 更改为:

import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { AngularSlickgridComponent } from './angular-slickgrid.component';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AngularSlickgridComponent
      ],
      providers: [],
      imports: [RouterTestingModule]
    }).compileComponents();
  }));

  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AngularSlickgridComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

  it(`should have as title 'Angular SlickGrid Demo'`, async(() => {
    const fixture = TestBed.createComponent(AngularSlickgridComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('Angular SlickGrid Demo');
  }));
});

...这将使您摆脱最初的 jQuery 错误。

【讨论】:

  • 啊,太棒了,非常感谢。我在构造函数中还有一个@Inject 的小问题,如果你碰巧知道如何解决这个问题,那就太棒了。尽管如此,我仍然会在一天结束之前接受你的回答。非常感谢
  • @ghiscoding 与jQuery 问题已解决我收到Tempate parse errors: Can't bind to 'dataView' since it isn't a known property of 'slick-pagination'。我不确定我还能提供多少帮助,因为我对 React 比较熟悉,并且对较新的 Angular 的东西没有做太多。
  • 是的,没关系,我问的是你是否已经知道答案,但你回答了我原来的问题也没关系。对于您看到的其他错误,我提供了必要的服务,并且我更进一步,它在这个commit 中。我只是被 @Inject() 阻止了,对此进行更多研究,可能是一个单独的 SO 问题。
【解决方案2】:

在你的jest.config.js这一行添加:

setupFiles: ['<rootDir>/src/test-env.js']

将您的 test-env.ts 更改为:

import * as jQuery from 'jquery';
window.$ = window.jQuery = jQuery;
global.$ = global.jQuery = jQuery;

import 'jquery-ui/ui/version';
import 'jquery-ui/ui/plugin';
import 'jquery-ui/ui/widget';
import 'jquery-ui/ui/widgets/mouse';

【讨论】:

  • 如果你能解释你的答案,而不是仅仅发布一些代码,那就太好了。谢谢!
猜你喜欢
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 2020-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多