
Security News
Deno 2.4 Brings Back deno bundle, Improves Dependency Management and Observability
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
typescript-mockify
Advanced tools
Typescript mocking library. Makes it easy to create mocks in typescript. Spies are automatically added on every mock function. Properties that are not mapped in the constructor will get default values.
$ npm install typescript-mockify --save
import typescript-mockify
import {ConstructorArguments, Mock, MockBuilder} from "typescript-mockify";
If you would have the following class:
interface ICar {
speed: number;
brand: string;
age: number;
drive(speed: number): void;
stop(): void;
toString(): string;
}
class Car implements ICar {
public speed: number = 0;
constructor(public brand: string, public age: number) {
}
public drive(speed: number): void {
this.speed = speed;
}
public stop(): void {
this.speed = 0;
}
public toString(): string {
return "brand:" + this.brand + ", speed:" + this.speed + ", age:" + this.age;
}
}
You can create a mock like this..
var mockedCar: Mock<ICar> =
new MockBuilder<ICar>().createInstance(Car, new ConstructorArguments()
.map("brand", "vw")
.map("age", 12));
The actual instance will be in the instance property
var actualInstance = mockedCar.instance;
Even though typescript-mockify has created spies for every function, you can still declare returnvalues in an easy way
mockedCar
.setupMethod("toString").andReturn("my mocked returnvalue")
.setupMethod("dummyFunc").andCallFake(() => _.noop);
Or directly fetch the spy of a stubbed function...
var driveSpy: Spy = mockedCar
.setupMethod("drive").getSpy();
You can also use the callback notation to keep the chain alive
mockedCar
.setupSpy("drive", (driveSpy: Spy) => driveSpy.and.callFake(_.noop))
.setupSpy("toString", (toStringSpy: Spy) => toStringSpy.and.returnValue("I came from the spy"));
Or just use it directly...
var driveSpy: Spy = mockedCar.instance.drive;
Just a tiny example to show the difference between the real instance and the mock instance
var car: ICar = new Car("vw", 10);
console.log(car.speed); // 0
car.drive(100);
console.log(car.speed); // 100
car.stop();
console.log(car.speed); // 0
console.log(car.toString()); // brand:vw, speed:0, age:10
var mockedCar: Mock<ICar> = new MockBuilder<ICar>()
.withCallConstructor(true)
.createInstance(Car, new ConstructorArguments()
.map("brand", "vw")
.map("age", 12))
.setupMethod("toString").andReturn("mockedstr");
console.log(mockedCar.instance.speed); // 0
car.drive(100);
console.log(mockedCar.instance.speed); // 0
car.stop();
console.log(mockedCar.instance.speed); // 0
console.log(mockedCar.instance.toString()); // mocked str
FAQs
A library to automate mocking in typescript
The npm package typescript-mockify receives a total of 15 weekly downloads. As such, typescript-mockify popularity was classified as not popular.
We found that typescript-mockify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.