sách gpt4 ăn đã đi

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

In lại 作者:太空狗 更新时间:2023-10-29 18:35:56 24 4
mua khóa gpt4 giày nike

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

  • 我不想修改 node_modules 下的任何东西;这个项目必须能够通过我的构建管道,每次都进行全新安装。
  • 我不想构建 npm 模块只是为了能够添加到 tsconfig.spec.json 中的 các loại

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

/// 

declare namespace jasmine {
interface Matchers {
toContainPath(path: string): boolean;
}
}

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

export const customMatchers: jasmine.CustomMatcherFactories = {

// Affirms the element contains a sub-element at the given CSS path.
toContainPath: function (_util, _customEqualityTesters) {
trở lại {
compare: function (element: HTMLElement, path: string) {
const result = {
pass: false,
message: ''
};
if (!element) {
result.message = 'Expected element to be non-empty';
} else if (!path) {
result.message = 'Expected path to be non-empty';
} khác {
result.pass = element.querySelector(path) !== null;
result.message =
'Expected ' + element + (result.pass ? ' not' : '') + ' to contain ' + path;
}
trả về kết quả;
}
};
}
};

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

import { customMatchers } from 'app/testing/matchers';
. . .
///
. . .
beforeEach(() => {
jasmine.addMatchers(customMatchers);
. . .

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

expect(element).toContainPath('my-sidebar');

我还尝试将 matchers.d.ts 的完整路径添加到 tsconfig.spec.json 中的 các loại 数组。

唉,我没能找到有效的解决方案。失败继续表现为 ojit_代码

我看过的有前途的地方,但无济于事:

  1. custom jasmine matchers
  2. adding a jasmine matcher TypeScript definition
  3. jasmine matchers
  4. testing Angular with custom matchers
  5. manually add typing file to TypeScript

谁能提供秘方?

câu trả lời hay nhất

这里的两个答案结合在一起最终对我有用!

Create custom jasmine matcher using Typescript

在测试文件夹中添加了一个 index.ts 并使 .ts 和 .d.ts 文件具有不同的名称(例如 custom-matchers.ts 和 matchers-types.d.ts)。

关于angular - 在 Angular CLI 项目中使用 Jasmine 自定义匹配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54282597/

24 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress