[object ErrorEvent] thrown
。 我看不到控制台上任何有助于找到错误代码的信息。 是否需要进行某些操作才能跟踪这些错误呢?[编辑]: 我正在运行Karma v1.70,Jasmine v2.7.0。
[object ErrorEvent] thrown
。 我看不到控制台上任何有助于找到错误代码的信息。 是否需要进行某些操作才能跟踪这些错误呢?提示:在运行测试时,只需打开 DevTools控制台,就可以找到抛出的确切错误。
作为快速修复,您可以尝试在不使用源映射的情况下运行测试:
CLI v6.0.8及以上版本
--source-map=false
CLI v6.0.x早期版本
--sourceMap=false
CLI v1.x
--sourcemaps=false
缩写
ng test -sm=false
也可能起作用
关于这个问题,这里有一个开放的问题:https://github.com/angular/angular-cli/issues/7296
更新:
我也遇到了这个问题,所以我刚刚迁移到了最新的cli,并确保package.json
中的所有软件包都已更新,同时我还完全重新安装了node_modules
,因此现在该问题已经消失了。
--sourcemaps
选项已经不存在。 - mwilson--sourceMap=false
似乎是有效的。 - skermajo尝试从终端中运行测试以获取更详细的错误信息,例如:
ng test -sm=false
it('should...')
使用
fit('should...')
现在只有在fit
前面的测试才会运行。
如果想要在运行测试后保持浏览器打开,请按照以下方式运行测试:
ng test -sm=false --single-run false
个人经历过这种错误两次。都是在调用fixture.detectChanges()时触发的。
第一次,我通过更加安全地在html文件中使用字符串插值解决了问题。
不安全示例:
<p>{{user.firstName}}</p>
更安全的示例(注意问号):
<p>{{user?.firstName}}</p>
属性绑定可能也适用于这种情况:
<p [innerText]="user?.firstName"></p>
<p>{{startDate | date: 'dd-MM-yyyy'}}</p>
.ts(模拟数据)文件(错误的):
let startDate = 'blablah';
.ts(模拟数据)文件(正确):
let startDate = '2018-01-26';
error.toString()
。我刚刚在Jasmine存储库中提交了一个问题https://github.com/jasmine/jasmine/issues/1594。只要它没有修复,您就可以在node_modules文件夹中临时修补已安装的jasmine包。在我的情况下,它是node_modules/jasmine/node_modules/lib/jasmine-core/jasmine.js
,然后更改ExceptionFormatter的实现从这里开始。if (error.name && error.message) {
message += error.name + ': ' + error.message;
} else {
message += error.toString() + ' thrown';
}
到这个
if (error.name && error.message) {
message += error.name + ': ' + error.message;
} else if (error.message) {
message += error.message;
} else {
message += error.toString() + ' thrown';
}
它有助于识别问题。
node_modules/jasmine_core/lib/jasmine_core/jasmine.js
中需要进行修补。 - Roger Garzaerror
对象及其属性。 if(error){ // message+=error; for(var propName in error) { propValue = error[propName] message+='error prop: '+propName+', value: '+propValue; } }
. 这提供了额外的信息,帮助我进行调试 - 例如error prop: filename, value: blob:http://localhost:9881/11777e3a-28d8-414e-9047-cee7d328e843
。 - Manu Chadha对我来说,这与在组件的ngOnInit
中解决承诺有关。我必须使用async
、fakeAsync
和tick,以及使用spyOn
替换异步服务。
beforeEach(async(
//... initialise testbed and component
))
beforeEach(fakeAsync(
// ... setup mocks then call:
component.ngOnInit()
tick()
fixture.detectChanges()
))
简述: 可能与路由测试有关。
我也遇到了[object ErrorEvent]
的错误。
一个小时后,将其追踪到一行代码。
this.username = this.userService.getUser(this.route.snapshot.paramMap.get('id'))[0];
this.route.snapshot.paramMap.get('id')
。0
替换它,[object ErrorEvent] thrown
就消失了。public users = [ ["admin", "First name", "Surname", etc... ] ].
所以,0
只是获取此用户,索引为 0
。
否则,在正常运行我的应用程序时,this.route.snapshot.paramMap.get('id')
在用户从我的用户表中选择要编辑的用户时进行评估。
因此,在我的 HTML 中,*ngFor="let user of users; index as i"
循环显示所有用户,然后使用 routerLink="/edit/{{i}}"
,因此您可以单击每个用户的编辑按钮,当单击时,转到例如 http://localhost:4200/edit/0
,以编辑上述管理员用户的详细信息。
每个测试用例后进行清理操作怎么样:
afterEach(() => {
TestBed.resetTestingModule();
})
对我而言,这是因为我未为我订阅的API定义错误处理程序所导致的。
我的修复只涉及添加以下处理(err)的代码块:
it('reports an error to subscribers of api calls if not authenticated', (done) => {
let obsDataFound = dataService.find(1);
obsDataFound.subscribe((app) => {
// not expecting this block to be called
done();
}, (err) => {
// This is the err block that I added that solved the problem
expect(true).toBeTruthy();
done();
});
let testRequest = httpMock.expectOne({method: 'GET'});
const mockErrorResponse = { status: 401, statusText: 'Unauthorized test status message' };
testRequest.flush(appTest, mockErrorResponse);
});
我有同样的问题。这种错误发生的一种方式是在模板中尝试访问任何 null 或 undefined 的内容。确保在这些变量上进行安全检查。
例如,当组件加载时,如果 config 未定义,则会抛出 [object: ErrorEvent]。
template.html
<div *ngIf="config.options">
.....
......
</div>
<div *ngIf="config?.options">
.....
......
</div>
it()
改为fit()
来隔离测试本身,即使只运行单个测试,错误仍然会被抛出。对于这种类型的错误,有什么调试建议吗? - Knight