yeoman-test 7.2.0 → 7.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -41,7 +41,7 @@ describe('generator test', () => {
41
41
  [.withLookups({})] // runs Environment lookups
42
42
  [.withOptions({})] // passes options to the generator
43
43
  [.withLocalConfig({})] // sets the generator config as soon as it is instantiated
44
- [.withPrompts()] // simulates the prompt answers
44
+ [.withAnswers()] // simulates the prompt answers
45
45
  [.build(runContext => { // instantiates Environment/Generator
46
46
  [runContext.env...] // does something with the environment
47
47
  [runContext.generator...] // does something with the generator
package/dist/helpers.d.ts CHANGED
@@ -10,8 +10,8 @@ import type { RunContextSettings } from './run-context.js';
10
10
  * Dependencies can be path (autodiscovery) or an array [<generator>, <name>]
11
11
  */
12
12
  export type Dependency = string | Parameters<Environment['registerStub']> | Parameters<Environment['register']>;
13
- type GeneratorNew<GenParameter extends YeomanGenerator = YeomanGenerator> = new (...args: ConstructorParameters<typeof YeomanGenerator<GenParameter['options']>>) => YeomanGenerator<GenParameter['options']>;
14
- type GeneratorBuilder<GenParameter extends YeomanGenerator = YeomanGenerator> = (...args: ConstructorParameters<typeof YeomanGenerator<GenParameter['options']>>) => YeomanGenerator<GenParameter['options']>;
13
+ type GeneratorNew<GenParameter extends YeomanGenerator = YeomanGenerator> = new (...args: ConstructorParameters<typeof YeomanGenerator<GenParameter['options']>>) => GenParameter;
14
+ type GeneratorBuilder<GenParameter extends YeomanGenerator = YeomanGenerator> = (...args: ConstructorParameters<typeof YeomanGenerator<GenParameter['options']>>) => GenParameter;
15
15
  export type GeneratorConstructor<GenParameter extends YeomanGenerator = YeomanGenerator> = GeneratorNew<GenParameter> | GeneratorBuilder<GenParameter>;
16
16
  /**
17
17
  * Collection of unit test helpers. (mostly related to Mocha syntax)
@@ -137,13 +137,13 @@ export declare class YeomanTest {
137
137
  * Run the provided Generator
138
138
  * @param GeneratorOrNamespace - Generator constructor or namespace
139
139
  */
140
- run<GeneratorType extends YeomanGenerator = YeomanGenerator>(GeneratorOrNamespace: string | GeneratorConstructor, settings?: RunContextSettings, envOptions?: Options): RunContext<GeneratorType>;
140
+ run<GeneratorType extends YeomanGenerator = YeomanGenerator>(GeneratorOrNamespace: string | GeneratorConstructor<GeneratorType>, settings?: RunContextSettings, envOptions?: Options): RunContext<GeneratorType>;
141
141
  /**
142
142
  * Prepare a run context
143
143
  * @param {String|Function} GeneratorOrNamespace - Generator constructor or namespace
144
144
  * @return {RunContext}
145
145
  */
146
- create<GeneratorType extends YeomanGenerator = YeomanGenerator>(GeneratorOrNamespace: string | GeneratorConstructor, settings?: RunContextSettings, envOptions?: Options): RunContext<GeneratorType>;
146
+ create<GeneratorType extends YeomanGenerator = YeomanGenerator>(GeneratorOrNamespace: string | GeneratorConstructor<GeneratorType>, settings?: RunContextSettings, envOptions?: Options): RunContext<GeneratorType>;
147
147
  }
148
148
  declare const _default: YeomanTest;
149
149
  export default _default;
package/dist/helpers.js CHANGED
@@ -9,6 +9,7 @@ import Environment from 'yeoman-environment';
9
9
  import { DummyPrompt, TestAdapter } from './adapter.js';
10
10
  import RunContext from './run-context.js';
11
11
  import testContext from './test-context.js';
12
+ const { cloneDeep } = _;
12
13
  /**
13
14
  * Collection of unit test helpers. (mostly related to Mocha syntax)
14
15
  * @class YeomanTest
@@ -206,7 +207,7 @@ export class YeomanTest {
206
207
  * const env = createTestEnv(require('yeoman-environment').createEnv);
207
208
  */
208
209
  createTestEnv(envContructor = this.createEnv, options = { localConfigOnly: true }) {
209
- const envOptions = _.cloneDeep(this.environmentOptions ?? {});
210
+ const envOptions = cloneDeep(this.environmentOptions ?? {});
210
211
  if (typeof options === 'boolean') {
211
212
  options = {
212
213
  newErrorHandler: true,
@@ -243,8 +244,8 @@ export class YeomanTest {
243
244
  * @param GeneratorOrNamespace - Generator constructor or namespace
244
245
  */
245
246
  run(GeneratorOrNamespace, settings, envOptions) {
246
- const contextSettings = _.cloneDeep(this.settings ?? {});
247
- const generatorOptions = _.cloneDeep(this.generatorOptions ?? {});
247
+ const contextSettings = cloneDeep(this.settings ?? {});
248
+ const generatorOptions = cloneDeep(this.generatorOptions ?? {});
248
249
  const RunContext = this.getRunContextType();
249
250
  const runContext = new RunContext(GeneratorOrNamespace, { ...contextSettings, ...settings }, envOptions, this).withOptions(generatorOptions);
250
251
  if (settings?.autoCleanup !== false) {
@@ -67,7 +67,7 @@ export declare class RunContextBase<GeneratorType extends Generator = Generator>
67
67
  * @param settings
68
68
  * @return {this}
69
69
  */
70
- constructor(generatorType: string | GeneratorConstructor | typeof Generator, settings?: RunContextSettings, envOptions?: Options, helpers?: YeomanTest);
70
+ constructor(generatorType: string | GeneratorConstructor<GeneratorType> | typeof Generator, settings?: RunContextSettings, envOptions?: Options, helpers?: YeomanTest);
71
71
  /**
72
72
  * Run the generator on the environment and promises a RunResult instance.
73
73
  * @return {PromiseRunResult} Promise a RunResult instance.
@@ -213,7 +213,9 @@ export declare class RunContextBase<GeneratorType extends Generator = Generator>
213
213
  /**
214
214
  * Add files to mem-fs.
215
215
  * Files will be resolved relative to targetDir.
216
- * @param files
216
+ *
217
+ * Files with Object content will be merged to existing content.
218
+ * To avoid merging, `JSON.stringify` the content.
217
219
  */
218
220
  withFiles(files: Record<string, string | Record<string, unknown>>): this;
219
221
  withFiles(relativePath: string, files: Record<string, string | Record<string, unknown>>): this;
@@ -224,6 +226,10 @@ export declare class RunContextBase<GeneratorType extends Generator = Generator>
224
226
  * @returns
225
227
  */
226
228
  withYoRc(content: string | Record<string, unknown>): this;
229
+ /**
230
+ * Add a generator config to .yo-rc.json
231
+ */
232
+ withYoRcConfig(key: string, content: Record<string, unknown>): this;
227
233
  /**
228
234
  * Commit mem-fs files.
229
235
  */
@@ -271,7 +277,7 @@ export declare class RunContextBase<GeneratorType extends Generator = Generator>
271
277
  private setDir;
272
278
  private _createRunResultOptions;
273
279
  }
274
- export default class RunContext<GeneratorType extends Generator> extends RunContextBase<GeneratorType> implements Promise<RunResult<GeneratorType>> {
280
+ export default class RunContext<GeneratorType extends Generator = Generator> extends RunContextBase<GeneratorType> implements Promise<RunResult<GeneratorType>> {
275
281
  then<TResult1 = RunResult<GeneratorType>, TResult2 = never>(onfulfilled?: ((value: RunResult<GeneratorType>) => TResult1 | PromiseLike<TResult1>) | undefined | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | undefined): Promise<TResult1 | TResult2>;
276
282
  catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | undefined): Promise<RunResult<GeneratorType> | TResult>;
277
283
  finally(onfinally?: (() => void) | undefined | undefined): Promise<RunResult<GeneratorType>>;
@@ -9,6 +9,7 @@ import tempDirectory from 'temp-dir';
9
9
  import MemFsEditor from 'mem-fs-editor';
10
10
  import RunResult from './run-result.js';
11
11
  import defaultHelpers from './helpers.js';
12
+ const { camelCase, kebabCase, merge: lodashMerge, set: lodashSet } = _;
12
13
  export class RunContextBase extends EventEmitter {
13
14
  mockedGenerators = {};
14
15
  env;
@@ -234,8 +235,8 @@ export class RunContextBase extends EventEmitter {
234
235
  // Add options as both kebab and camel case. This is to stay backward compatibles with
235
236
  // the switch we made to meow for options parsing.
236
237
  for (const key of Object.keys(options)) {
237
- options[_.camelCase(key)] = options[key];
238
- options[_.kebabCase(key)] = options[key];
238
+ options[camelCase(key)] = options[key];
239
+ options[kebabCase(key)] = options[key];
239
240
  }
240
241
  this.options = { ...this.options, ...options };
241
242
  return this;
@@ -346,7 +347,8 @@ export class RunContextBase extends EventEmitter {
346
347
  this.editor.write(resolvedFile, content);
347
348
  }
348
349
  else {
349
- this.editor.writeJSON(resolvedFile, content);
350
+ const fileContent = this.editor.readJSON(resolvedFile, {});
351
+ this.editor.writeJSON(resolvedFile, lodashMerge(fileContent, content));
350
352
  }
351
353
  }
352
354
  });
@@ -362,6 +364,13 @@ export class RunContextBase extends EventEmitter {
362
364
  '.yo-rc.json': content,
363
365
  });
364
366
  }
367
+ /**
368
+ * Add a generator config to .yo-rc.json
369
+ */
370
+ withYoRcConfig(key, content) {
371
+ const yoRcContent = lodashSet({}, key, content);
372
+ return this.withYoRc(yoRcContent);
373
+ }
365
374
  /**
366
375
  * Commit mem-fs files.
367
376
  */
@@ -37,7 +37,7 @@ export type RunResultOptions<GeneratorType extends Generator> = {
37
37
  /**
38
38
  * This class provides utilities for testing generated content.
39
39
  */
40
- export default class RunResult<GeneratorType extends Generator> {
40
+ export default class RunResult<GeneratorType extends Generator = Generator> {
41
41
  env: any;
42
42
  generator: GeneratorType;
43
43
  cwd: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yeoman-test",
3
- "version": "7.2.0",
3
+ "version": "7.3.0",
4
4
  "description": "Test utilities for Yeoman generators",
5
5
  "homepage": "http://yeoman.io/authoring/testing.html",
6
6
  "author": "The Yeoman Team",