admin管理员组文章数量:1332394
If I want to create a mock implementation of an instance method of an ES6 Class I would do this
// ExampleClass.js
export class ExampleClass {
constructor(someValue) {
this.a = someValue;
}
exampleMethod(anotherValue) {
// do something with 'anotherValue'
}
}
// OtherModule.js
import {ExampleClass} from './ExampleClass';
export const fooBar = () => {
const ex = new ExampleClass("hello world");
ex.exampleMethod("another value");
};
// ExampleClass.test.js
import {fooBar} from './OtherModule';
import {ExampleClass} from './ExampleClass';
jest.mock('./ExampleClass');
it('try to create a mock of ExampleClass', () => {
ExampleClass.mockClear();
fooBar();
// to verify values for of instance method "exampleMethod" of ExampleClass instance
expect(ExampleClass.mock.instances[0].exampleMethod.calls.length).toBe(1);
expect(ExampleClass.mock.instances[0].exampleMethod.calls[0][0]).toBe("another value");
// How to verify values for **constructor** of ExampleClass ?????
// expect(ExampleClass.mock.instances[0].constructor.calls.length).toBe(1);
// expect(ExampleClass.mock.instances[0].constructor.calls[0][0]).toBe("another value");
});
What I don't know how to do (and sort of alluded to in the mented code) is how to spy on / access the values of the constructor (not just an instance method).
Any help would be greatly appreciated! ❤
If I want to create a mock implementation of an instance method of an ES6 Class I would do this
// ExampleClass.js
export class ExampleClass {
constructor(someValue) {
this.a = someValue;
}
exampleMethod(anotherValue) {
// do something with 'anotherValue'
}
}
// OtherModule.js
import {ExampleClass} from './ExampleClass';
export const fooBar = () => {
const ex = new ExampleClass("hello world");
ex.exampleMethod("another value");
};
// ExampleClass.test.js
import {fooBar} from './OtherModule';
import {ExampleClass} from './ExampleClass';
jest.mock('./ExampleClass');
it('try to create a mock of ExampleClass', () => {
ExampleClass.mockClear();
fooBar();
// to verify values for of instance method "exampleMethod" of ExampleClass instance
expect(ExampleClass.mock.instances[0].exampleMethod.calls.length).toBe(1);
expect(ExampleClass.mock.instances[0].exampleMethod.calls[0][0]).toBe("another value");
// How to verify values for **constructor** of ExampleClass ?????
// expect(ExampleClass.mock.instances[0].constructor.calls.length).toBe(1);
// expect(ExampleClass.mock.instances[0].constructor.calls[0][0]).toBe("another value");
});
What I don't know how to do (and sort of alluded to in the mented code) is how to spy on / access the values of the constructor (not just an instance method).
Any help would be greatly appreciated! ❤
Share Improve this question asked Jan 27, 2019 at 3:22 devinmdevinm 8651 gold badge9 silver badges18 bronze badges1 Answer
Reset to default 4ExampleClass
is the constructor function and since the entire module is auto-mocked it is already set up as a mock function:
import {fooBar} from './OtherModule';
import {ExampleClass} from './ExampleClass';
jest.mock('./ExampleClass');
it('try to create a mock of ExampleClass', () => {
ExampleClass.mockClear();
fooBar();
// to verify values for of instance method "exampleMethod" of ExampleClass instance
expect(ExampleClass.mock.instances[0].exampleMethod.mock.calls.length).toBe(1); // SUCCESS
expect(ExampleClass.mock.instances[0].exampleMethod.mock.calls[0][0]).toBe("another value"); // SUCCESS
// Verify values for **constructor** of ExampleClass
expect(ExampleClass.mock.calls.length).toBe(1); // SUCCESS
expect(ExampleClass.mock.calls[0][0]).toBe("hello world"); // SUCCESS
});
本文标签: javascriptJest How to get arguments passed to mock constructorStack Overflow
版权声明:本文标题:javascript - Jest: How to get arguments passed to mock constructor? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742283393a2446506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论