Giter Club home page Giter Club logo

ts-jest-mock-examples's Introduction

ts-jest-mock-examples's People

Contributors

dependabot[bot] avatar tbinna avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

oriamd zwessels

ts-jest-mock-examples's Issues

If possible to mock an interface using ts-jest/utils

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.

Jest running JS files and failing

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) ✗
`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.