UNPKG

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