From dd8bdba0e92aae053076ab542ea7bcd820f82185 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Tue, 27 Aug 2024 13:45:12 -0300 Subject: [PATCH] chore: convert functions to arrow style --- test/helpers.spec.ts | 51 ++++++++++++++------------- test/run-context.spec.ts | 74 ++++++++++++++++++++++------------------ 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 679c161..b4718cc 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -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', @@ -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'); }); }); @@ -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'); }); }); @@ -134,10 +137,10 @@ 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'); @@ -145,10 +148,10 @@ describe('yeoman-test', () => { ); }); - 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); }); @@ -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) { diff --git a/test/run-context.spec.ts b/test/run-context.spec.ts index ae293d9..dedc26d 100644 --- a/test/run-context.spec.ts +++ b/test/run-context.spec.ts @@ -305,20 +305,22 @@ 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) { @@ -326,16 +328,16 @@ describe('RunContext', () => { 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.')); @@ -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) { @@ -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) { @@ -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)); } }); });