Skip to content

Commit

Permalink
feat: accept async createEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Dec 21, 2022
1 parent f34f19d commit 57438dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
19 changes: 13 additions & 6 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,19 @@ export class YeomanTest {
args?: string[],
options?: YeomanGenerator.GeneratorOptions,
localConfigOnly = true,
): GeneratorType {
const env = this.createEnv([], { sharedOptions: { localConfigOnly } });
this.registerDependencies(env, dependencies);
): GeneratorType | Promise<GeneratorType> {
const maybeEnv = this.createEnv([], { sharedOptions: { localConfigOnly } });
// TODO convert method to async.
if ('then' in maybeEnv) {
return maybeEnv.then(env => {
this.registerDependencies(env, dependencies);
return env.create<YeomanGenerator['options']>(name, args as any, options as any) as unknown as GeneratorType;
});
}

this.registerDependencies(maybeEnv, dependencies);

return env.create<YeomanGenerator['options']>(name, args as any, options as any) as unknown as GeneratorType;
return maybeEnv.create<YeomanGenerator['options']>(name, args as any, options as any) as unknown as GeneratorType;
}

/**
Expand Down Expand Up @@ -239,7 +247,7 @@ export class YeomanTest {
* });
*/

createEnv(...args: Parameters<typeof createEnv>): ReturnType<typeof createEnv> {
createEnv(...args: Parameters<typeof createEnv>): ReturnType<typeof createEnv> | Promise<ReturnType<typeof createEnv>> {
return Environment.createEnv(...args);
}

Expand All @@ -248,7 +256,6 @@ export class YeomanTest {
*
* @param {Function} envContructor - environment constructor method.
* @param {Object} [options] - Options to be passed to the environment
* @returns {Object} environment instance
* const env = createTestEnv(require('yeoman-environment').createEnv);
*/

Expand Down
2 changes: 1 addition & 1 deletion src/run-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export class RunContextBase<GeneratorType extends Generator> extends EventEmitte
throw new Error('targetDirectory is required');
}

const testEnv = this.helpers.createTestEnv(this.envOptions.createEnv, {
const testEnv = await this.helpers.createTestEnv(this.envOptions.createEnv, {
cwd: this.settings.forwardCwd ? this.targetDirectory : undefined,
...this.options,
...this.envOptions,
Expand Down
28 changes: 14 additions & 14 deletions test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const env = yeoman.createEnv(undefined, undefined, new TestAdapter() as any);

describe('yeoman-test', function () {
beforeEach(function () {
process.chdir(path.join(__dirname, './fixtures'));
process.chdir(join(__dirname, './fixtures'));

this.StubGenerator = class extends Generator {};
});
Expand All @@ -40,20 +40,20 @@ describe('yeoman-test', function () {
});

describe('.createGenerator()', function () {
it('create a new generator', function () {
const generator = helpers.createGenerator('unicorn:app', [[this.StubGenerator, 'unicorn:app']]);
it('create a new generator', async function () {
const generator = await helpers.createGenerator('unicorn:app', [[this.StubGenerator, 'unicorn:app']]);

assert.ok(generator instanceof this.StubGenerator);
});

it('pass args params to the generator', function () {
const generator = helpers.createGenerator('unicorn:app', [[this.StubGenerator, 'unicorn:app']], ['temp']);
it('pass args params to the generator', async function () {
const generator = await helpers.createGenerator('unicorn:app', [[this.StubGenerator, 'unicorn:app']], ['temp']);

assert.deepEqual(generator.args, ['temp']);
});

it('pass options param to the generator', function () {
const generator = helpers.createGenerator('unicorn:app', [[this.StubGenerator, 'unicorn:app']], ['temp'], { ui: 'tdd' });
it('pass options param to the generator', async function () {
const generator = await helpers.createGenerator('unicorn:app', [[this.StubGenerator, 'unicorn:app']], ['temp'], { ui: 'tdd' });

assert.equal(generator.options.ui, 'tdd');
});
Expand Down Expand Up @@ -372,21 +372,21 @@ describe('yeoman-test', function () {
let mockedCreateEnv;
const createEnvReturn = {};
beforeEach(() => {
mockedCreateEnv = sinonStub(helpers, 'createEnv').returns(createEnvReturn as Environment);
mockedCreateEnv = sinonStub(helpers, 'createEnv').returns(Promise.resolve(createEnvReturn) as Promise<Environment>);
});
afterEach(() => {
mockedCreateEnv.restore();
});
it('calls mocked createEnv', () => {
assert.equal(helpers.createTestEnv(), createEnvReturn);
it('calls mocked createEnv', async () => {
assert.equal(await helpers.createTestEnv(), createEnvReturn);
assert.ok(mockedCreateEnv.calledOnce);
});
it('calls mocked createEnv with newErrorHandler option', () => {
assert.equal(helpers.createTestEnv(), createEnvReturn);
it('calls mocked createEnv with newErrorHandler option', async () => {
assert.equal(await helpers.createTestEnv(), createEnvReturn);
assert.equal(mockedCreateEnv.getCall(0).args[1].newErrorHandler, true);
});
it('calls mocked createEnv with sharedOptions.localConfigOnly option', () => {
assert.equal(helpers.createTestEnv(), createEnvReturn);
it('calls mocked createEnv with sharedOptions.localConfigOnly option', async () => {
assert.equal(await helpers.createTestEnv(), createEnvReturn);
assert.equal(mockedCreateEnv.getCall(0).args[1].sharedOptions.localConfigOnly, true);
});
});
Expand Down

0 comments on commit 57438dd

Please sign in to comment.