Skip to content

Commit

Permalink
testing: add useEnvironmentBuilder option
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Sep 26, 2024
1 parent 50de0d2 commit 090811f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion generators/base/blueprints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
29 changes: 25 additions & 4 deletions lib/testing/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,25 @@ import getGenerator, { getGeneratorRelativeFolder } from './get-generator.js';

type GeneratorTestType = YeomanGenerator<JHipsterGeneratorOptions>;
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<GeneratorType extends CoreGenerator = CoreGenerator> = RunResult<GeneratorType> & {
/**
Expand Down Expand Up @@ -64,6 +82,8 @@ const defaultSharedApplication = Object.fromEntries(['CLIENT_WEBPACK_DIR'].map(k
let defaultMockFactory: (original?: any) => any;
let defaultAccumulateMockArgs: (mocks: Record<string, any>) => Record<string, any>;

const createEnvBuilderEnvironment = (...args) => EnvironmentBuilder.createEnv(...args);

export const defineDefaults = async (
defaults: {
/** @deprecated mock from `node:test` is used internally */
Expand Down Expand Up @@ -413,7 +433,7 @@ class JHipsterTest extends YeomanTest {
run<GeneratorType extends YeomanGenerator<GeneratorTestOptions> = YeomanGenerator<GeneratorTestOptions>>(
GeneratorOrNamespace: string | GetGeneratorConstructor<GeneratorType>,
settings?: RunContextSettings | undefined,
envOptions?: BaseEnvironmentOptions | undefined,
envOptions?: (BaseEnvironmentOptions & { createEnv?: any }) | undefined,
): JHipsterRunContext {
return super
.run<GeneratorType>(GeneratorOrNamespace, settings, envOptions)
Expand All @@ -438,9 +458,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();
}
Expand Down

0 comments on commit 090811f

Please sign in to comment.