diff --git a/generators/generate-blueprint/__snapshots__/generator.spec.ts.snap b/generators/generate-blueprint/__snapshots__/generator.spec.ts.snap index 7a5521ffb139..2ec4f41544c5 100644 --- a/generators/generate-blueprint/__snapshots__/generator.spec.ts.snap +++ b/generators/generate-blueprint/__snapshots__/generator.spec.ts.snap @@ -863,6 +863,9 @@ exports[`generator - generate-blueprint with all option should match snapshot 1` "vitest.config.ts": { "stateCleared": "modified", }, + "vitest.test-setup.ts": { + "stateCleared": "modified", + }, } `; @@ -907,5 +910,8 @@ exports[`generator - generate-blueprint with default config should write files a "vitest.config.ts": { "stateCleared": "modified", }, + "vitest.test-setup.ts": { + "stateCleared": "modified", + }, } `; diff --git a/generators/generate-blueprint/files.ts b/generators/generate-blueprint/files.ts index 49a75734e3b0..91f401dec16a 100644 --- a/generators/generate-blueprint/files.ts +++ b/generators/generate-blueprint/files.ts @@ -30,6 +30,7 @@ export const files = asWriteFilesSection({ 'README.md', 'tsconfig.json', 'vitest.config.ts', + 'vitest.test-setup.ts', '.blueprint/cli/commands.mjs', '.blueprint/generate-sample/command.mjs', '.blueprint/generate-sample/generator.mjs', diff --git a/generators/generate-blueprint/templates/generators/generator/generator.spec.mjs.ejs b/generators/generate-blueprint/templates/generators/generator/generator.spec.mjs.ejs index 9fd887094e3d..d49fcad21c07 100644 --- a/generators/generate-blueprint/templates/generators/generator/generator.spec.mjs.ejs +++ b/generators/generate-blueprint/templates/generators/generator/generator.spec.mjs.ejs @@ -31,19 +31,18 @@ describe('SubGenerator <%= subGenerator %> of <%= application.baseName %> JHipst describe('run', () => { beforeAll(async function() { await helpers +<%_ if (subGenerator === 'jdl') { _%> + .runJDL('application { }') +<%_ } else { _%> .run(<%= customGenerator ? 'SUB_GENERATOR_NAMESPACE' : 'BLUEPRINT_NAMESPACE' %>) +<%_ } _%> .withJHipsterConfig() .withOptions({ ignoreNeedlesError: true, -<%_ if (!customGenerator) { _%> - blueprint: ['<%= application.baseName %>'], -<%_ } _%> -<%_ if (subGenerator === 'jdl') { _%> - inline: 'application { }', -<%_ } _%> }) .withJHipsterGenerators() - .withParentBlueprintLookup(); + .withConfiguredBlueprint() + .withBlueprintConfig(); }); it('should succeed', () => { diff --git a/generators/generate-blueprint/templates/vitest.config.ts.ejs b/generators/generate-blueprint/templates/vitest.config.ts.ejs index d46556a90a3b..d1fa83cf8397 100644 --- a/generators/generate-blueprint/templates/vitest.config.ts.ejs +++ b/generators/generate-blueprint/templates/vitest.config.ts.ejs @@ -5,5 +5,6 @@ export default defineConfig({ pool: 'forks', hookTimeout: 20000, exclude: [...defaultExclude.filter(val => val !== '**/cypress/**'), '**/templates/**', '**/resources/**'], + setupFiles: ['./vitest.test-setup.ts'], }, }); diff --git a/generators/generate-blueprint/templates/vitest.test-setup.ts.ejs b/generators/generate-blueprint/templates/vitest.test-setup.ts.ejs new file mode 100644 index 000000000000..0240b97c5722 --- /dev/null +++ b/generators/generate-blueprint/templates/vitest.test-setup.ts.ejs @@ -0,0 +1,7 @@ +import { fileURLToPath } from 'node:url'; +import { defineDefaults } from 'generator-jhipster/testing'; + +defineDefaults({ + blueprint: 'generator-jhipster-<%= baseName %>', + blueprintPackagePath: fileURLToPath(new URL('./', import.meta.url)), +}); diff --git a/lib/testing/helpers.ts b/lib/testing/helpers.ts index 8c6a00934427..956f6b3bdc86 100644 --- a/lib/testing/helpers.ts +++ b/lib/testing/helpers.ts @@ -1,4 +1,5 @@ -import { basename, dirname, isAbsolute, join } from 'path'; +import assert from 'node:assert'; +import { basename, dirname, isAbsolute, join } from 'node:path'; import { mock } from 'node:test'; import { merge, set, snakeCase } from 'lodash-es'; import type { RunContextSettings, RunResult } from 'yeoman-test'; @@ -65,6 +66,14 @@ type JHipsterRunResult = Om createJHipster: (ns: string, options?: WithJHipsterGenerators) => JHipsterRunContext; }; +type HelpersDefaults = { + /** Blueprint namespace */ + blueprint?: string; + /** Path where blueprint's generators folder is located */ + blueprintPackagePath?: string; + entrypointGenerator?: string; +}; + const runResult = result as JHipsterRunResult; export { runResult, runResult as result }; @@ -90,6 +99,11 @@ const defaultSharedApplication = Object.fromEntries(['CLIENT_WEBPACK_DIR'].map(k let defaultMockFactory: (original?: any) => any; let defaultAccumulateMockArgs: (mocks: Record) => Record; +let helpersDefaults: HelpersDefaults = {}; + +export const resetDefaults = () => { + helpersDefaults = {}; +}; const createEnvBuilderEnvironment = (...args) => EnvironmentBuilder.createEnv(...args); @@ -99,9 +113,11 @@ export const defineDefaults = async ( mockFactory?: any; /** @deprecated mock from `node:test` is used internally */ accumulateMockArgs?: (mock: Record) => Record; - } = {}, + } & HelpersDefaults = {}, ) => { - const { mockFactory, accumulateMockArgs } = defaults; + const { mockFactory, accumulateMockArgs, ...rest } = defaults; + Object.assign(helpersDefaults, rest); + if (mockFactory) { defaultMockFactory = mockFactory; } else if (!defaultMockFactory) { @@ -253,6 +269,36 @@ class JHipsterRunContext extends RunContext { return this.withJHipsterGenerators(); } + withBlueprintConfig(config: Record): this { + const { blueprint } = helpersDefaults; + assert(blueprint, 'Blueprint must be configured'); + return this.withYoRcConfig(blueprint, config); + } + + /** + * Use configured default blueprint. + */ + withConfiguredBlueprint(): this { + const { blueprint, blueprintPackagePath, entrypointGenerator } = helpersDefaults; + assert(blueprintPackagePath, 'Blueprint generators package path must be configured'); + assert(blueprint, 'Blueprint must be configured'); + + if (entrypointGenerator) { + this.withOptions({ entrypointGenerator }); + } + + return this.withLookups([ + { + packagePaths: [blueprintPackagePath], + // @ts-expect-error lookups is not exported + lookups: [`generators`, `generators/*/generators`], + customizeNamespace: ns => ns?.replaceAll(':generators:', ':'), + }, + ]).withOptions({ + blueprint: [blueprint], + }); + } + /** * Lookup generators at generator-jhipster's parent at a npm repository * @param lookups generators relative folder diff --git a/test/__snapshots__/api.spec.js.snap b/test/__snapshots__/api.spec.js.snap index d58796a40c63..600ec1669a4a 100644 --- a/test/__snapshots__/api.spec.js.snap +++ b/test/__snapshots__/api.spec.js.snap @@ -104,6 +104,7 @@ exports[`public api generator-jhipster/generators should match snapshot 2`] = ` "matchWrittenFiles", "parseIssue", "prepareSample", + "resetDefaults", "result", "runResult", "setGithubTaskOutput",