
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@aesop-fables/containr-testing
Advanced tools
Testing support (utilities, pattern examples) for containr
containr-testing
provides testing support when using containr
for development.
npm install @aesop-fables/containr-testing
yarn add @aesop-fables/containr-testing
One of the most common testing patterns (when using dependency injection) leverages mocks and/or stubs (we're generalizing quite a bit here). containr-testing
provides the InteractionContext<T>
class that allows you
to automatically generate mocks for your dependencies using jest-mock-extended
.
// MyEntityService.ts
import { inject } from '@aesop-fables/containr';
export interface IEvent {
correlationId: string;
}
export interface IEventPublisher {
publish(event: IEvent): Promise<void>;
}
export const MyServices = {
Publisher: 'publisher',
};
export class MyEntityService implements IEntityService {
constructor(
@inject(MyServices.Publisher) private readonly publisher: IEventPublisher,
) {}
createEntity(name: string): Promise<void> {
const entity = {
id: 'you would probably generate this',
name,
};
await this.publisher.push({
correlationId: entity.id,
});
}
}
// MyEntityService.test.ts
import { IEventPublisher, MyEntityService, MyServices } from './MyEntityService';
import { createInteractionContext, InteractionContext } from '@aesop-fables/containr-testing';
describe('InteractionContext example', () => {
let context: InteractionContext<MyEntityService>;
beforeEach(() => {
// This will automatically configured the context
context = createInteractionContext(MyEntityService);
// If you need to modify the underlying collection
// You can do it - only if you do it BEFORE you call `context.classUnderTest`
// Calls to `context.services` will reset the instance of classUnderTest as well as the underlying service container (if it's already been instantiated)
});
test('demo auto-mocking', async () => {
// classsUnderTest is lazily evaluated and is only instantiated once (the first time you call the property)
await context.classUnderTest.createEntity('Hello');
// the context allows you to retrieve the mocks
// You could obviously inspect the invocation and do more elaborate assertions
expect(context.mockFor<IEventPublisher>(MyServices.Publisher).publish).toBeCalledTimes(1);
});
});
FAQs
Testing support (utilities, pattern examples) for containr
The npm package @aesop-fables/containr-testing receives a total of 4 weekly downloads. As such, @aesop-fables/containr-testing popularity was classified as not popular.
We found that @aesop-fables/containr-testing demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.