首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在JEST中模拟静态方法

在JEST中模拟静态方法

提问于 2018-06-06 09:36:12
回答 1关注 0查看 197

我很难用玩笑来模拟静态的方法。Immagine有一个具有静态方法的A类:

代码语言:txt
复制
export default class A {
  f() {
    return 'a.f()'
  }

  static staticF () {
    return 'A.staticF()'
  }
}

以及进口A的B级

代码语言:txt
复制
import A from './a'

export default class B {
  g() {
    const a = new A()
    return a.f()  
  }

  gCallsStaticF() {
    return A.staticF()
  }
}

现在您想要模拟A。很容易模拟f():

代码语言:txt
复制
import A from '../src/a'
import B from '../src/b'

jest.mock('../src/a', () => {
  return jest.fn().mockImplementation(() => {
    return { f: () => { return 'mockedA.f()'} }
  })
})

describe('Wallet', () => {
  it('should work', () => {
    const b = new B()
    const result = b.g()
    console.log(result) // prints 'mockedA.f()'
  })
})

然而,我无法资助任何关于如何模拟A.StaticF的文档。这个是可能的吗?

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档