UNPKG

9.67 kBTypeScriptView Raw
1import { type mock } from 'node:test';
2import type { Store } from 'mem-fs';
3import { type MemFsEditor, type MemFsEditorFile } from 'mem-fs-editor';
4import type { BaseEnvironmentOptions, BaseGenerator, GetGeneratorConstructor } from '@yeoman/types';
5import type { DefaultEnvironmentApi } from '../types/type-helpers.js';
6import { type RunContextSettings } from './run-context.js';
7import { type YeomanTest } from './helpers.js';
8import { type AskedQuestions } from './adapter.js';
9/**
10 * Provides options for `RunResult`s.
11 */
12export type RunResultOptions<GeneratorType extends BaseGenerator> = {
13 generator: GeneratorType;
14 /**
15 * The environment of the generator.
16 */
17 env: DefaultEnvironmentApi;
18 envOptions: BaseEnvironmentOptions;
19 /**
20 * The working directory after running the generator.
21 */
22 cwd: string;
23 /**
24 * The working directory before on running the generator.
25 */
26 oldCwd: string;
27 /**
28 * The file-system of the generator.
29 */
30 memFs: Store<MemFsEditorFile>;
31 fs?: MemFsEditor;
32 /**
33 * The mocked generators of the context.
34 */
35 mockedGenerators: Record<string, BaseGenerator>;
36 spawnStub?: any;
37 settings: RunContextSettings;
38 helpers: YeomanTest;
39 askedQuestions: AskedQuestions;
40};
41/**
42 * This class provides utilities for testing generated content.
43 */
44export default class RunResult<GeneratorType extends BaseGenerator = BaseGenerator> {
45 env: any;
46 generator: GeneratorType;
47 cwd: string;
48 oldCwd: string;
49 memFs: Store<MemFsEditorFile>;
50 fs: MemFsEditor;
51 mockedGenerators: any;
52 options: RunResultOptions<GeneratorType>;
53 spawnStub?: any;
54 readonly askedQuestions: AskedQuestions;
55 constructor(options: RunResultOptions<GeneratorType>);
56 /**
57 * Create another RunContext reusing the settings.
58 * See helpers.create api
59 */
60 create<G extends BaseGenerator = GeneratorType>(GeneratorOrNamespace: string | GetGeneratorConstructor<G>, settings?: RunContextSettings, environmentOptions?: BaseEnvironmentOptions): import("./run-context.js").default<G>;
61 getSpawnArgsUsingDefaultImplementation(): unknown[][];
62 /**
63 * Return an object with fs changes.
64 * @param {Function} filter - parameter forwarded to mem-fs-editor#dump
65 */
66 getSnapshot(filter?: any): Record<string, {
67 contents: string;
68 stateCleared: string;
69 }>;
70 /**
71 * Return an object with filenames with state.
72 * @param {Function} filter - parameter forwarded to mem-fs-editor#dump
73 * @returns {Object}
74 */
75 getStateSnapshot(filter?: any): Record<string, {
76 stateCleared?: string;
77 state?: string;
78 }>;
79 /**
80 * Either dumps the contents of the specified files or the name and the contents of each file to the console.
81 */
82 dumpFiles(...files: string[]): this;
83 /**
84 * Dumps the name of each file to the console.
85 */
86 dumpFilenames(): this;
87 /**
88 * Reverts to old cwd.
89 * @returns this
90 */
91 restore(): this;
92 /**
93 * Deletes the test directory recursively.
94 */
95 cleanup(): this;
96 _fileName(filename: any): any;
97 _readFile(filename: any, json?: boolean): any;
98 _exists(filename: any): boolean;
99 /**
100 * Assert that a file exists
101 * @param path - path to a file
102 * @example
103 * result.assertFile('templates/user.hbs');
104 *
105 * @also
106 *
107 * Assert that each files in the array exists
108 * @param paths - an array of paths to files
109 * @example
110 * result.assertFile(['templates/user.hbs', 'templates/user/edit.hbs']);
111 */
112 assertFile(path: string | string[]): void;
113 /**
114 * Assert that a file doesn't exist
115 * @param file - path to a file
116 * @example
117 * result.assertNoFile('templates/user.hbs');
118 *
119 * @also
120 *
121 * Assert that each of an array of files doesn't exist
122 * @param pairs - an array of paths to files
123 * @example
124 * result.assertNoFile(['templates/user.hbs', 'templates/user/edit.hbs']);
125 */
126 assertNoFile(files: string | string[]): void;
127 /**
128 * Assert that a file's content matches a regex or string
129 * @param file - path to a file
130 * @param reg - regex / string that will be used to search the file
131 * @example
132 * result.assertFileContent('models/user.js', /App\.User = DS\.Model\.extend/);
133 * result.assertFileContent('models/user.js', 'App.User = DS.Model.extend');
134 *
135 * @also
136 *
137 * Assert that each file in an array of file-regex pairs matches its corresponding regex
138 * @param pairs - an array of arrays, where each subarray is a [String, RegExp] pair
139 * @example
140 * var arg = [
141 * [ 'models/user.js', /App\.User = DS\.Model\.extend/ ],
142 * [ 'controllers/user.js', /App\.UserController = Ember\.ObjectController\.extend/ ]
143 * ]
144 * result.assertFileContent(arg);
145 */
146 assertFileContent(file: string, reg: string | RegExp): void;
147 assertFileContent(pairs: Array<[string, string | RegExp]>): void;
148 /**
149 * Assert that a file's content is the same as the given string
150 * @param file - path to a file
151 * @param expectedContent - the expected content of the file
152 * @example
153 * result.assertEqualsFileContent(
154 * 'data.js',
155 * 'const greeting = "Hello";\nexport default { greeting }'
156 * );
157 *
158 * @also
159 *
160 * Assert that each file in an array of file-string pairs equals its corresponding string
161 * @param pairs - an array of arrays, where each subarray is a [String, String] pair
162 * @example
163 * result.assertEqualsFileContent([
164 * ['data.js', 'const greeting = "Hello";\nexport default { greeting }'],
165 * ['user.js', 'export default {\n name: 'Coleman',\n age: 0\n}']
166 * ]);
167 */
168 assertEqualsFileContent(file: string, expectedContent: string): void;
169 assertEqualsFileContent(pairs: Array<[string, string]>): void;
170 /**
171 * Assert that a file's content does not match a regex / string
172 * @param file - path to a file
173 * @param reg - regex / string that will be used to search the file
174 * @example
175 * result.assertNoFileContent('models/user.js', /App\.User = DS\.Model\.extend/);
176 * result.assertNoFileContent('models/user.js', 'App.User = DS.Model.extend');
177 *
178 * @also
179 *
180 * Assert that each file in an array of file-regex pairs does not match its corresponding regex
181 * @param pairs - an array of arrays, where each subarray is a [String, RegExp] pair
182 * var arg = [
183 * [ 'models/user.js', /App\.User \ DS\.Model\.extend/ ],
184 * [ 'controllers/user.js', /App\.UserController = Ember\.ObjectController\.extend/ ]
185 * ]
186 * result.assertNoFileContent(arg);
187 */
188 assertNoFileContent(file: string, reg: RegExp | string): void;
189 assertNoFileContent(pairs: Array<[string, string | RegExp]>): void;
190 /**
191 * Assert that two strings are equal after standardization of newlines
192 * @param value - a string
193 * @param expected - the expected value of the string
194 * @example
195 * result.assertTextEqual('I have a yellow cat', 'I have a yellow cat');
196 */
197 assertTextEqual(value: string, expected: string): void;
198 /**
199 * Assert an object contains the provided keys
200 * @param obj Object that should match the given pattern
201 * @param content An object of key/values the object should contains
202 */
203 assertObjectContent(object: Record<string, unknown>, content: Record<string, any>): void;
204 /**
205 * Assert an object does not contain the provided keys
206 * @param obj Object that should not match the given pattern
207 * @param content An object of key/values the object should not contain
208 */
209 assertNoObjectContent(object: Record<string, unknown>, content: Record<string, any>): void;
210 /**
211 * Assert a JSON file contains the provided keys
212 * @param filename
213 * @param content An object of key/values the file should contains
214 */
215 assertJsonFileContent(filename: string, content: Record<string, any>): void;
216 /**
217 * Assert a JSON file does not contain the provided keys
218 * @param filename
219 * @param content An object of key/values the file should not contain
220 */
221 assertNoJsonFileContent(filename: string, content: Record<string, any>): void;
222 /**
223 * Get the generator mock
224 * @param generator - the namespace of the mocked generator
225 * @returns the generator mock
226 */
227 getGeneratorMock(generator: string): ReturnType<typeof mock.fn>['mock'];
228 /**
229 * Get the number of times a mocked generator was composed
230 * @param generator - the namespace of the mocked generator
231 * @returns the number of times the generator was composed
232 */
233 getGeneratorComposeCount(generator: string): number;
234 /**
235 * Assert that a generator was composed
236 * @param generator - the namespace of the mocked generator
237 */
238 assertGeneratorComposed(generator: string): void;
239 /**
240 * Assert that a generator was composed
241 * @param generator - the namespace of the mocked generator
242 */
243 assertGeneratorNotComposed(generator: string): void;
244 /**
245 * Assert that a generator was composed only once
246 * @param generator - the namespace of the mocked generator
247 */
248 assertGeneratorComposedOnce(generator: string): void;
249 /**
250 * Assert that a generator was composed multiple times
251 * @returns an array of the names of the mocked generators that were composed
252 */
253 getComposedGenerators(): string[];
254}