Skip to content

Typescript Moq - Helpers to create mocks for unit testing typescript applications

License

Notifications You must be signed in to change notification settings

rkrdovrgs/ts-moq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typescript Moq

Usage

  • Import the the Mock class

import { Mock } from "ts-moq/moq";;

  • Declare a variable to hold the instance of the mock

let dummyServiceMock: Mock<DummyService>;

  • Initialize the mock before each unit test

dummyServiceMock = new Mock<DummyService>();

  • Setup your mock
dummyServiceMock.spyOn(x => x.getDummyData)
  .and.callFake.promise.resolve(() => 
    [
      {name: "John Dae"}, 
      {name: "Nanni Sacase"}, 
      {name: "Terrye Riccetti"}
    ]);
  • If you are using a dependency injection container, register the mock on your container

container.registerInstance(DummyService, dummyServiceMock.object);

  • If you are using a dependency injection container, get the instance of your subject under test through your container so that the registration of your mock is injected

sut = container.get(HomeComponent);

  • Use and flush the mock promise queue on your unit tests as needed. e.g.:
it("should disable the edit option until the has loaded", async (done) => {
    // call the function that calls the dataservice
    sut.loadData();
    
    // assert on values that should not change until the promise is resolved (data is back from the server)
    expect(sut.disableEdit).toBe(true);
    
    // control when a promise should be resolved during the unit test
    await Mock.promiseQueue.flushAndWait();
    
     // assert on what should happen when the promise is resolved (data is back from the server)
    expect(sut.disableEdit).toBe(false);

    done();
});

About

Typescript Moq - Helpers to create mocks for unit testing typescript applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published