Install: yarn
Run tests: yarn test
Related jest documentation:
Examples on how to use ts-jest to mock classes written in Tyepescript
License: MIT License
Install: yarn
Run tests: yarn test
Related jest documentation:
Assume there is a MyService
class that depends on the MyRepository
interface.
When testing the MyService
, if possible to mock the dependencies.
I have tried jest-mock-extended
and ts-mockito
, both worked. Not sure there is an approach with this simple ts-jest utils.
Testes does not pass since jest is running *.test.js files.
Need to add testMatch to jest.config.js
testMatch: [ "**/__tests__/**/*.ts", "**/?(*.)+(spec|test).ts" ]
LOG
`
➜ jest-examples cd ts-jest-mock-examples
➜ ts-jest-mock-examples git:(master) ✗ jest
FAIL sound-player/SoundPlayer.test.js
● Console
console.log sound-player/SoundPlayer.js:8
Playing sound file song.mp3
● is called with filename
TypeError: soundPlayer.playSoundFile.mockClear is not a function
9 | const soundPlayer = utils_1.mocked(new SoundPlayer_1.default());
10 | beforeEach(() => {
> 11 | soundPlayer.playSoundFile.mockClear();
| ^
12 | });
13 | it('is called with filename', () => {
14 | const filename = 'song.mp3';
at Object.<anonymous>.beforeEach (sound-player/SoundPlayer.test.js:11:31)
● is called with filename
expect(received).toBeCalledWith(...expected)
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function playSoundFile]
14 | const filename = 'song.mp3';
15 | soundPlayer.playSoundFile(filename);
> 16 | expect(soundPlayer.playSoundFile).toBeCalledWith(filename);
| ^
17 | });
18 |
at Object.<anonymous>.it (sound-player/SoundPlayer.test.js:16:39)
FAIL my-class/MyClass.test.js
● MyClass › should check the constructor has been called
TypeError: myClass.instanceMethod.mockClear is not a function
26 | const myClass = utils_1.mocked(new MyClass_1.default());
27 | beforeEach(() => {
> 28 | myClass.instanceMethod.mockClear();
| ^
29 | myClass.arrowFunction.mockClear();
30 | });
31 | it("should check the constructor has been called", () => {
at Object.beforeEach (my-class/MyClass.test.js:28:32)
● MyClass › should check the constructor has been called
expect(received).toBeCalledTimes(expected)
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function MyClass]
31 | it("should check the constructor has been called", () => {
32 | new MyClass_1.default();
> 33 | expect(MyClass_1.default).toBeCalledTimes(1);
| ^
34 | });
35 | it("should call mocked instanceMethod", () => {
36 | const result = myClass.instanceMethod("Peter");
at Object.it (my-class/MyClass.test.js:33:35)
● MyClass › should call mocked instanceMethod
TypeError: myClass.instanceMethod.mockClear is not a function
26 | const myClass = utils_1.mocked(new MyClass_1.default());
27 | beforeEach(() => {
> 28 | myClass.instanceMethod.mockClear();
| ^
29 | myClass.arrowFunction.mockClear();
30 | });
31 | it("should check the constructor has been called", () => {
at Object.beforeEach (my-class/MyClass.test.js:28:32)
● MyClass › should call mocked instanceMethod
expect(received).toBeCalledWith(...expected)
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function instanceMethod]
35 | it("should call mocked instanceMethod", () => {
36 | const result = myClass.instanceMethod("Peter");
> 37 | expect(myClass.instanceMethod).toBeCalledWith("Peter");
| ^
38 | // result is undefined because we did not change the default mock
39 | // implementation of instanceMethod which is jest.fn() and simply returns
40 | // undefined.
at Object.it (my-class/MyClass.test.js:37:40)
● MyClass › should change the instanceMethod mock implementation
TypeError: myClass.instanceMethod.mockClear is not a function
26 | const myClass = utils_1.mocked(new MyClass_1.default());
27 | beforeEach(() => {
> 28 | myClass.instanceMethod.mockClear();
| ^
29 | myClass.arrowFunction.mockClear();
30 | });
31 | it("should check the constructor has been called", () => {
at Object.beforeEach (my-class/MyClass.test.js:28:32)
● MyClass › should change the instanceMethod mock implementation
TypeError: myClass.instanceMethod.mockImplementationOnce is not a function
42 | });
43 | it("should change the instanceMethod mock implementation", () => {
> 44 | myClass.instanceMethod.mockImplementationOnce((s) => `Hi, ${s}`);
| ^
45 | const result = myClass.instanceMethod("Peter");
46 | expect(myClass.instanceMethod).toBeCalledWith("Peter");
47 | expect(result).toBe("Hi, Peter");
at Object.it (my-class/MyClass.test.js:44:32)
● MyClass › should call mocked arrowFunction
TypeError: myClass.instanceMethod.mockClear is not a function
26 | const myClass = utils_1.mocked(new MyClass_1.default());
27 | beforeEach(() => {
> 28 | myClass.instanceMethod.mockClear();
| ^
29 | myClass.arrowFunction.mockClear();
30 | });
31 | it("should check the constructor has been called", () => {
at Object.beforeEach (my-class/MyClass.test.js:28:32)
● MyClass › should call mocked arrowFunction
expect(received).toBeCalledWith(...expected)
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function anonymous]
49 | it("should call mocked arrowFunction", () => {
50 | const result = myClass.arrowFunction("Joe");
> 51 | expect(myClass.arrowFunction).toBeCalledWith("Joe");
| ^
52 | expect(result).toBeUndefined();
53 | });
54 | it("should change the arrowFunction mock implementation", () => {
at Object.it (my-class/MyClass.test.js:51:39)
● MyClass › should change the arrowFunction mock implementation
TypeError: myClass.instanceMethod.mockClear is not a function
26 | const myClass = utils_1.mocked(new MyClass_1.default());
27 | beforeEach(() => {
> 28 | myClass.instanceMethod.mockClear();
| ^
29 | myClass.arrowFunction.mockClear();
30 | });
31 | it("should check the constructor has been called", () => {
at Object.beforeEach (my-class/MyClass.test.js:28:32)
● MyClass › should change the arrowFunction mock implementation
TypeError: myClass.arrowFunction.mockImplementation is not a function
53 | });
54 | it("should change the arrowFunction mock implementation", () => {
> 55 | myClass.arrowFunction.mockImplementation((s) => `Bonjour, ${s}`);
| ^
56 | const result = myClass.arrowFunction("Joe");
57 | expect(myClass.arrowFunction).toBeCalledWith("Joe");
58 | expect(result).toBe("Bonjour, Joe");
at Object.it (my-class/MyClass.test.js:55:31)
PASS my-class/MyClass.test.ts
PASS sound-player/SoundPlayer.test.ts
Test Suites: 2 failed, 2 passed, 4 total
Tests: 6 failed, 6 passed, 12 total
Snapshots: 0 total
Time: 2.955s
Ran all test suites.
➜ ts-jest-mock-examples git:(master) ✗
`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.