问题标签 [jasmine-matchers]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
682 浏览

javascript - 如何在全球范围内向茉莉花添加自定义匹配器?

我需要替换jasmine.addMatchers1.3 版中的功能。当前的 API 允许将匹配器添加到describe块中,但我希望能够在任何地方使用我的匹配器,而无需一次又一次地添加它们。

有没有一种将自己的匹配器添加到 jasmine 3.1.0的全局方法?

0 投票
1 回答
1743 浏览

angular - Angular 6 业力测试中的“TypeError:expect(...).toBeVisible 不是函数”错误

我使用 npm 将 jasmine-jquery-matchers 库安装到我的 Angular 6 应用程序中。运行测试会出现以下错误:

这是我的测试规范:

还有其他测试我没有包括在内,因为它们都有效。

这是堆栈跟踪:

也许我需要在 karma.conf.js 中配置一些东西?我确实将它添加为框架:

任何帮助将非常感激!

0 投票
1 回答
647 浏览

angular - 在 Angular CLI 项目中使用 Jasmine 自定义匹配器

我有一个简单的需求:在 Angular CLI 项目(Angular v6)中的 Jasmine 单元测试中使用我自己的自定义匹配器。几个约束:

  • 我不想修改 node_modules 下的任何内容;这个项目必须能够通过我的构建管道,它每次都进行全新安装。
  • 我不想为了能够添加到typestsconfig.spec.json 中而构建一个 npm 模块。

到目前为止,我能够辨别的是我需要一个 .d.ts 文件,所以这是我的 matchers.d.ts:

在同一目录中,我有 matchers.ts 文件:

在我的测试(规格)文件中,我有:

最后,我在包含的测试中使用匹配器,例如:

我还尝试将 matchers.d.ts 的完整路径添加到typestsconfig.spec.json 中的数组中。

唉,我一直无法找到有效的解决方案。失败继续表现为 error TS2339: Property 'toContainPath' does not exist on type 'Matchers<HTMLElement>'

我看了有希望的地方,但无济于事:

  1. 定制茉莉花匹配器
  2. 添加茉莉花匹配器 TypeScript 定义
  3. 茉莉花火柴
  4. 使用自定义匹配器测试 Angular
  5. 手动将输入文件添加到 TypeScript

有人能提供秘方吗?

0 投票
1 回答
3456 浏览

typescript - How to let TypeScript know about custom Jest matchers?

I have a react/typescript project, using jest, where I have a custom matcher like:

which I reference in my tests by doing inside the describe function:

And use like this in it functions:

Tests run properly, but typescript compiler does not recognize the matcher, which makes sense cuz I haven't defined it anywhere in the types system

What I have found so far relates to extending the namespace for jasmine and/or jest, e.g.

which hasn't worked for me.

Do you have any idea?