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

Expose test helper to blueprints #23431

Closed
1 task
mshima opened this issue Sep 6, 2023 · 2 comments · Fixed by #23438
Closed
1 task

Expose test helper to blueprints #23431

mshima opened this issue Sep 6, 2023 · 2 comments · Fixed by #23438
Labels
area: enhancement 🔧 $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ theme: blueprint 🐾 theme: jhipster-internals $100 https://www.jhipster.tech/bug-bounties/
Milestone

Comments

@mshima
Copy link
Member

mshima commented Sep 6, 2023

Overview of the feature request

Expose jhipster test helpers:

class JHipsterRunContext<GeneratorType extends YeomanGenerator = BaseGenerator> extends RunContext<GeneratorType> {
public sharedSource: Record<string, esmocha.Mock>;
private sharedApplication: Record<string, any>;
private sharedControl: Record<string, any>;
private workspaceApplications: string[] = [];
private commonWorkspacesConfig: Record<string, unknown>;
private generateApplicationsSet = false;
withJHipsterConfig(configuration?: Record<string, unknown>, entities?: BaseEntity[]): this {
return this.withFiles(createFiles('', { baseName: 'jhipster', ...configuration }, entities));
}
withSkipWritingPriorities(): this {
return this.withOptions({ skipPriorities: ['writing', 'postWriting', 'writingEntities', 'postWritingEntities'] });
}
withWorkspacesCommonConfig(commonWorkspacesConfig: Record<string, unknown>): this {
if (this.workspaceApplications.length > 0) {
throw new Error('Cannot be called after withWorkspaceApplication');
}
this.commonWorkspacesConfig = { ...this.commonWorkspacesConfig, ...commonWorkspacesConfig };
return this;
}
withWorkspaceApplicationAtFolder(workspaceFolder: string, configuration: Record<string, unknown>, entities?: BaseEntity[]): this {
if (this.generateApplicationsSet) {
throw new Error('Cannot be called after withWorkspaceApplication');
}
this.workspaceApplications.push(workspaceFolder);
return this.withFiles(createFiles(workspaceFolder, { ...configuration, ...this.commonWorkspacesConfig }, entities));
}
withWorkspaceApplication(configuration: Record<string, unknown>, entities?: BaseEntity[]): this {
return this.withWorkspaceApplicationAtFolder(configuration.baseName as string, configuration, entities);
}
withWorkspacesSamples(...appNames: string[]): this {
for (const appName of appNames) {
const application = deploymentTestSamples[appName];
if (!application) {
throw new Error(`Application ${appName} not found`);
}
this.withWorkspaceApplicationAtFolder(appName, deploymentTestSamples[appName]);
}
return this;
}
withGenerateWorkspaceApplications(generateWorkspaces: boolean = false): this {
this.generateApplicationsSet = true;
return this.withOptions({ generateApplications: true, workspacesFolders: this.workspaceApplications, workspaces: generateWorkspaces });
}
withFakeTestBlueprint(blueprintPackage: string, { packageJson, generator = 'test-blueprint' }: FakeBlueprintOptions = {}): this {
return this.withFiles(createBlueprintFiles(blueprintPackage, { packageJson, generator }))
.withLookups({ localOnly: true })
.commitFiles();
}
withMockedSource(): this {
this.sharedSource = new Proxy(
{},
{
get(target, name) {
if (!target[name]) {
target[name] = esmocha.fn();
}
return target[name];
},
set() {
return true;
},
},
);
return this.withSharedApplication({ sharedSource: this.sharedSource });
}
withControl(sharedControl: Record<string, any>): this {
this.sharedControl = {};
Object.assign(this.sharedControl, sharedControl);
return this.withSharedApplication({ control: this.sharedControl });
}
private withSharedApplication(sharedApplication: Record<string, any>): this {
if (!this.sharedApplication) {
const applicationId = 'test-application';
this.sharedApplication = { ...sharedApplication };
set((this as any).envOptions, `sharedOptions.sharedData.applications.${applicationId}`, this.sharedApplication);
return this.withOptions({
applicationId,
});
}
Object.assign(this.sharedApplication, sharedApplication);
return this;
}
async run(): Promise<RunResult<GeneratorType>> {
const runResult = await super.run();
if (this.sharedSource) {
const sourceCallsArg = Object.fromEntries(
Object.entries(this.sharedSource).map(([name, fn]) => [name, fn.mock.calls.map(args => args[0])]),
);
if (sourceCallsArg.addEntitiesToClient) {
sourceCallsArg.addEntitiesToClient = (sourceCallsArg.addEntitiesToClient as any).map(({ application, entities }) => ({
application: `Application[${application.baseName}]`,
entities: entities.map(entity => `Entity[${entity.name}]`),
}));
}
if (sourceCallsArg.addEntityToCache) {
sourceCallsArg.addEntityToCache = (sourceCallsArg.addEntityToCache as any).map(({ relationships, ...fields }) => ({
...fields,
relationships: relationships.map(rel => `Relationship[${rel.relationshipName}]`),
}));
}
const jhipsterRunResult = runResult as unknown as JHipsterRunResult;
jhipsterRunResult.sourceCallsArg = sourceCallsArg;
}
return runResult;
}
}

jhipster helpers provides features that can be used for blueprint like:

import {
  basicHelpers,
  /* files are not committed to disk */
  dryRunHelpers,
  /* prettier is ignored for improved speed */
  skipPrettierHelpers,
  /* disables commit and prettier */
  defaultHelpers,
} from 'generator-jhipster/testing';

defaultHelpers.run().withJHipsterConfig();

And drop blueprint test utils.
https://github.com/jhipster/generator-jhipster/blob/main/generators/generate-blueprint/templates/test/utils.mjs.ejs

Motivation for or Use Case
Related issues or PR
  • Checking this box is mandatory (this is just to show you read everything)
@mshima
Copy link
Member Author

mshima commented Nov 8, 2023

@DanielFran
Copy link
Member

@mshima approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: enhancement 🔧 $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ theme: blueprint 🐾 theme: jhipster-internals $100 https://www.jhipster.tech/bug-bounties/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants