【发布时间】: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 对jQuery 和jQuery-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