Skip to content

Commit

Permalink
allow to customize the mockedGenerator (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 28, 2023
1 parent 09c0e2e commit 9f4ddd8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/run-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type RunContextSettings = {
};

type PromiseRunResult<GeneratorType extends Generator> = Promise<RunResult<GeneratorType>>;
type MockedGeneratorFactory = (GeneratorClass?: typeof Generator) => typeof Generator;

export class RunContextBase<GeneratorType extends Generator = Generator> extends EventEmitter {
readonly mockedGenerators: Record<string, Generator> = {};
Expand All @@ -63,6 +64,7 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
targetDirectory?: string;
editor!: MemFsEditor.Editor;
memFs: MemFs.Store;
mockedGeneratorFactory: MockedGeneratorFactory;

protected environmentPromise?: PromiseRunResult<GeneratorType>;

Expand Down Expand Up @@ -127,6 +129,7 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends

this.helpers = helpers;
this.memFs = settings?.memFs ?? MemFs.create();
this.mockedGeneratorFactory = this.helpers.createMockedGenerator as any;
}

/**
Expand Down Expand Up @@ -394,6 +397,11 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends
});
}

withMockedGeneratorFactory(mockedGeneratorFactory: MockedGeneratorFactory): this {
this.mockedGeneratorFactory = mockedGeneratorFactory;
return this;
}

/**
* Create mocked generators
* @param namespaces - namespaces of mocked generators
Expand All @@ -413,10 +421,10 @@ export class RunContextBase<GeneratorType extends Generator = Generator> extends

withMockedGenerators(namespaces: string[]): this {
assert(Array.isArray(namespaces), 'namespaces should be an array');
const dependencies = namespaces.map(namespace => [this.helpers.createMockedGenerator(), namespace]);
const dependencies: Dependency[] = namespaces.map(namespace => [this.mockedGeneratorFactory(), namespace]);
const entries = dependencies.map(([generator, namespace]) => [namespace, generator]);
Object.assign(this.mockedGenerators, Object.fromEntries(entries));
return this.withGenerators(dependencies as Dependency[]);
return this.withGenerators(dependencies);
}

/**
Expand Down

0 comments on commit 9f4ddd8

Please sign in to comment.