yeoman-test
Version:
Test utilities for Yeoman generators
222 lines (221 loc) • 8.44 kB
TypeScript
import type { Store } from 'mem-fs';
import { type MemFsEditorFile, type MemFsEditor } from 'mem-fs-editor';
import type { BaseEnvironmentOptions, BaseGenerator, GetGeneratorConstructor } from '@yeoman/types';
import type { DefaultEnvironmentApi, DefaultGeneratorApi } from '../types/type-helpers.js';
import { type RunContextSettings } from './run-context.js';
import { type YeomanTest } from './helpers.js';
import { type AskedQuestions } from './adapter.js';
/**
* Provides options for `RunResult`s.
*/
export type RunResultOptions<GeneratorType extends BaseGenerator> = {
generator: GeneratorType;
/**
* The environment of the generator.
*/
env: DefaultEnvironmentApi;
envOptions: BaseEnvironmentOptions;
/**
* The working directory after running the generator.
*/
cwd: string;
/**
* The working directory before on running the generator.
*/
oldCwd: string;
/**
* The file-system of the generator.
*/
memFs: Store<MemFsEditorFile>;
fs?: MemFsEditor;
/**
* The mocked generators of the context.
*/
mockedGenerators: Record<string, BaseGenerator>;
spawnStub?: any;
settings: RunContextSettings;
helpers: YeomanTest;
askedQuestions: AskedQuestions;
};
/**
* This class provides utilities for testing generated content.
*/
export default class RunResult<GeneratorType extends BaseGenerator = BaseGenerator> {
env: any;
generator: GeneratorType;
cwd: string;
oldCwd: string;
memFs: Store<MemFsEditorFile>;
fs: MemFsEditor;
mockedGenerators: any;
options: RunResultOptions<GeneratorType>;
spawnStub?: any;
readonly askedQuestions: AskedQuestions;
constructor(options: RunResultOptions<GeneratorType>);
/**
* Create another RunContext reusing the settings.
* See helpers.create api
*/
create<GeneratorType extends BaseGenerator = DefaultGeneratorApi>(GeneratorOrNamespace: string | GetGeneratorConstructor<GeneratorType>, settings?: RunContextSettings, envOptions?: BaseEnvironmentOptions): import("./run-context.js").default<GeneratorType>;
getSpawnArgsUsingDefaultImplementation(): any;
/**
* Return an object with fs changes.
* @param {Function} filter - parameter forwarded to mem-fs-editor#dump
*/
getSnapshot(filter?: any): Record<string, {
contents: string;
stateCleared: string;
}>;
/**
* Return an object with filenames with state.
* @param {Function} filter - parameter forwarded to mem-fs-editor#dump
* @returns {Object}
*/
getStateSnapshot(filter?: any): Record<string, {
stateCleared?: string;
state?: string;
}>;
/**
* Either dumps the contents of the specified files or the name and the contents of each file to the console.
*/
dumpFiles(...files: string[]): this;
/**
* Dumps the name of each file to the console.
*/
dumpFilenames(): this;
/**
* Reverts to old cwd.
* @returns this
*/
restore(): this;
/**
* Deletes the test directory recursively.
*/
cleanup(): this;
_fileName(filename: any): any;
_readFile(filename: any, json?: boolean): any;
_exists(filename: any): boolean;
/**
* Assert that a file exists
* @param path - path to a file
* @example
* result.assertFile('templates/user.hbs');
*
* @also
*
* Assert that each files in the array exists
* @param paths - an array of paths to files
* @example
* result.assertFile(['templates/user.hbs', 'templates/user/edit.hbs']);
*/
assertFile(path: string | string[]): void;
/**
* Assert that a file doesn't exist
* @param file - path to a file
* @example
* result.assertNoFile('templates/user.hbs');
*
* @also
*
* Assert that each of an array of files doesn't exist
* @param pairs - an array of paths to files
* @example
* result.assertNoFile(['templates/user.hbs', 'templates/user/edit.hbs']);
*/
assertNoFile(files: string | string[]): void;
/**
* Assert that a file's content matches a regex or string
* @param file - path to a file
* @param reg - regex / string that will be used to search the file
* @example
* result.assertFileContent('models/user.js', /App\.User = DS\.Model\.extend/);
* result.assertFileContent('models/user.js', 'App.User = DS.Model.extend');
*
* @also
*
* Assert that each file in an array of file-regex pairs matches its corresponding regex
* @param pairs - an array of arrays, where each subarray is a [String, RegExp] pair
* @example
* var arg = [
* [ 'models/user.js', /App\.User = DS\.Model\.extend/ ],
* [ 'controllers/user.js', /App\.UserController = Ember\.ObjectController\.extend/ ]
* ]
* result.assertFileContent(arg);
*/
assertFileContent(file: string, reg: string | RegExp): void;
assertFileContent(pairs: Array<[string, string | RegExp]>): void;
/**
* Assert that a file's content is the same as the given string
* @param file - path to a file
* @param expectedContent - the expected content of the file
* @example
* result.assertEqualsFileContent(
* 'data.js',
* 'const greeting = "Hello";\nexport default { greeting }'
* );
*
* @also
*
* Assert that each file in an array of file-string pairs equals its corresponding string
* @param pairs - an array of arrays, where each subarray is a [String, String] pair
* @example
* result.assertEqualsFileContent([
* ['data.js', 'const greeting = "Hello";\nexport default { greeting }'],
* ['user.js', 'export default {\n name: 'Coleman',\n age: 0\n}']
* ]);
*/
assertEqualsFileContent(file: string, expectedContent: string): void;
assertEqualsFileContent(pairs: Array<[string, string]>): void;
/**
* Assert that a file's content does not match a regex / string
* @param file - path to a file
* @param reg - regex / string that will be used to search the file
* @example
* result.assertNoFileContent('models/user.js', /App\.User = DS\.Model\.extend/);
* result.assertNoFileContent('models/user.js', 'App.User = DS.Model.extend');
*
* @also
*
* Assert that each file in an array of file-regex pairs does not match its corresponding regex
* @param pairs - an array of arrays, where each subarray is a [String, RegExp] pair
* var arg = [
* [ 'models/user.js', /App\.User \ DS\.Model\.extend/ ],
* [ 'controllers/user.js', /App\.UserController = Ember\.ObjectController\.extend/ ]
* ]
* result.assertNoFileContent(arg);
*/
assertNoFileContent(file: string, reg: RegExp | string): void;
assertNoFileContent(pairs: Array<[string, string | RegExp]>): void;
/**
* Assert that two strings are equal after standardization of newlines
* @param value - a string
* @param expected - the expected value of the string
* @example
* result.assertTextEqual('I have a yellow cat', 'I have a yellow cat');
*/
assertTextEqual(value: string, expected: string): void;
/**
* Assert an object contains the provided keys
* @param obj Object that should match the given pattern
* @param content An object of key/values the object should contains
*/
assertObjectContent(object: Record<string, unknown>, content: Record<string, any>): void;
/**
* Assert an object does not contain the provided keys
* @param obj Object that should not match the given pattern
* @param content An object of key/values the object should not contain
*/
assertNoObjectContent(object: Record<string, unknown>, content: Record<string, any>): void;
/**
* Assert a JSON file contains the provided keys
* @param filename
* @param content An object of key/values the file should contains
*/
assertJsonFileContent(filename: string, content: Record<string, any>): void;
/**
* Assert a JSON file does not contain the provided keys
* @param filename
* @param content An object of key/values the file should not contain
*/
assertNoJsonFileContent(filename: string, content: Record<string, any>): void;
}