
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
typescript-easymock
Advanced tools
Delightful unit test mocking for TypeScript, inspired by EasyMock.
After extensive consideration of existing mocking frameworks for JavaScript and TypeScript, I came to the conclusion that none of them offered the same level of control in an API as expressive as that of easymock, while providing a high level of type safety.
The goals of typescript-easymock are :
A test with easymock is broken down in three phases:
This is what it looks like in code:
// The control is a context you use to create mocks and control the current phase
const control = createControl();
// Create a mock for each dependency of the class under test
const dataServiceMock = control.mock(MyDataService);
// The control always starts in record state. This is where we specify the expected behavior
expectCall(dataServiceMock.fetchUserProfile({ id: 3 })).andReturn(Promise.resolve({ name: "John", email: "john.snow@petghost.com" }));
// Switch to replay mode
control.replay();
const testClass = new UserProfileInformation(dataServiceMock);
const name = await testClass.getUserNameById(3);
// Check that all expected calls actually happened
control.verify();
// You can use regular assertions from your testing framework (jasmine/jest in this example)
expect(name).toBe("John");
You will generally create the control and the mocks during the setup of your test (beforeEach), and call control.verify() during your test teardown.
For instance, in Jasmine:
describe('UserProfileInformation', () => {
let control: IMocksControl
let testClass: UserProfileInformation
let dataServiceMock: MyDataService
beforeEach(() => {
control = createControl()
dataServiceMock = control.mock(MyDataService)
testClass = new UserProfileInformation(dataServiceMock)
})
afterEach(() => {
control.verify();
})
})
⚠️ Warning: Always avoid side effects in class constructors. If the constructor makes a call to dataServiceMock (and remember that we are still in record mode at this point), then weird things will happen.
typescript-easymock provides a collection of matchers to use when you cannot or don't want to specifiy exactly the parameters which will be passed to a mocked method.
expectCall(dataServiceMock.fetchUserProfile({ id: anyNumber() }))
control.replay()
testClass.getRandomUserName()
Yes, of course in reality you would mock the randomness source used by getRandomUserName such that you can inject a well known value... This is just an example.
Here's another example if you would want to check that the parameter is equal by reference to the expected parameter:
const veryHeavyObject = createSomeVeryComplexObject()
expectCall(queryEngineMock.transform(same(veryHeavyObject)))
control.replay()
// Here, we want to check that the object has been passed as-is and that it was not copied (which would be expensive).
myDatabase.processQuery({ query: veryHeavyObject })
FAQs
Delightful class mocks for JavaScript and TypeScript.
We found that typescript-easymock 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
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.