Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix runResult.create type chainning and add mocked generators assertions. #240

Merged
merged 4 commits into from
Sep 11, 2024
Merged
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
63 changes: 60 additions & 3 deletions src/run-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type mock } from 'node:test';
import type { Store } from 'mem-fs';
import { type MemFsEditor, type MemFsEditorFile, create as createMemFsEditor } from 'mem-fs-editor';
import type { BaseEnvironmentOptions, BaseGenerator, GetGeneratorConstructor } from '@yeoman/types';
import type { DefaultEnvironmentApi, DefaultGeneratorApi } from '../types/type-helpers.js';
import type { DefaultEnvironmentApi } from '../types/type-helpers.js';
import { type RunContextSettings } from './run-context.js';
import { type YeomanTest } from './helpers.js';
import { type AskedQuestions } from './adapter.js';
Expand Down Expand Up @@ -103,8 +103,8 @@ export default class RunResult<GeneratorType extends BaseGenerator = BaseGenerat
* Create another RunContext reusing the settings.
* See helpers.create api
*/
create<GeneratorType extends BaseGenerator = DefaultGeneratorApi>(
GeneratorOrNamespace: string | GetGeneratorConstructor<GeneratorType>,
create<G extends BaseGenerator = GeneratorType>(
GeneratorOrNamespace: string | GetGeneratorConstructor<G>,
settings?: RunContextSettings,
environmentOptions?: BaseEnvironmentOptions,
) {
Expand Down Expand Up @@ -429,4 +429,61 @@ export default class RunResult<GeneratorType extends BaseGenerator = BaseGenerat
assertNoJsonFileContent(filename: string, content: Record<string, any>): void {
this.assertNoObjectContent(this._readFile(filename, true), content);
}

/**
* Get the generator mock
* @param generator - the namespace of the mocked generator
* @returns the generator mock
*/
getGeneratorMock(generator: string): ReturnType<typeof mock.fn>['mock'] {
const mockedGenerator: ReturnType<typeof mock.fn> = this.mockedGenerators[generator];
if (!mockedGenerator) {
throw new Error(`Generator ${generator} is not mocked`);
}
return mockedGenerator.mock;
}

/**
* Get the number of times a mocked generator was composed
* @param generator - the namespace of the mocked generator
* @returns the number of times the generator was composed
*/
getGeneratorComposeCount(generator: string): number {
return this.getGeneratorMock(generator).callCount();
}

/**
* Assert that a generator was composed
* @param generator - the namespace of the mocked generator
*/
assertGeneratorComposed(generator: string): void {
assert.ok(this.getGeneratorComposeCount(generator) > 0, `Generator ${generator} is not composed`);
}

/**
* Assert that a generator was composed
* @param generator - the namespace of the mocked generator
*/
assertGeneratorNotComposed(generator: string): void {
const composeCount = this.getGeneratorComposeCount(generator);
assert.ok(composeCount === 0, `Generator ${generator} is composed ${composeCount}`);
}

/**
* Assert that a generator was composed only once
* @param generator - the namespace of the mocked generator
*/
assertGeneratorComposedOnce(generator: string): void {
assert.ok(this.getGeneratorComposeCount(generator) === 1, `Generator ${generator} is not composed`);
}

/**
* Assert that a generator was composed multiple times
* @returns an array of the names of the mocked generators that were composed
*/
getComposedGenerators(): string[] {
return Object.entries(this.mockedGenerators)
.filter(([_generator, mockedGenerator]) => (mockedGenerator as any).mock.callCount() > 0)
.map(([generator]) => generator);
}
}
21 changes: 21 additions & 0 deletions test/run-result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,25 @@ describe('run-result', () => {
}
});
});

it('provides mocked generators check helpers', async () => {
const mockedNamespace = 'mocked:gen';
const result = await helpers
.run(
helpers.createDummyGenerator(undefined, {
async default() {
await this.composeWith(mockedNamespace);
},
}),
)
.withMockedGenerators([mockedNamespace, 'another:gen']);

result.assertGeneratorComposedOnce(mockedNamespace);
result.assertGeneratorComposed(mockedNamespace);
assert(result.getGeneratorComposeCount(mockedNamespace) === 1);
assert.equal(result.getComposedGenerators().length, 1);
assert(result.getComposedGenerators()[0] === mockedNamespace);

result.assertGeneratorNotComposed('another:gen');
});
});
Loading