jasmine - 如何修复类型 'AsymmetricMatcher<any>' 不能分配给类型 '() => void' 。在 Jasmine /开 Jest

标签 jasmine jestjs ts-jest

我有一个对象配置,我在其中向函数发送函数回调。

test('should create a ComponentMockwith the needed config', () => {
        const config: EqListeningPlayerConfig = {
           // more options
          myCallback: jasmine.anything()
        };

        sut.doYourThing(config);

        expect(ComponentMock.create).toHaveBeenCalledWith(config);
      });

我遇到的问题是,在更新到最新的 jest/jasmine 后,我在 myCallback: jasmine.anything() 中收到此错误:

How to fix Type 'AsymmetricMatcher' is not assignable to type '() => void'. in jasmine/jest



如果我忽略该行 @ts-ignore我的测试继续工作,但我想知道为什么 linter 会这样说以及如何解决它,因为我真的不明白它。

最佳答案

我在我的应用程序中遇到了同样的问题,我通过将麻烦的类型映射到任何 jasmine.anything() as any 来解决它.也许它也能解决你的问题。

test('should create a ComponentMockwith the needed config', () => {
        const config: EqListeningPlayerConfig = {
           // more options
          myCallback: jasmine.anything() as any // < --- here is what solved my issue
        };

        sut.doYourThing(config);

        expect(ComponentMock.create).toHaveBeenCalledWith(config);
      });

关于jasmine - 如何修复类型 'AsymmetricMatcher<any>' 不能分配给类型 '() => void' 。在 Jasmine /开 Jest ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59215686/

相关文章:

jestjs - 如何使用 Jest 测试 classList.add/remove?

angularjs - Jasmine 测试中未声明的 Controller

angularjs - 模拟 document.referrer Angular Jasmine

javascript - 使用 Jest 和 Create React App (CRA) 预计异步函数内的同步代码会出现函数抛出异常

jestjs - 避免有条件地调用 `expect` : jest testing error

typescript - Vuetify Jest 未知自定义元素 <v-*>

typescript - 如何在 Jest 的 globalSetup 函数中使用模块导入 (@) 和 TypeScript?

javascript - 如何为包含来自另一个类/文件的全局变量的方法编写 Jasmine 测试?

node.js - Jasmine 不与 puppeteer 师合作

javascript - 使用 Jest/Enzyme 访问用 `this` 声明的类对象