- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用时 spyOn使用 Jest 和 typescript ,我收到此类型错误:
Type 'Spy' is not assignable to type 'SpyInstance<{}>'. Property 'mockRestore' is missing in type 'Spy'.
class A {
foo = () => this.bar() + 1;
bar = () => 1;
}
test('should pass', () => {
const a = new A();
let barSpy: jest.SpyInstance;
barSpy = spyOn(a, 'bar');
a.foo();
expect(barSpy).toHaveBeenCalled();
});
最佳答案
简答
全局spyOn(...)
函数返回 jasmine.Spy
不是 jest.SpyInstance
.据我所知,这样做的原因是 ease migration from Jasmine to Jest.
这里有两个选项:
let barSpy: jest.SpyInstance;
barSpy = jest.spyOn(a, 'bar'); // <--- explicitly use jest.spyOn
// or
let barSpy: jasmine.Spy; // <--- use jasmine.Spy as your type
barSpy = spyOn(a, 'bar');
node_modules\@types\jest\index.d.ts
文件具有 Jest 类型定义。通过查看它们,我们可以看到
spyOn
的两个实现.
spyOn
返回 jest.SpyInstance
在 jest
里面命名空间。 spyOn
返回 jasmine.Spy
位于全局命名空间中。 jest.spyOn
函数而不是全局函数。
关于typescript - Jest spyOn 不适用于 typescript : "Property ' mockRestore' is missing in type 'Spy' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53989032/
当我尝试监视 $scope.$watch 的监听器函数时,就像从未调用过 spy On http://jsfiddle.net/b8LoLwLb/1/ 我的 Controller angular.mo
我正在尝试为 Angular 应用程序编写单元测试测试用例,并且我正在使用 SpyOn() 方法来监视服务方法。 我正在测试一个服务,它有一个名为 getCurrentBoardTimeIdByCur
我确实有一个具有 2 个重载方法的类。 public static create( this: ModelStatic, values?: M['_creationAttribut
我有一个函数,我只想在第二次调用和第三次调用时模拟,但在第一次调用时使用默认实现。我查看了 Jest 文档,并且有一个函数 mockImplementationOnce 可以用来模拟单个调用的实现。
案例 当我在 rootScope 上创建一个 spy 时,期望由于某种原因失败了。查看 plunkr 并尝试将其注释掉并反向查看。 代码 Plunker Example describe('Testi
我有一个 Angular Controller ,其方法调用 $location.search() 两次。 第一次只是$location.search()返回值。 第二次是 $location.sea
为什么 jest.spyOn 不能使用在测试现场解构的解构函数? 以下测试将失败: export const Funcs = { foo: () => { return 'foo';
我正在使用 Jest 来测试我的 React 组件,但我遇到了一个我以前从未见过的错误。 这是我的 组件: class Row extends React.Component { construc
如果在回调函数中调用该方法,则在对象上使用 spyOn 似乎会失败。 jasmine 不会注意到回调中对方法的任何调用。 请看下面的代码,我在 child.print() 方法上创建了一个 spy 。
对于下面的代码: class Endpoint { constructor(type, start_value) { this.type = type this.end_value
我正在尝试在 React Native 应用程序中测试异步函数。 class myClass extends React.Component { ... closeModal = async
我试图用 Jasmine 测试的函数获取一个对象数组,然后使用 splice() 方法根据函数传递的参数对其重新排序。 我知道我使用 spyOn().and.returnValue() 伪造返回数组。
我正在测试调用其辅助函数 callApi 的 apiMiddleware。为了防止调用将发出 API 调用的实际 callApi,我模拟了该函数。但是,它仍然会被调用。 apiMiddleware.j
我正在尝试为一个简单的 React 组件编写一个简单的测试,并且我想使用 Jest 来确认在我使用 enzyme 模拟点击时调用了一个函数。根据 Jest 文档,我应该能够使用 spyOn 来执行此操
我将 Jasmine-Species 与 jasmine 一起用于 GWT 。 我编写了一个如下所示的测试 feature('checking spy', function() { summary(
您好,我有一个关于使用 Jasmine 模拟 JS 代码的问题。 想象一下有以下情况: function Test(){ var a = 5; var b = 3; Test2
我遇到了一个问题,试图监视在构造函数中调用的服务函数调用。测试是基本的,只是验证函数调用是否实际被调用。 beforeEach(async(() => { TestBed.configure
我定义了一个接口(interface)和不透明的token如下 export let AUTH_SERVICE = new OpaqueToken('auth.service'); export in
我正在使用 Karma-Jasmine 为我的组件(Angular2 应用程序)编写单元测试。我正在使用 Istanbul 进行代码覆盖率报告。 这是我的测试用例, it('Should Invoke
我想用 Jasmine 测试我的 Angular 应用程序。所以我创建了一些测试,其中大部分都运行良好。但是,我的功能之一要求用户填写提示。测试无法填充此提示,所以我用 spyOn(window,'p
我是一名优秀的程序员,十分优秀!