From 3728cccf2ef33e9a490374f923e71822fc73e024 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Wed, 25 Sep 2024 22:54:35 -0300 Subject: [PATCH] testing: add useEnvironmentBuilder option --- generators/base/blueprints.spec.ts | 2 +- lib/testing/helpers.ts | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/generators/base/blueprints.spec.ts b/generators/base/blueprints.spec.ts index 5b8b19bf19d8..102cc81fc1a7 100644 --- a/generators/base/blueprints.spec.ts +++ b/generators/base/blueprints.spec.ts @@ -292,7 +292,7 @@ describe('generator - base - local blueprint', () => { describe('generates application', () => { before(async () => { await helpers - .runJHipster(BLUEPRINT_NS) + .runJHipster(BLUEPRINT_NS, { useEnvironmentBuilder: true }) .withFiles({ '.blueprint/app/index.mjs': BLUEPRINT_CONTENTS }) .commitFiles() .withJHipsterConfig(); diff --git a/lib/testing/helpers.ts b/lib/testing/helpers.ts index 9fd8c0c22cd9..251ba699ae50 100644 --- a/lib/testing/helpers.ts +++ b/lib/testing/helpers.ts @@ -23,7 +23,25 @@ import getGenerator, { getGeneratorRelativeFolder } from './get-generator.js'; type GeneratorTestType = YeomanGenerator; type GeneratorTestOptions = JHipsterGeneratorOptions; -type WithJHipsterGenerators = { useDefaultMocks?: boolean; actualGeneratorsList?: string[]; useMock?: (ns: string) => boolean }; +type WithJHipsterGenerators = { + /** + * Apply default mocks. + */ + useDefaultMocks?: boolean; + /** + * List of generators to don't mock. + */ + actualGeneratorsList?: string[]; + /** + * Filter to mock a generator. + */ + useMock?: (ns: string) => boolean; + /** + * Use the EnviromentBuilder default preparation to create the environment. + * Includes local and dev blueprints. + */ + useEnvironmentBuilder?: boolean; +}; type JHipsterRunResult = RunResult & { /** @@ -65,6 +83,8 @@ const defaultSharedApplication = Object.fromEntries(['CLIENT_WEBPACK_DIR'].map(k let defaultMockFactory: (original?: any) => any; let defaultAccumulateMockArgs: (mocks: Record) => Record; +const createEnvBuilderEnvironment = (...args) => EnvironmentBuilder.createEnv(...args); + export const defineDefaults = async ( defaults: { /** @deprecated mock from `node:test` is used internally */ @@ -414,7 +434,7 @@ class JHipsterTest extends YeomanTest { run = YeomanGenerator>( GeneratorOrNamespace: string | GetGeneratorConstructor, settings?: RunContextSettings | undefined, - envOptions?: BaseEnvironmentOptions | undefined, + envOptions?: (BaseEnvironmentOptions & { createEnv?: any }) | undefined, ): JHipsterRunContext { return super .run(GeneratorOrNamespace, settings, envOptions) @@ -439,9 +459,10 @@ class JHipsterTest extends YeomanTest { jhipsterGenerator = jhipsterGenerator.startsWith('jhipster:') ? jhipsterGenerator : `jhipster:${jhipsterGenerator}`; } const isWithJHipsterGenerators = (opt: any): opt is WithJHipsterGenerators | undefined => - opt === undefined || 'actualGeneratorsList' in opt || 'useMock' in opt || 'useDefaultMocks' in opt; + opt === undefined || 'actualGeneratorsList' in opt || 'useMock' in opt || 'useDefaultMocks' in opt || 'useEnvironmentBuilder' in opt; if (isWithJHipsterGenerators(settings)) { - return this.run(jhipsterGenerator).withJHipsterGenerators(settings); + const createEnv = settings?.useEnvironmentBuilder ? createEnvBuilderEnvironment : undefined; + return this.run(jhipsterGenerator, undefined, { createEnv }).withJHipsterGenerators(settings); } return this.run(getGenerator(jhipsterGenerator), settings, envOptions).withJHipsterGenerators(); }