Код: Выделить всё
// src/example.js
class A {
name = undefined
constructor(name) {
this.name = name
}
say_my_name() {
console.log('The arg is: ', this.arg1)
}
}
class B {
constructor(name) {
this.a = new A(name)
}
run() {
this.a.say_my_name()
}
}
module.exports = { A, B }
Мой тестовый пример выглядит следующим образом
Код: Выделить всё
const { A, B } = require('../src/example')
it('should run', () => {
const b = new B('test')
b.run()
// how to test if class A's constructor was called with 'test'?
})
Код: Выделить всё
const example = require('../src/example')
const B = example.B
jest.spyOn(example, 'A')
it('should run', () => {
const b = new B('test')
b.run()
expect(example.A).toHaveBeenCalledWith('test')
})
Код: Выделить всё
const example = require('../src/example')
const B = example.B
example.A = jest.fn()
it('should run', () => {
const b = new B('test')
b.run()
expect(example.A).toHaveBeenCalledWith('test')
})
Есть идеи, как заставить это работать? Возможно ли это вообще с Jest?
Подробнее здесь: https://stackoverflow.com/questions/793 ... with-param
Мобильная версия