Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
ts-mockery
Advanced tools
Simple type-safe mocking library for TypeScript.
import { Mock } from 'ts-mockery';
interface ObjectToNest {
anotherStringFunction: () => string;
unusedFunction: () => void;
}
class ObjectToMock {
nestedObject: ObjectToNest;
string = ':-)';
stringFunction = (buzz: string): string => buzz.toUpperCase();
}
Mock.of<ObjectToMock>({ string: 'not :-)' });
Mock.of<ObjectToMock>({ string: 'still not :-)', stringFunction: () => 'type-safe partial of return type' });
Mock.of<ObjectToMock>({ nestedObject: { anotherStringFunction: () => 'type-safe partial of return type' } });
import { Mock } from 'ts-mockery';
interface ObjectToNest {
anotherStringFunction: () => string;
unusedFunction: () => void;
}
class ObjectToMock {
nestedObject: ObjectToNest;
string = ':-)';
stringFunction = (buzz: string): string => buzz.toUpperCase();
}
const mock = Mock.of<ObjectToMock>({ string: 'not :-)' });
Mock.extend(mock).with({ string: 'still not :-)', stringFunction: () => 'type-safe partial of return type' });
More usage examples can be found @ https://stackblitz.com/edit/ts-mockery-examples?file=tests.ts
import { Mock } from 'ts-mockery';
class ObjectToMock {
static static: () => 'hi';
}
Mock.staticMethod(ObjectToMock, 'static', () => 'not hi');
We noticed issues when we gave instantiated base class objects to Mock.of
. The reason being that the type checker does not look at the prototype.
import { Mock } from 'ts-mockery';
Mock.of<SomeClass>(new BaseOfSomeClass());
now with Mock.from
:
import { Mock } from 'ts-mockery';
Mock.from<SomeClass>(new BaseOfSomeClass());
Also when setting mocked property to an observable there is a circular reference that would throw an RangeError {}
import { Mock } from 'ts-mockery';
import { of } from 'rxjs';
Mock.of<SomeClass>({ something$: of(someValue) });
now with Mock.from
:
import { Mock } from 'ts-mockery';
import { of } from 'rxjs';
Mock.from<SomeClass>({ something$: of(someValue) });
We got you covered, Mock.noop
will return you a spied on function.
import { Mock } from 'ts-mockery';
class ObjectToMock {
doNotCare: () => 'hi';
}
const mock = Mock.of<ObjectToMock>({ doNotCare: Mock.noop });
const result = mock.doNotCare();
expect(mock.doNotCare).toHaveBeenCalled();
expect(result).not.toBe('hi');
With Mock.all it will use a proxy to create spies as on demand.
import { Mock } from 'ts-mockery';
class ObjectToMock {
doNotCare: () => 'hi';
}
const mock = Mock.all<ObjectToMock>();
const result = mock.doNotCare();
expect(mock.doNotCare).toHaveBeenCalled();
expect(result).not.toBe('hi');
Create a test setup file to be included in your Jest or Jasmine config.
import { Mock } from 'ts-mockery';
// The argument to configure can be either jest, jasmine, noop, or an object that implements the exported SpyAdapater interface
Mock.configure('jest');
The above can be added directly to your karma test shim if you'd like.
To configure in Jest add the mockery configuration into the jest config with the key "setupFiles" like so:
setupFiles: ['./jest-setup.ts'],
It is important that this file is included before tests run.
Also Important:
Jest for whatever reason does not reset mocks between tests by default. This causes problems with the mocking of static methods. If you intend to use static method mocking you can add "restoreMocks: true" to your jest config and all will be right in the world.
FAQs
Yet another typescript mocking library.
The npm package ts-mockery receives a total of 24,798 weekly downloads. As such, ts-mockery popularity was classified as popular.
We found that ts-mockery 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.