In a generator test, how can you supply answers to prompts programmatically?

Prepare for the Yeoman (YN) Test. Use flashcards and multiple-choice questions, all with hints and explanations, to get ready for your upcoming exam. Enhance your learning experience!

Multiple Choice

In a generator test, how can you supply answers to prompts programmatically?

Explanation:
When testing a generator, you supply the answers to prompts by using the withPrompts helper from yeoman-test. This lets the test automatically provide the responses the generator asks for, so the test runs without any interactive input and outcomes are consistent each run. You pass an object mapping each prompt name to the answer you want the generator to receive, for example providing the project name, whether to include tests, etc. The test then proceeds as if a user answered those questions. Example in practice: await helpers.run(pathToGenerator).withPrompts({ projectName: 'MyApp', includeTests: true }); This approach is the standard, built-in way to drive prompts in tests. Editing the generator to bypass prompts is invasive and makes tests fragile. A mock prompts API could work in some setups, but using withPrompts is the idiomatic, supported method that keeps tests clean and deterministic. Asking the user during tests defeats automation.

When testing a generator, you supply the answers to prompts by using the withPrompts helper from yeoman-test. This lets the test automatically provide the responses the generator asks for, so the test runs without any interactive input and outcomes are consistent each run. You pass an object mapping each prompt name to the answer you want the generator to receive, for example providing the project name, whether to include tests, etc. The test then proceeds as if a user answered those questions.

Example in practice: await helpers.run(pathToGenerator).withPrompts({ projectName: 'MyApp', includeTests: true });

This approach is the standard, built-in way to drive prompts in tests. Editing the generator to bypass prompts is invasive and makes tests fragile. A mock prompts API could work in some setups, but using withPrompts is the idiomatic, supported method that keeps tests clean and deterministic. Asking the user during tests defeats automation.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy