Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions JSDOC_STYLE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ Error classes need special attention with comprehensive documentation:
* // How to prevent/fix the error
* await TestBed.solitary(MyService)
* .mock(SomeDependency)
* .impl(() => ({ method: jest.fn() }))
* .compile();
*/
export class CustomError extends Error {
Expand Down Expand Up @@ -454,8 +453,8 @@ export const stub = jest.fn();
* const { unit, unitRef } = await TestBed.sociable(MyService)
* .expose(DependencyOne)
* .mock(Logger)
* .impl(stub => ({ log: stub().mockReturnValue('overridden') }))
* .compile();
* unitRef.get(Logger).log.mockReturnValue('overridden');
*/
```

Expand Down
18 changes: 5 additions & 13 deletions e2e/esm/jest/injection-js/abstract-tokens.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,15 @@ describe('abstract class dependency tokens', () => {

it('accepts a concrete implementation for an abstract token', async () => {
const supplied = new MockHost('singapore');
const { unit, unitRef } = await TestBed.solitary(Repository)
.mock(Host)
.final(supplied)
.compile();
const { unit, unitRef } = await TestBed.solitary(Repository).final(Host, supplied).compile();
expect(unit.host).toBe(supplied);
expect(unit.readRegion()).toBe('singapore');
expect(() => unitRef.get(Host)).toThrow(/faked dependency/);
});

it('infers the abstract dependency when configuring an implementation', async () => {
const { unit, unitRef } = await TestBed.solitary(Repository)
.mock(Host)
.impl((stub) => ({ region: stub().mockReturnValue('singapore') }))
.compile();
const { unit, unitRef } = await TestBed.solitary(Repository).mock(Host).compile();
unitRef.get(Host).region.mockReturnValue('singapore');
const host = unitRef.get(Host);
expect(unit.readRegion()).toBe('singapore');
host.region.mockReturnValue('london');
Expand All @@ -79,7 +74,6 @@ describe('abstract class dependency tokens', () => {
const { unit, unitRef } = await TestBed.sociable(Service)
.expose(Repository)
.mock(Host)
.impl((stub) => ({ region: stub() }))
.compile();
unitRef.get(Host).region.mockReturnValue('singapore');
expect(unit.repository).toBeInstanceOf(Repository);
Expand All @@ -104,11 +98,9 @@ describe('abstract class dependency tokens', () => {
TestBed.sociable(Host);
// @ts-expect-error Exposed classes must be concrete.
TestBed.sociable(Service).expose(Host);
const override = TestBed.solitary(Repository).mock(Host);
const builder = TestBed.solitary(Repository);
// @ts-expect-error Final overrides infer Host and reject unrelated properties.
override.final({ unknownMethod: () => 42 });
// @ts-expect-error Implementation callbacks infer Host and reject unrelated properties.
override.impl(() => ({ unknownMethod: () => 42 }));
builder.final(Host, { unknownMethod: () => 42 });
};
expect(checkTypes).toBeDefined();
});
Expand Down
6 changes: 2 additions & 4 deletions e2e/esm/jest/injection-js/composition.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ describe('default sociable composition', () => {
it('retains explicit override chains and excludes selective exposure', async () => {
const composed = TestBed.sociable(Service);
const selective = TestBed.sociable(Service).expose(Repository);
const overridden = await composed
.mock(Leaf)
.impl((stub) => ({ prefix: stub().mockReturnValue('mock:') }))
.compile();
const overridden = await composed.mock(Leaf).compile();
overridden.unitRef.get(Leaf).prefix.mockReturnValue('mock:');
// @ts-expect-error Choose selective exposure before declaring a boundary.
const invalidExpose = composed.forbid(Leaf).expose;
// @ts-expect-error All exposed classes are supplied in one call.
Expand Down
18 changes: 7 additions & 11 deletions e2e/esm/jest/injection-js/ctor-injection.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ class Service {
describe('published InjectionJS adapter', () => {
it('distinguishes finalized and mocked tokens with identical descriptions', async () => {
const { unit, unitRef } = await TestBed.solitary(Service)
.mock<Repository>(TOKEN)
.final({ find: () => 'final' })
.mock<string>(CONFIG)
.final('test')
.final<Repository>(TOKEN, { find: () => 'final' })
.final<string>(CONFIG, 'test')
.compile();
expect(unit.token.find()).toBe('final');
expect(unit.other).toBe(unitRef.get<Repository>(OTHER));
Expand All @@ -59,19 +57,17 @@ describe('published InjectionJS adapter', () => {
constructor(@Optional() @Inject(TOKEN) readonly repository: Repository | null) {}
}
const { unit } = await TestBed.solitary(OptionalService)
.mock<Repository | null>(TOKEN)
.final(null)
.final<Repository | null>(TOKEN, null)
.compile();
expect(unit.repository).toBeNull();
});

it('mocks class, string, symbol and InjectionToken dependencies', async () => {
const { unit, unitRef } = await TestBed.solitary(Service)
.mock<Repository>(TOKEN)
.impl((stub) => ({ find: stub().mockReturnValue('mock') }))
.mock<string>(CONFIG)
.final('test')
.final<string>(CONFIG, 'test')
.compile();
unitRef.get<Repository>(TOKEN).find.mockReturnValue('mock');
const repository: Mocked<Repository> = unitRef.get(Repository);
repository.find.mockReturnValue('class mock');
expect(unit.repository.find()).toBe('class mock');
Expand Down Expand Up @@ -137,12 +133,12 @@ describe('published InjectionJS adapter', () => {
// This function is checked by tsc without executing the intentionally invalid calls.
function checkPublicTypes() {
const builder = TestBed.solitary(Service);
builder.mock<Repository>(TOKEN).impl((stub) => ({ find: stub().mockReturnValue('ok') }));
builder.mock<Repository>(TOKEN);
// @ts-expect-error arbitrary object tokens are not registered
builder.mock({});
// @ts-expect-error InjectionJS has no identifier metadata
builder.mock(TOKEN, { tag: 'production' });
// @ts-expect-error a string token cannot be finalized with a number
builder.mock<string>(CONFIG).final(42);
builder.final<string>(CONFIG, 42);
}
void checkPublicTypes;
18 changes: 5 additions & 13 deletions e2e/esm/jest/nestjs/abstract-tokens.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,15 @@ describe('abstract class dependency tokens', () => {

it('accepts a concrete implementation for an abstract token', async () => {
const supplied = new MockHost('singapore');
const { unit, unitRef } = await TestBed.solitary(Repository)
.mock(Host)
.final(supplied)
.compile();
const { unit, unitRef } = await TestBed.solitary(Repository).final(Host, supplied).compile();
expect(unit.host).toBe(supplied);
expect(unit.readRegion()).toBe('singapore');
expect(() => unitRef.get(Host)).toThrow(/faked dependency/);
});

it('infers the abstract dependency when configuring an implementation', async () => {
const { unit, unitRef } = await TestBed.solitary(Repository)
.mock(Host)
.impl((stub) => ({ region: stub().mockReturnValue('singapore') }))
.compile();
const { unit, unitRef } = await TestBed.solitary(Repository).mock(Host).compile();
unitRef.get(Host).region.mockReturnValue('singapore');
const host = unitRef.get(Host);
expect(unit.readRegion()).toBe('singapore');
host.region.mockReturnValue('london');
Expand All @@ -79,7 +74,6 @@ describe('abstract class dependency tokens', () => {
const { unit, unitRef } = await TestBed.sociable(Service)
.expose(Repository)
.mock(Host)
.impl((stub) => ({ region: stub() }))
.compile();
unitRef.get(Host).region.mockReturnValue('singapore');
expect(unit.repository).toBeInstanceOf(Repository);
Expand All @@ -104,11 +98,9 @@ describe('abstract class dependency tokens', () => {
TestBed.sociable(Host);
// @ts-expect-error Exposed classes must be concrete.
TestBed.sociable(Service).expose(Host);
const override = TestBed.solitary(Repository).mock(Host);
const builder = TestBed.solitary(Repository);
// @ts-expect-error Final overrides infer Host and reject unrelated properties.
override.final({ unknownMethod: () => 42 });
// @ts-expect-error Implementation callbacks infer Host and reject unrelated properties.
override.impl(() => ({ unknownMethod: () => 42 }));
builder.final(Host, { unknownMethod: () => 42 });
};
expect(checkTypes).toBeDefined();
});
Expand Down
6 changes: 2 additions & 4 deletions e2e/esm/jest/nestjs/composition.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ describe('default sociable composition', () => {
it('retains explicit override chains and excludes selective exposure', async () => {
const composed = TestBed.sociable(Service);
const selective = TestBed.sociable(Service).expose(Repository);
const overridden = await composed
.mock(Leaf)
.impl((stub) => ({ prefix: stub().mockReturnValue('mock:') }))
.compile();
const overridden = await composed.mock(Leaf).compile();
overridden.unitRef.get(Leaf).prefix.mockReturnValue('mock:');
// @ts-expect-error Choose selective exposure before declaring a boundary.
const invalidExpose = composed.forbid(Leaf).expose;
// @ts-expect-error All exposed classes are supplied in one call.
Expand Down
17 changes: 7 additions & 10 deletions e2e/esm/jest/nestjs/getters.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@ class Application {
describe('Supplied getter implementations', () => {
it('configures a readonly getter through TestBed without assignment', async () => {
let reads = 0;
const { unit, unitRef } = await TestBed.solitary(Application)
.mock(ConfigService)
.impl(() => ({
get isCli() {
reads += 1;
return false;
},
}))
.compile();
const config = mock<ConfigService>({
get isCli() {
reads += 1;
return false;
},
});
const { unit } = await TestBed.solitary(Application).final(ConfigService, config).compile();

expect(reads).toBe(0);
expect(unit.isCli()).toBe(false);
expect(reads).toBe(1);
const config = unitRef.get(ConfigService);
jest.spyOn(config, 'isCli', 'get').mockReturnValue(true);
expect(unit.isCli()).toBe(true);
});
Expand Down
26 changes: 10 additions & 16 deletions e2e/esm/jest/nestjs/suites-jest-ctor-injection.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mock } from '@suites/doubles.jest';
import type { UnitReference, Mocked } from '@suites/unit';
import { TestBed } from '@suites/unit';
import type { Logger, TestClassFive } from './e2e-assets.js';
Expand All @@ -13,27 +14,21 @@ import {
describe('Suites Jest / NestJS E2E Test Ctor', () => {
let unit: NestJSTestClass;
let unitRef: UnitReference;
const testClassOne = mock<TestClassOne>();

beforeAll(async () => {
testClassOne.foo.mockResolvedValue('foo-from-test');
testClassOne.bar.mockReturnValue('bar');
const { unitRef: ref, unit: underTest } = await TestBed.solitary<NestJSTestClass>(
NestJSTestClass
)
.mock(TestClassOne)
.impl((stubFn) => ({
foo: stubFn().mockResolvedValue('foo-from-test'),
bar(): string {
return 'bar';
},
}))
.mock<string>('CONSTANT_VALUE')
.final('arbitrary-string')
.mock('UNDEFINED')
.final({ method: () => 456 })
.final(TestClassOne, testClassOne)
.final<string>('CONSTANT_VALUE', 'arbitrary-string')
.final('UNDEFINED', { method: () => 456 })
.mock<Logger>('LOGGER')
.impl((stubFn) => ({ log: stubFn().mockReturnValue('baz-from-test') }))
.mock<TestClassFive>(SymbolToken)
.final({ doSomething: () => 'mocked' })
.final<TestClassFive>(SymbolToken, { doSomething: () => 'mocked' })
.compile();
ref.get<Logger>('LOGGER').log.mockReturnValue('baz-from-test');

unitRef = ref;
unit = underTest;
Expand All @@ -46,7 +41,7 @@ describe('Suites Jest / NestJS E2E Test Ctor', () => {

test('then successfully resolve the dependencies of the tested classes', () => {
expect(() => unitRef.get<{ log: () => void }>('LOGGER')).toBeDefined();
expect(() => unitRef.get(TestClassOne)).toBeDefined();
expect(() => unitRef.get(TestClassOne)).toThrow(/faked dependency/);
});

test('call the unit instance method', async () => {
Expand All @@ -59,7 +54,6 @@ describe('Suites Jest / NestJS E2E Test Ctor', () => {
});

test('then mock the implementation of the dependencies', async () => {
const testClassOne: Mocked<TestClassOne> = unitRef.get(TestClassOne);
const logger = unitRef.get<Logger>('LOGGER');

// The original 'foo' method in TestClassOne return value should be changed
Expand Down
3 changes: 1 addition & 2 deletions e2e/esm/jest/nestjs/type-safety.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { NestJSTestClass } from './e2e-assets.js';
describe('NestJS DI Adapter Type Safety', () => {
it('allows mocking without metadata', async () => {
const { unitRef } = await TestBed.solitary(NestJSTestClass)
.mock<string>('CONSTANT_VALUE')
.final('test-value')
.final<string>('CONSTANT_VALUE', 'test-value')
.compile();

expect(unitRef).toBeDefined();
Expand Down
18 changes: 5 additions & 13 deletions e2e/esm/vitest/injection-js/abstract-tokens.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,15 @@ describe('abstract class dependency tokens', () => {

it('accepts a concrete implementation for an abstract token', async () => {
const supplied = new MockHost('singapore');
const { unit, unitRef } = await TestBed.solitary(Repository)
.mock(Host)
.final(supplied)
.compile();
const { unit, unitRef } = await TestBed.solitary(Repository).final(Host, supplied).compile();
expect(unit.host).toBe(supplied);
expect(unit.readRegion()).toBe('singapore');
expect(() => unitRef.get(Host)).toThrow(/faked dependency/);
});

it('infers the abstract dependency when configuring an implementation', async () => {
const { unit, unitRef } = await TestBed.solitary(Repository)
.mock(Host)
.impl((stub) => ({ region: stub().mockReturnValue('singapore') }))
.compile();
const { unit, unitRef } = await TestBed.solitary(Repository).mock(Host).compile();
unitRef.get(Host).region.mockReturnValue('singapore');
const host = unitRef.get(Host);
expect(unit.readRegion()).toBe('singapore');
host.region.mockReturnValue('london');
Expand All @@ -79,7 +74,6 @@ describe('abstract class dependency tokens', () => {
const { unit, unitRef } = await TestBed.sociable(Service)
.expose(Repository)
.mock(Host)
.impl((stub) => ({ region: stub() }))
.compile();
unitRef.get(Host).region.mockReturnValue('singapore');
expect(unit.repository).toBeInstanceOf(Repository);
Expand All @@ -104,11 +98,9 @@ describe('abstract class dependency tokens', () => {
TestBed.sociable(Host);
// @ts-expect-error Exposed classes must be concrete.
TestBed.sociable(Service).expose(Host);
const override = TestBed.solitary(Repository).mock(Host);
const builder = TestBed.solitary(Repository);
// @ts-expect-error Final overrides infer Host and reject unrelated properties.
override.final({ unknownMethod: () => 42 });
// @ts-expect-error Implementation callbacks infer Host and reject unrelated properties.
override.impl(() => ({ unknownMethod: () => 42 }));
builder.final(Host, { unknownMethod: () => 42 });
};
expect(checkTypes).toBeDefined();
});
Expand Down
6 changes: 2 additions & 4 deletions e2e/esm/vitest/injection-js/composition.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ describe('default sociable composition', () => {
it('retains explicit override chains and excludes selective exposure', async () => {
const composed = TestBed.sociable(Service);
const selective = TestBed.sociable(Service).expose(Repository);
const overridden = await composed
.mock(Leaf)
.impl((stub) => ({ prefix: stub().mockReturnValue('mock:') }))
.compile();
const overridden = await composed.mock(Leaf).compile();
overridden.unitRef.get(Leaf).prefix.mockReturnValue('mock:');
// @ts-expect-error Choose selective exposure before declaring a boundary.
const invalidExpose = composed.forbid(Leaf).expose;
// @ts-expect-error All exposed classes are supplied in one call.
Expand Down
18 changes: 7 additions & 11 deletions e2e/esm/vitest/injection-js/ctor-injection.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ class Service {
describe('published InjectionJS adapter', () => {
it('distinguishes finalized and mocked tokens with identical descriptions', async () => {
const { unit, unitRef } = await TestBed.solitary(Service)
.mock<Repository>(TOKEN)
.final({ find: () => 'final' })
.mock<string>(CONFIG)
.final('test')
.final<Repository>(TOKEN, { find: () => 'final' })
.final<string>(CONFIG, 'test')
.compile();
expect(unit.token.find()).toBe('final');
expect(unit.other).toBe(unitRef.get<Repository>(OTHER));
Expand All @@ -59,19 +57,17 @@ describe('published InjectionJS adapter', () => {
constructor(@Optional() @Inject(TOKEN) readonly repository: Repository | null) {}
}
const { unit } = await TestBed.solitary(OptionalService)
.mock<Repository | null>(TOKEN)
.final(null)
.final<Repository | null>(TOKEN, null)
.compile();
expect(unit.repository).toBeNull();
});

it('mocks class, string, symbol and InjectionToken dependencies', async () => {
const { unit, unitRef } = await TestBed.solitary(Service)
.mock<Repository>(TOKEN)
.impl((stub) => ({ find: stub().mockReturnValue('mock') }))
.mock<string>(CONFIG)
.final('test')
.final<string>(CONFIG, 'test')
.compile();
unitRef.get<Repository>(TOKEN).find.mockReturnValue('mock');
const repository: Mocked<Repository> = unitRef.get(Repository);
repository.find.mockReturnValue('class mock');
expect(unit.repository.find()).toBe('class mock');
Expand Down Expand Up @@ -137,12 +133,12 @@ describe('published InjectionJS adapter', () => {
// This function is checked by tsc without executing the intentionally invalid calls.
function checkPublicTypes() {
const builder = TestBed.solitary(Service);
builder.mock<Repository>(TOKEN).impl((stub) => ({ find: stub().mockReturnValue('ok') }));
builder.mock<Repository>(TOKEN);
// @ts-expect-error arbitrary object tokens are not registered
builder.mock({});
// @ts-expect-error InjectionJS has no identifier metadata
builder.mock(TOKEN, { tag: 'production' });
// @ts-expect-error a string token cannot be finalized with a number
builder.mock<string>(CONFIG).final(42);
builder.final<string>(CONFIG, 42);
}
void checkPublicTypes;
Loading
Loading