diff --git a/src/run-context.ts b/src/run-context.ts index 0e97de8..76e63b2 100644 --- a/src/run-context.ts +++ b/src/run-context.ts @@ -22,7 +22,7 @@ import { create as createMemFsEditor, type MemFsEditorFile, type MemFsEditor } f import type { DefaultGeneratorApi, DefaultEnvironmentApi } from '../types/type-helpers.js'; import RunResult, { type RunResultOptions } from './run-result.js'; import defaultHelpers, { type CreateEnv, type Dependency, type YeomanTest } from './helpers.js'; -import { type DummyPromptOptions, type TestAdapterOptions } from './adapter.js'; +import { type DummyPromptCallback, type DummyPromptOptions, type TestAdapterOptions } from './adapter.js'; import testContext from './test-context.js'; /** @@ -76,6 +76,7 @@ export class RunContextBase; spawnStub?: any; mockedGeneratorFactory: MockedGeneratorFactory; + readonly askedQuestions: Array<{ name: string; answer: any }> = []; protected environmentPromise?: PromiseRunResult; @@ -664,13 +665,23 @@ export class RunContextBase { await this.prepare(); + const { askedQuestions, adapterOptions } = this; + const promptCallback: DummyPromptCallback = function (this, answer: any, options) { + const { question } = options; + if (question.name) { + askedQuestions.push({ name: question.name, answer }); + } + + return adapterOptions?.callback ? adapterOptions.callback.call(this, answer, options) : answer; + }; + const testEnv = await this.helpers.createTestEnv(this.envOptions.createEnv, { cwd: this.settings.forwardCwd ? this.targetDirectory : undefined, sharedFs: this.memFs, force: true, skipCache: true, skipInstall: true, - adapter: this.helpers.createTestAdapter({ ...this.adapterOptions, mockedAnswers: this.answers }), + adapter: this.helpers.createTestAdapter({ ...this.adapterOptions, mockedAnswers: this.answers, callback: promptCallback }), ...this.envOptions, } as any); this.env = this.envCB ? (await this.envCB(testEnv)) ?? testEnv : testEnv; diff --git a/test/run-context.spec.ts b/test/run-context.spec.ts index 76aeefb..72ed27c 100644 --- a/test/run-context.spec.ts +++ b/test/run-context.spec.ts @@ -601,6 +601,30 @@ describe('RunContext', function () { assert.ok(promptSpy.getCall(0).thisValue instanceof DummyPrompt); }); }); + + it.only('sets askedQuestions', async function () { + Dummy.prototype.askFor = function () { + return this.prompt([ + { + name: 'yeoman', + type: 'input', + message: 'Hey!', + }, + { + name: 'yeoman2', + type: 'input', + message: 'Hey!', + }, + ]); + }; + + await ctx.withAnswers({ yeoman: 'no please' }).toPromise(); + + assert.deepEqual(ctx.askedQuestions, [ + { name: 'yeoman', answer: 'no please' }, + { name: 'yeoman2', answer: undefined }, + ]); + }); }); describe('#withMockedGenerators()', function () {