import { type mock } from 'node:test'; import type { Store } from 'mem-fs'; import { type MemFsEditor, type MemFsEditorFile } from 'mem-fs-editor'; import type { BaseEnvironmentOptions, BaseGenerator, GetGeneratorConstructor } from '@yeoman/types'; import type { DefaultEnvironmentApi } 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 = { 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; fs?: MemFsEditor; /** * The mocked generators of the context. */ mockedGenerators: Record; spawnStub?: any; settings: RunContextSettings; helpers: YeomanTest; askedQuestions: AskedQuestions; }; /** * This class provides utilities for testing generated content. */ export default class RunResult { env: any; generator: GeneratorType; cwd: string; oldCwd: string; memFs: Store; fs: MemFsEditor; mockedGenerators: any; options: RunResultOptions; spawnStub?: any; readonly askedQuestions: AskedQuestions; constructor(options: RunResultOptions); /** * Create another RunContext reusing the settings. * See helpers.create api */ create(GeneratorOrNamespace: string | GetGeneratorConstructor, settings?: RunContextSettings, environmentOptions?: BaseEnvironmentOptions): import("./run-context.js").default; getSpawnArgsUsingDefaultImplementation(): unknown[][]; /** * Return an object with fs changes. * @param {Function} filter - parameter forwarded to mem-fs-editor#dump */ getSnapshot(filter?: any): Record; /** * Return an object with filenames with state. * @param {Function} filter - parameter forwarded to mem-fs-editor#dump * @returns {Object} */ getStateSnapshot(filter?: any): Record; /** * 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, content: Record): 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, content: Record): 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): 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): void; /** * Get the generator mock * @param generator - the namespace of the mocked generator * @returns the generator mock */ getGeneratorMock(generator: string): ReturnType['mock']; /** * Get the number of times a mocked generator was composed * @param generator - the namespace of the mocked generator * @returns the number of times the generator was composed */ getGeneratorComposeCount(generator: string): number; /** * Assert that a generator was composed * @param generator - the namespace of the mocked generator */ assertGeneratorComposed(generator: string): void; /** * Assert that a generator was composed * @param generator - the namespace of the mocked generator */ assertGeneratorNotComposed(generator: string): void; /** * Assert that a generator was composed only once * @param generator - the namespace of the mocked generator */ assertGeneratorComposedOnce(generator: string): void; /** * Assert that a generator was composed multiple times * @returns an array of the names of the mocked generators that were composed */ getComposedGenerators(): string[]; }