Skip to content

Commit

Permalink
chore: convert functions to arrow style
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Aug 27, 2024
1 parent 05628f4 commit dd8bdba
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 58 deletions.
51 changes: 27 additions & 24 deletions test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,35 @@ const { resolve, join } = path;
const environment = await createEnvironment({ adapter: new TestAdapter() });

describe('yeoman-test', () => {
beforeEach(function () {
let StubGenerator;

beforeEach(() => {
process.chdir(join(__dirname, './fixtures'));

this.StubGenerator = class extends Generator {};
StubGenerator = class extends Generator {};
});

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

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

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

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

it('pass options param to the generator', async function () {
it('pass options param to the generator', async () => {
const generator = await helpers.createGenerator('unicorn:app', {
dependencies: [[this.StubGenerator, { namespace: 'unicorn:app' }]],
dependencies: [[StubGenerator, { namespace: 'unicorn:app' }]],
generatorArgs: ['temp'],
generatorOptions: {
ui: 'tdd',
Expand All @@ -58,13 +60,14 @@ describe('yeoman-test', () => {
});

describe('.mockPrompt()', () => {
beforeEach(async function () {
this.generator = await environment.instantiate(helpers.createDummyGenerator(), { generatorArgs: [], generatorOptions: {} });
helpers.mockPrompt(this.generator, { answer: 'foo' });
let generator;
beforeEach(async () => {
generator = await environment.instantiate(helpers.createDummyGenerator(), { generatorArgs: [], generatorOptions: {} });
helpers.mockPrompt(generator, { answer: 'foo' });
});

it('uses default values', function () {
return this.generator.prompt([{ name: 'respuesta', type: 'input', default: 'bar' }]).then(answers => {
it('uses default values', () => {
return generator.prompt([{ name: 'respuesta', type: 'input', default: 'bar' }]).then(answers => {
assert.equal(answers.respuesta, 'bar');
});
});
Expand Down Expand Up @@ -119,8 +122,8 @@ describe('yeoman-test', () => {
});
});

it('prefers mocked values over defaults', function () {
return this.generator.prompt([{ name: 'answer', type: 'input', default: 'bar' }]).then(answers => {
it('prefers mocked values over defaults', () => {
return generator.prompt([{ name: 'answer', type: 'input', default: 'bar' }]).then(answers => {
assert.equal(answers.answer, 'foo');
});
});
Expand All @@ -134,21 +137,21 @@ describe('yeoman-test', () => {
});
});

it('throws if answer is not provided', async function () {
it('throws if answer is not provided', async () => {
const generator = await environment.instantiate(helpers.createDummyGenerator(), { generatorArgs: [], generatorOptions: {} });
helpers.mockPrompt(generator, { foo: 1 }, { throwOnMissingAnswer: true });
return this.generator.prompt([{ message: 'bar', name: 'notFound' }]).then(
return generator.prompt([{ message: 'bar', name: 'notFound' }]).then(
() => assert.fail(),
error => {
assert.equal(error.message, 'yeoman-test: question notFound was asked but answer was not provided');
},
);
});

it('keep prompt method asynchronous', function () {
it('keep prompt method asynchronous', () => {
const spy = mock.fn();

const promise = this.generator.prompt({ name: 'answer', type: 'input' }).then(() => {
const promise = generator.prompt({ name: 'answer', type: 'input' }).then(() => {
assert.strictEqual(spy.mock.callCount(), 1);
});

Expand Down Expand Up @@ -362,14 +365,14 @@ describe('yeoman-test', () => {

const runContext = helpers.run(helpers.createDummyGenerator());
await runContext
.onGenerator(function (generator) {
.onGenerator(function (newGenerator) {
assert.strictEqual(this, runContext);
assert.strictEqual(this.generator, generator);
assert.strictEqual(this.generator, newGenerator);
order.push('onGenerator 0');
})
.onGenerator(function (generator) {
.onGenerator(function (newGenerator) {
assert.strictEqual(this, runContext);
assert.strictEqual(this.generator, generator);
assert.strictEqual(this.generator, newGenerator);
order.push('onGenerator 1');
})
.onEnvironment(function (environment) {
Expand Down
74 changes: 40 additions & 34 deletions test/run-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,37 +305,39 @@ describe('RunContext', () => {
});

describe('#inDir()', () => {
beforeEach(function () {
let temporaryDirectory;

beforeEach(() => {
process.chdir(__dirname);
this.tmp = tmpdir;
temporaryDirectory = tmpdir;
});

it('call helpers.testDirectory()', function () {
it('call helpers.testDirectory()', () => {
const spy = mock.method(helpers, 'testDirectory');
context.inDir(this.tmp);
assert.equal(spy.mock.calls[0].arguments[0], this.tmp);
context.inDir(temporaryDirectory);
assert.equal(spy.mock.calls[0].arguments[0], temporaryDirectory);
spy.mock.restore();
});

it('is chainable', function () {
assert.equal(context.inDir(this.tmp), context);
it('is chainable', () => {
assert.equal(context.inDir(temporaryDirectory), context);
});

it('accepts optional `cb` to be invoked with resolved `dir`', function (done) {
const context = new RunContext(Dummy);
const callback = mock.fn(() => {
assert.strictEqual(callback.mock.callCount(), 1);
assert.equal(callback.mock.calls[0].this, context);
assert.equal(callback.mock.calls[0].arguments[0], path.resolve(this.tmp));
assert.equal(callback.mock.calls[0].arguments[0], path.resolve(temporaryDirectory));
});

context.inDir(this.tmp, callback).on('end', done);
context.inDir(temporaryDirectory, callback).on('end', done);
});

it('throws error at additional calls with dirPath', function () {
assert(context.inDir(this.tmp));
it('throws error at additional calls with dirPath', () => {
assert(context.inDir(temporaryDirectory));
try {
context.inDir(this.tmp);
context.inDir(temporaryDirectory);
assert.fail();
} catch (error) {
assert(error.message.includes('Test directory has already been set.'));
Expand All @@ -344,18 +346,20 @@ describe('RunContext', () => {
});

describe('#doInDir()', () => {
beforeEach(function () {
let temporaryDirectory;

beforeEach(() => {
process.chdir(__dirname);
this.tmp = tmpdir;
temporaryDirectory = tmpdir;
});

it('accepts `cb` to be invoked with resolved `dir`', function (done) {
let callbackCalled = false;
context
.inDir(this.tmp)
.inDir(temporaryDirectory)
.doInDir(dirPath => {
callbackCalled = true;
assert.equal(dirPath, this.tmp);
assert.equal(dirPath, temporaryDirectory);
})
.on('end', () => {
if (callbackCalled) {
Expand All @@ -368,14 +372,14 @@ describe('RunContext', () => {
let callbackCalled1 = false;
let callbackCalled2 = false;
context
.inDir(this.tmp)
.inDir(temporaryDirectory)
.doInDir(dirPath => {
callbackCalled1 = true;
assert.equal(dirPath, this.tmp);
assert.equal(dirPath, temporaryDirectory);
})
.doInDir(dirPath => {
callbackCalled2 = true;
assert.equal(dirPath, this.tmp);
assert.equal(dirPath, temporaryDirectory);
})
.on('end', () => {
if (callbackCalled1 && callbackCalled2) {
Expand All @@ -386,42 +390,44 @@ describe('RunContext', () => {
});

describe('#cd()', () => {
beforeEach(function () {
let temporaryDirectory;

beforeEach(() => {
process.chdir(__dirname);
this.tmp = tmpdir;
temporaryDirectory = tmpdir;
fs.mkdirSync(tmpdir, { recursive: true });
});

it('do not call helpers.testDirectory()', function () {
it('do not call helpers.testDirectory()', () => {
const spy = mock.method(helpers, 'testDirectory');
context.cd(this.tmp);
context.cd(temporaryDirectory);
assert.strictEqual(spy.mock.callCount(), 0);
spy.mock.restore();
});

it('is chainable', function () {
assert.equal(context.cd(this.tmp), context);
it('is chainable', () => {
assert.equal(context.cd(temporaryDirectory), context);
});

it('should set inDirSet & targetDirectory', function () {
it('should set inDirSet & targetDirectory', () => {
assert(!context.targetDirectory);
context.cd(this.tmp);
assert.equal(context.targetDirectory, this.tmp);
context.cd(temporaryDirectory);
assert.equal(context.targetDirectory, temporaryDirectory);
});

it('should cd into created directory', function () {
it('should cd into created directory', () => {
const spy = mock.method(process, 'chdir');
context.cd(this.tmp);
assert.equal(spy.mock.calls[0].arguments[0], this.tmp);
context.cd(temporaryDirectory);
assert.equal(spy.mock.calls[0].arguments[0], temporaryDirectory);
spy.mock.restore();
});

it('should throw error if directory do not exist', function () {
it('should throw error if directory do not exist', () => {
try {
context.cd(path.join(this.tmp, 'NOT_EXIST'));
context.cd(path.join(temporaryDirectory, 'NOT_EXIST'));
assert.fail();
} catch (error) {
assert(error.message.includes(this.tmp));
assert(error.message.includes(temporaryDirectory));
}
});
});
Expand Down

0 comments on commit dd8bdba

Please sign in to comment.