angular - 为使用 routerLink、Spectator 和 Jest 的 Angular 组件编写测试时出现“TypeError:将循环结构转换为 JSON”错误
问题描述
在为我的应用程序设置新的测试平台时,我正在尝试慢慢消除应用程序中复杂模块中的错误。我正在利用观众的路由工厂 api 创建我的测试床(https://github.com/ngneat/spectator#testing-with-routing)
describe('LoginComponent', () => {
let spectator: SpectatorRouting<LoginComponent>;
let defaultComponentSettings = {
component: LoginComponent,
componentMocks: [SessionQuery],
componentProviders: [
mockProvider(SessionService, {
credentials: { email: 'name@example.com' }
})
],
imports: [...imports, HttpClientTestingModule],
url: [new UrlSegment('/login', {})]
};
let createTargetRedirectUrlComponent = createRoutingFactory({
...defaultComponentSettings,
queryParams: {
redirectUrl: 'http://any/'
}
});
describe('with target redirectUrl', () => {
beforeEach(() => {
spectator = createTargetRedirectUrlComponent();
spectator.component.mfa_token_view$ = of(false);
spectator.detectChanges();
});
it('should have a sign up link', () => {
console.log('do we have a form yet', spectator.query('form'));
expect(spectator.query(byText('Sign up'))).toBeTruthy();
});
});
});
现在事情似乎工作正常,直到我[routerLink]
在我的模板中添加一个带有指令的锚点<a [routerLink]="['/signup']" class="auth-link">Sign up</a>
,这出于某种原因会产生以下错误:
● Test suite failed to run
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
| property 'element' -> object with constructor 'Object'
| property 'publicProviders' -> object with constructor 'Object'
| property 'RouterLink_109' -> object with constructor 'Object'
--- property 'parent' closes the circle
at stringify (<anonymous>)
at messageParent (node_modules/jest-runner/node_modules/jest-worker/build/workers/messageParent.js:34:19)
而且我真的不知道如何进一步诊断
解决方案
啊,找到答案了!
导入列表包括一个本身导入的模块RouterModule
,删除了这个修复了错误。
推荐阅读
- swig - 为什么 swig 将 python 列表无缝地转换为 std::vector 而不是 std::set?
- node.js - 如何在 Express 中使用带有路由参数的 PassportJS?
- php - 需要在 json 中使用 php json_encode 的数组
- android - Android:如何以编程方式禁用通知通道?
- android - 如何重构我的 kotlin 代码并使其更清晰
- r - 如何使用聚合来计算数据集多行的平均值?
- reactjs - React 在异步调用中调度方法
- python - 如何在 PyQt5 的弹出窗口中隐藏主窗口(父窗口)?
- javascript - 如何让事件监听使用 jquery 动态创建的 html 元素?
- internet-explorer - 如何使用 VBScript 在 Messenger 中键入消息?