UNPKG

6.78 kBTypeScriptView Raw
1import { mock } from 'node:test';
2import type { BaseEnvironment, BaseEnvironmentOptions, BaseGenerator, BaseGeneratorOptions, GetGeneratorConstructor, InstantiateOptions, PromptAnswers } from '@yeoman/types';
3import type { DefaultEnvironmentApi, DefaultGeneratorApi } from '../types/type-helpers.js';
4import { type DummyPromptOptions, TestAdapter, type TestAdapterOptions } from './adapter.js';
5import RunContext, { BasicRunContext, type RunContextSettings } from './run-context.js';
6export type CreateEnv = (options: BaseEnvironmentOptions) => Promise<BaseEnvironment>;
7/**
8 * Dependencies can be path (autodiscovery) or an array [<generator>, <name>]
9 */
10export type Dependency = string | Parameters<DefaultEnvironmentApi['register']>;
11/**
12 * Collection of unit test helpers. (mostly related to Mocha syntax)
13 * @class YeomanTest
14 */
15export declare class YeomanTest {
16 settings?: RunContextSettings;
17 environmentOptions?: BaseEnvironmentOptions;
18 generatorOptions?: BaseGeneratorOptions;
19 adapterOptions?: Omit<TestAdapterOptions, 'mockedAnswers'>;
20 /**
21 * @deprecated
22 * Create a function that will clean up the test directory,
23 * cd into it. Intended for use
24 * as a callback for the mocha `before` hook.
25 *
26 * @param dir - path to the test directory
27 * @returns mocha callback
28 */
29 setUpTestDirectory(dir: string): () => void;
30 /**
31 * @deprecated
32 * Clean-up the test directory and cd into it.
33 * Call given callback after entering the test directory.
34 * @param dir - path to the test directory
35 * @param cb - callback executed after setting working directory to dir
36 * @example
37 * testDirectory(path.join(__dirname, './temp'), function () {
38 * fs.writeFileSync('testfile', 'Roses are red.');
39 * });
40 */
41 testDirectory(dir: string, callback?: (error?: any) => unknown): unknown;
42 /**
43 * @deprecated
44 * Answer prompt questions for the passed-in generator
45 * @param generator - a Yeoman generator or environment
46 * @param answers - an object where keys are the
47 * generators prompt names and values are the answers to
48 * the prompt questions
49 * @param options - Options or callback
50 * @example
51 * mockPrompt(angular, {'bootstrap': 'Y', 'compassBoostrap': 'Y'});
52 */
53 mockPrompt(environmentOrGenerator: BaseGenerator | DefaultEnvironmentApi, mockedAnswers?: PromptAnswers, options?: DummyPromptOptions): void;
54 /**
55 * @deprecated
56 * Restore defaults prompts on a generator.
57 * @param generator or environment
58 */
59 restorePrompt(environmentOrGenerator: BaseGenerator | DefaultEnvironmentApi): void;
60 /**
61 * @deprecated
62 * Provide mocked values to the config
63 * @param generator - a Yeoman generator
64 * @param localConfig - localConfig - should look just like if called config.getAll()
65 */
66 mockLocalConfig(generator: BaseGenerator, localConfig: any): void;
67 /**
68 * Create a mocked generator
69 */
70 createMockedGenerator(GeneratorClass?: any): ReturnType<typeof mock.fn>;
71 /**
72 * Create a simple, dummy generator
73 */
74 createDummyGenerator<GenParameter extends BaseGenerator = DefaultGeneratorApi>(Generator?: GetGeneratorConstructor<GenParameter>, contents?: Record<string, (...arguments_: any[]) => void>): new (...arguments_: any[]) => GenParameter;
75 /**
76 * Create a generator, using the given dependencies and controller arguments
77 * Dependecies can be path (autodiscovery) or an array [{generator}, {name}]
78 *
79 * @param name - the name of the generator
80 * @param dependencies - paths to the generators dependencies
81 * @param args - arguments to the generator;
82 * if String, will be split on spaces to create an Array
83 * @param options - configuration for the generator
84 * @param localConfigOnly - passes localConfigOnly to the generators
85 * @example
86 * var deps = ['../../app',
87 * '../../common',
88 * '../../controller',
89 * '../../main',
90 * [createDummyGenerator(), 'testacular:app']
91 * ];
92 * var angular = createGenerator('angular:app', deps);
93 */
94 createGenerator<GeneratorType extends BaseGenerator = DefaultGeneratorApi>(name: string | GetGeneratorConstructor<GeneratorType>, options?: {
95 dependencies?: Dependency[];
96 localConfigOnly?: boolean;
97 } & InstantiateOptions<GeneratorType>): Promise<GeneratorType>;
98 /**
99 * Shortcut to the Environment's createEnv.
100 *
101 * @param {...any} args - environment constructor arguments.
102 * @returns {Object} environment instance
103 *
104 * Use to test with specific Environment version:
105 * let createEnv;
106 * before(() => {
107 * createEnv = stub(helper, 'createEnv').callsFake(Environment.creatEnv);
108 * });
109 * after(() => {
110 * createEnv.restore();
111 * });
112 */
113 createEnv(options: BaseEnvironmentOptions): Promise<DefaultEnvironmentApi>;
114 /**
115 * Creates a test environment.
116 *
117 * @param {Function} - environment constructor method.
118 * @param {Object} - Options to be passed to the environment
119 * const env = createTestEnv(require('yeoman-environment').createEnv);
120 */
121 createTestEnv(environmentContructor?: CreateEnv, options?: BaseEnvironmentOptions): Promise<BaseEnvironment>;
122 /**
123 * Creates a TestAdapter using helpers default options.
124 */
125 createTestAdapter(options?: TestAdapterOptions): TestAdapter;
126 /**
127 * Get RunContext type
128 * @return {RunContext}
129 */
130 getRunContextType(): typeof RunContext;
131 /**
132 * Run the provided Generator
133 * @param GeneratorOrNamespace - Generator constructor or namespace
134 */
135 run<GeneratorType extends BaseGenerator = DefaultGeneratorApi>(GeneratorOrNamespace: string | GetGeneratorConstructor<GeneratorType>, settings?: RunContextSettings, environmentOptions?: BaseEnvironmentOptions): RunContext<GeneratorType>;
136 /**
137 * Prepare a run context
138 * @param {String|Function} GeneratorOrNamespace - Generator constructor or namespace
139 * @return {RunContext}
140 */
141 create<GeneratorType extends BaseGenerator = DefaultGeneratorApi>(GeneratorOrNamespace: string | GetGeneratorConstructor<GeneratorType>, settings?: RunContextSettings, environmentOptions?: BaseEnvironmentOptions): RunContext<GeneratorType>;
142 /**
143 * Prepare temporary dir without generator support.
144 * Generator and environment will be undefined.
145 */
146 prepareTemporaryDir(settings?: RunContextSettings): BasicRunContext<BaseGenerator>;
147}
148declare const defaultHelpers: YeomanTest;
149export default defaultHelpers;
150export declare const createHelpers: (options: any) => YeomanTest;