Skip to content

Commit 1cc1b0a

Browse files
authored
fix runResult.create type chainning and add mocked generators assertions. (#240)
* fix runResult.create type chainning * add mocked generators helpers * add assertGeneratorNotComposed api * add getGeneratorMock method
1 parent d2cccba commit 1cc1b0a

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

src/run-result.ts

+60-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { type mock } from 'node:test';
66
import type { Store } from 'mem-fs';
77
import { type MemFsEditor, type MemFsEditorFile, create as createMemFsEditor } from 'mem-fs-editor';
88
import type { BaseEnvironmentOptions, BaseGenerator, GetGeneratorConstructor } from '@yeoman/types';
9-
import type { DefaultEnvironmentApi, DefaultGeneratorApi } from '../types/type-helpers.js';
9+
import type { DefaultEnvironmentApi } from '../types/type-helpers.js';
1010
import { type RunContextSettings } from './run-context.js';
1111
import { type YeomanTest } from './helpers.js';
1212
import { type AskedQuestions } from './adapter.js';
@@ -103,8 +103,8 @@ export default class RunResult<GeneratorType extends BaseGenerator = BaseGenerat
103103
* Create another RunContext reusing the settings.
104104
* See helpers.create api
105105
*/
106-
create<GeneratorType extends BaseGenerator = DefaultGeneratorApi>(
107-
GeneratorOrNamespace: string | GetGeneratorConstructor<GeneratorType>,
106+
create<G extends BaseGenerator = GeneratorType>(
107+
GeneratorOrNamespace: string | GetGeneratorConstructor<G>,
108108
settings?: RunContextSettings,
109109
environmentOptions?: BaseEnvironmentOptions,
110110
) {
@@ -429,4 +429,61 @@ export default class RunResult<GeneratorType extends BaseGenerator = BaseGenerat
429429
assertNoJsonFileContent(filename: string, content: Record<string, any>): void {
430430
this.assertNoObjectContent(this._readFile(filename, true), content);
431431
}
432+
433+
/**
434+
* Get the generator mock
435+
* @param generator - the namespace of the mocked generator
436+
* @returns the generator mock
437+
*/
438+
getGeneratorMock(generator: string): ReturnType<typeof mock.fn>['mock'] {
439+
const mockedGenerator: ReturnType<typeof mock.fn> = this.mockedGenerators[generator];
440+
if (!mockedGenerator) {
441+
throw new Error(`Generator ${generator} is not mocked`);
442+
}
443+
return mockedGenerator.mock;
444+
}
445+
446+
/**
447+
* Get the number of times a mocked generator was composed
448+
* @param generator - the namespace of the mocked generator
449+
* @returns the number of times the generator was composed
450+
*/
451+
getGeneratorComposeCount(generator: string): number {
452+
return this.getGeneratorMock(generator).callCount();
453+
}
454+
455+
/**
456+
* Assert that a generator was composed
457+
* @param generator - the namespace of the mocked generator
458+
*/
459+
assertGeneratorComposed(generator: string): void {
460+
assert.ok(this.getGeneratorComposeCount(generator) > 0, `Generator ${generator} is not composed`);
461+
}
462+
463+
/**
464+
* Assert that a generator was composed
465+
* @param generator - the namespace of the mocked generator
466+
*/
467+
assertGeneratorNotComposed(generator: string): void {
468+
const composeCount = this.getGeneratorComposeCount(generator);
469+
assert.ok(composeCount === 0, `Generator ${generator} is composed ${composeCount}`);
470+
}
471+
472+
/**
473+
* Assert that a generator was composed only once
474+
* @param generator - the namespace of the mocked generator
475+
*/
476+
assertGeneratorComposedOnce(generator: string): void {
477+
assert.ok(this.getGeneratorComposeCount(generator) === 1, `Generator ${generator} is not composed`);
478+
}
479+
480+
/**
481+
* Assert that a generator was composed multiple times
482+
* @returns an array of the names of the mocked generators that were composed
483+
*/
484+
getComposedGenerators(): string[] {
485+
return Object.entries(this.mockedGenerators)
486+
.filter(([_generator, mockedGenerator]) => (mockedGenerator as any).mock.callCount() > 0)
487+
.map(([generator]) => generator);
488+
}
432489
}

test/run-result.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,25 @@ describe('run-result', () => {
266266
}
267267
});
268268
});
269+
270+
it('provides mocked generators check helpers', async () => {
271+
const mockedNamespace = 'mocked:gen';
272+
const result = await helpers
273+
.run(
274+
helpers.createDummyGenerator(undefined, {
275+
async default() {
276+
await this.composeWith(mockedNamespace);
277+
},
278+
}),
279+
)
280+
.withMockedGenerators([mockedNamespace, 'another:gen']);
281+
282+
result.assertGeneratorComposedOnce(mockedNamespace);
283+
result.assertGeneratorComposed(mockedNamespace);
284+
assert(result.getGeneratorComposeCount(mockedNamespace) === 1);
285+
assert.equal(result.getComposedGenerators().length, 1);
286+
assert(result.getComposedGenerators()[0] === mockedNamespace);
287+
288+
result.assertGeneratorNotComposed('another:gen');
289+
});
269290
});

0 commit comments

Comments
 (0)