diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index b4718cc..5a90b5f 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -5,6 +5,7 @@ import process from 'node:process'; import { createRequire } from 'node:module'; import { existsSync } from 'node:fs'; import { mock } from 'node:test'; +import { promisify as promisify_ } from 'node:util'; import Generator from 'yeoman-generator'; import { afterEach, beforeEach, describe, expect, it } from 'esmocha'; import type Environment from 'yeoman-environment'; @@ -13,6 +14,8 @@ import helpers from '../src/helpers.js'; import { TestAdapter } from '../src/adapter.js'; import RunContext from '../src/run-context.js'; +/* Remove argument from promisify return */ +const promisify = function_ => () => promisify_(function_)(); const require = createRequire(import.meta.url); const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -162,21 +165,27 @@ describe('yeoman-test', () => { describe('.run()', () => { describe('with a generator', () => { - it('return a RunContext object', done => { - const context = helpers.run(helpers.createDummyGenerator()); - assert(context instanceof RunContext); - context.on('end', done); - }); + it( + 'return a RunContext object', + promisify(done => { + const context = helpers.run(helpers.createDummyGenerator()); + assert(context instanceof RunContext); + context.on('end', done); + }), + ); }); describe('with a namespace', () => { - it('return a RunContext object', done => { - const context = helpers.run('simple:app').withEnvironment(environment => { - environment.register(require.resolve('./fixtures/generator-simple/app')); - }); - assert(context instanceof RunContext); - context.on('end', done); - }); + it( + 'return a RunContext object', + promisify(done => { + const context = helpers.run('simple:app').withEnvironment(environment => { + environment.register(require.resolve('./fixtures/generator-simple/app')); + }); + assert(context instanceof RunContext); + context.on('end', done); + }), + ); }); it('pass settings to RunContext', () => { @@ -192,55 +201,67 @@ describe('yeoman-test', () => { assert.equal(runContext.envOptions, environmentOptions); }); - it('catch env errors', done => { - helpers - .run( - class extends helpers.createDummyGenerator() { - throws() { - this.env.emit('error', new Error('an error')); - } - }, - ) - .on('error', _ => { - done(); - }); - }); - - it('catch generator emitted errors', done => { - helpers - .run( - class extends helpers.createDummyGenerator() { - throws() { - this.emit('error', new Error('an error')); - } - }, - ) - .on('error', _ => { - done(); - }); - }); - - it('catch generator thrown errors', done => { - helpers - .run( - class extends helpers.createDummyGenerator() { - throws() { - throw new Error('Some error.'); - } - }, - ) - .on('error', _ => { - done(); - }); - }); + it( + 'catch env errors', + promisify(done => { + helpers + .run( + class extends helpers.createDummyGenerator() { + throws() { + this.env.emit('error', new Error('an error')); + } + }, + ) + .on('error', _ => { + done(); + }); + }), + ); + + it( + 'catch generator emitted errors', + promisify(done => { + helpers + .run( + class extends helpers.createDummyGenerator() { + throws() { + this.emit('error', new Error('an error')); + } + }, + ) + .on('error', _ => { + done(); + }); + }), + ); + + it( + 'catch generator thrown errors', + promisify(done => { + helpers + .run( + class extends helpers.createDummyGenerator() { + throws() { + throw new Error('Some error.'); + } + }, + ) + .on('error', _ => { + done(); + }); + }), + ); // This is a workaround for corner case were an error is not correctly emitted // See https://github.com/yeoman/generator/pull/1155 - it('catch run errors', done => { - helpers.run(class extends Generator {}, {}, { catchGeneratorError: true }).on('error', _ => { - done(); - }); - }); + it( + 'catch run errors', + promisify(done => { + helpers.run(class extends Generator {}, {}, { catchGeneratorError: true }).on('error', _ => { + done(); + }); + }), + ); describe('with files', () => { it('write files to mem-fs', async () => { diff --git a/test/run-context-environment.spec.ts b/test/run-context-environment.spec.ts index a622a87..82a5938 100644 --- a/test/run-context-environment.spec.ts +++ b/test/run-context-environment.spec.ts @@ -5,7 +5,7 @@ import process from 'node:process'; import { createRequire } from 'node:module'; import { mock } from 'node:test'; import { type LookupOptions } from '@yeoman/types'; -import { afterEach, beforeEach, describe, it } from 'esmocha'; +import { after as afterAll, afterEach, before as beforeAll, beforeEach, describe, it } from 'esmocha'; import helpers from '../src/helpers.js'; import RunContext from '../src/run-context.js'; import RunResult from '../src/run-result.js'; @@ -46,10 +46,10 @@ describe('RunContext running environment', () => { }); describe('with string', () => { - before(() => { + beforeAll(() => { gen = require.resolve('./fixtures/generator-simple/app'); }); - after(() => { + afterAll(() => { gen = undefined; }); @@ -83,10 +83,10 @@ describe('RunContext running environment', () => { }); describe('with generator', () => { - before(() => { + beforeAll(() => { gen = SimpleApp; }); - after(() => { + afterAll(() => { gen = undefined; }); @@ -98,7 +98,7 @@ describe('RunContext running environment', () => { }); describe('with promised generator', () => { - before(() => { + beforeAll(() => { gen = 'promised-generator'; build = false; }); @@ -108,7 +108,7 @@ describe('RunContext running environment', () => { mock.method(environment, 'create', () => Promise.resolve(new FakeGenerator([], { env: environment }))); }); }); - after(() => { + afterAll(() => { gen = undefined; build = true; }); @@ -121,10 +121,10 @@ describe('RunContext running environment', () => { }); describe('with path', () => { - before(() => { + beforeAll(() => { gen = require.resolve('./fixtures/generator-simple/app'); }); - after(() => { + afterAll(() => { gen = undefined; }); @@ -142,11 +142,11 @@ describe('RunContext running environment', () => { }); describe('with lookups with packagePaths', () => { - before(() => { + beforeAll(() => { lookups = [{ packagePaths: [path.resolve('./fixtures/generator-simple')] }]; gen = 'simple:app'; }); - after(() => { + afterAll(() => { lookups = []; gen = undefined; }); @@ -165,18 +165,18 @@ describe('RunContext running environment', () => { }); describe('with lookups with npmPaths', () => { - before(() => { + beforeAll(() => { lookups = [{ npmPaths: [path.resolve('./fixtures/')] }]; }); - after(() => { + afterAll(() => { lookups = []; }); describe('and simple generator', () => { - before(() => { + beforeAll(() => { gen = 'simple:app'; }); - after(() => { + afterAll(() => { gen = undefined; }); @@ -194,10 +194,10 @@ describe('RunContext running environment', () => { }); describe('and generator that throws', () => { - before(() => { + beforeAll(() => { gen = 'simple:throwing'; }); - after(() => { + afterAll(() => { gen = undefined; }); @@ -212,12 +212,12 @@ describe('RunContext running environment', () => { }); describe('with composing generator', () => { - before(() => { + beforeAll(() => { lookups = [{ packagePaths: [path.resolve('./fixtures/generator-simple')] }]; gen = 'simple:composing'; build = false; }); - after(() => { + afterAll(() => { lookups = []; gen = undefined; build = true; diff --git a/test/run-context.spec.ts b/test/run-context.spec.ts index dedc26d..eb2761a 100644 --- a/test/run-context.spec.ts +++ b/test/run-context.spec.ts @@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url'; import process from 'node:process'; import { createRequire } from 'node:module'; import { mock } from 'node:test'; +import { promisify as promisify_ } from 'node:util'; import { afterEach, beforeEach, describe, expect, it } from 'esmocha'; import inquirer from 'inquirer'; import Generator from 'yeoman-generator'; @@ -13,6 +14,8 @@ import { RunContextBase as RunContext } from '../src/run-context.js'; import helpers from '../src/helpers.js'; import { DummyPrompt } from '../src/adapter.js'; +/* Remove argument from promisify return */ +const promisify = function_ => () => promisify_(function_)(); const require = createRequire(import.meta.url); const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -39,71 +42,85 @@ describe('RunContext', () => { context = new RunContext(Dummy, undefined, environmentOptions); }); - afterEach(done => { - process.chdir(__dirname); - - if (context.settings.tmpdir) { - context.cleanTestDirectory(); - } - - if (context.completed || context.errored || !context.ran) { - done(); - return; - } + afterEach( + promisify(done => { + process.chdir(__dirname); - try { - context.on('end', done); - } catch { - // Ignore error - } - }); + if (context.settings.tmpdir) { + context.cleanTestDirectory(); + } - describe('constructor', () => { - it('forwards envOptions to the environment', done => { - context.on('ready', function () { - assert.equal(this.env.options.foo, environmentOptions.foo); + if (context.completed || context.errored || !context.ran) { done(); - }); - }); - - it('accept path parameter', done => { - const context = new RunContext(require.resolve('./fixtures/generator-simple/app')); - - context - .on('ready', async () => { - assert(await context.env.get('simple:app')); - }) - .on('end', done); - }); + return; + } - it('propagate generator error events', done => { - const error = new Error('an error'); - const Dummy = helpers.createDummyGenerator(); - const execSpy = mock.fn( - () => {}, - () => { - throw error; - }, - ); - const endSpy = mock.fn(); - Dummy.prototype.test = execSpy; - Dummy.prototype.end = execSpy; - const context = new RunContext(Dummy); + try { + context.on('end', done); + } catch { + // Ignore error + } + }), + ); - context.on('error', error_ => { - assert.strictEqual(execSpy.mock.callCount(), 1); - assert.equal(error_, error); - assert.strictEqual(endSpy.mock.callCount(), 0); - done(); - }); - }); + describe('constructor', () => { + it( + 'forwards envOptions to the environment', + promisify(done => { + context.on('ready', function () { + assert.equal(this.env.options.foo, environmentOptions.foo); + done(); + }); + }), + ); + + it( + 'accept path parameter', + promisify(done => { + const context = new RunContext(require.resolve('./fixtures/generator-simple/app')); + + context + .on('ready', async () => { + assert(await context.env.get('simple:app')); + }) + .on('end', done); + }), + ); + + it( + 'propagate generator error events', + promisify(done => { + const error = new Error('an error'); + const Dummy = helpers.createDummyGenerator(); + const execSpy = mock.fn( + () => {}, + () => { + throw error; + }, + ); + const endSpy = mock.fn(); + Dummy.prototype.test = execSpy; + Dummy.prototype.end = execSpy; + const context = new RunContext(Dummy); - it('accept generator constructor parameter (and assign gen:test as namespace)', done => { - context.on('ready', async () => { - assert(await context.env.get('gen:test')); - done(); - }); - }); + context.on('error', error_ => { + assert.strictEqual(execSpy.mock.callCount(), 1); + assert.equal(error_, error); + assert.strictEqual(endSpy.mock.callCount(), 0); + done(); + }); + }), + ); + + it( + 'accept generator constructor parameter (and assign gen:test as namespace)', + promisify(done => { + context.on('ready', async () => { + assert(await context.env.get('gen:test')); + done(); + }); + }), + ); it('set namespace and resolved path in generator', async () => { const context = new RunContext(Dummy, { @@ -116,37 +133,49 @@ describe('RunContext', () => { expect(((await context.env.get('simple:app')) as any).resolved).toMatch(/^path/); }); - it('run the generator asynchronously', done => { - assert.equal(execSpy.mock.callCount(), 0); - context.on('end', () => { - assert.strictEqual(execSpy.mock.callCount(), 1); - done(); - }); - }); - - it('reset mocked prompt after running', done => { - context.on('end', () => { - assert.equal(defaultInput, inquirer.prompt.input); - done(); - }); - }); - - it('automatically run in a random tmpdir', done => { - context.on('end', () => { - assert.notEqual(process.cwd(), __dirname); - assert.equal(tempDirectory, path.dirname(process.cwd())); - done(); - }); - }); - - it('allows an option to not automatically run in tmpdir', done => { - const cwd = process.cwd(); - const context = new RunContext(Dummy, { cwd, tmpdir: false }); - context.on('end', () => { - assert.equal(cwd, process.cwd()); - done(); - }); - }); + it( + 'run the generator asynchronously', + promisify(done => { + assert.equal(execSpy.mock.callCount(), 0); + context.on('end', () => { + assert.strictEqual(execSpy.mock.callCount(), 1); + done(); + }); + }), + ); + + it( + 'reset mocked prompt after running', + promisify(done => { + context.on('end', () => { + assert.equal(defaultInput, inquirer.prompt.input); + done(); + }); + }), + ); + + it( + 'automatically run in a random tmpdir', + promisify(done => { + context.on('end', () => { + assert.notEqual(process.cwd(), __dirname); + assert.equal(tempDirectory, path.dirname(process.cwd())); + done(); + }); + }), + ); + + it( + 'allows an option to not automatically run in tmpdir', + promisify(done => { + const cwd = process.cwd(); + const context = new RunContext(Dummy, { cwd, tmpdir: false }); + context.on('end', () => { + assert.equal(cwd, process.cwd()); + done(); + }); + }), + ); it('throws an error when calling cleanTestDirectory with not tmpdir settings', () => { const cwd = process.cwd(); @@ -171,36 +200,48 @@ describe('RunContext', () => { assert.equal(context.settings.namespace, 'simple:app'); }); - it('only run a generator once', done => { - context.on('end', () => { - assert.strictEqual(execSpy.mock.callCount(), 1); - done(); - }); - - context.setupEventListeners(); - context.setupEventListeners(); - }); - - it('set --force by default', done => { - context.on('end', () => { - assert.equal(execSpy.mock.calls[0].this.options.force, true); - done(); - }); - }); + it( + 'only run a generator once', + promisify(done => { + context.on('end', () => { + assert.strictEqual(execSpy.mock.callCount(), 1); + done(); + }); - it('set --skip-install by default', done => { - context.on('end', () => { - assert.equal(execSpy.mock.calls[0].this.options.skipInstall, true); - done(); - }); - }); + context.setupEventListeners(); + context.setupEventListeners(); + }), + ); - it('set --skip-cache by default', done => { - context.on('end', () => { - assert.equal(execSpy.mock.calls[0].this.options.skipCache, true); - done(); - }); - }); + it( + 'set --force by default', + promisify(done => { + context.on('end', () => { + assert.equal(execSpy.mock.calls[0].this.options.force, true); + done(); + }); + }), + ); + + it( + 'set --skip-install by default', + promisify(done => { + context.on('end', () => { + assert.equal(execSpy.mock.calls[0].this.options.skipInstall, true); + done(); + }); + }), + ); + + it( + 'set --skip-cache by default', + promisify(done => { + context.on('end', () => { + assert.equal(execSpy.mock.calls[0].this.options.skipCache, true); + done(); + }); + }), + ); }); describe('error handling', () => { @@ -208,29 +249,32 @@ describe('RunContext', () => { process.removeAllListeners('unhandledRejection'); }); - it('throw an unhandledRejection when no listener is present', done => { - const error = new Error('dummy exception'); - const execSpy = mock.fn( - () => {}, - () => { - throw error; - }, - ); - const errorHandler = function (error_) { - assert.strictEqual(execSpy.mock.callCount(), 1); - assert.equal(error_, error); - done(); - }; + it( + 'throw an unhandledRejection when no listener is present', + promisify(done => { + const error = new Error('dummy exception'); + const execSpy = mock.fn( + () => {}, + () => { + throw error; + }, + ); + const errorHandler = function (error_) { + assert.strictEqual(execSpy.mock.callCount(), 1); + assert.equal(error_, error); + done(); + }; - process.once('unhandledRejection', errorHandler); + process.once('unhandledRejection', errorHandler); - const Dummy = helpers.createDummyGenerator(); - Dummy.prototype.test = execSpy; + const Dummy = helpers.createDummyGenerator(); + Dummy.prototype.test = execSpy; - setImmediate(() => { - return new RunContext(Dummy).on('end', () => {}); - }); - }); + setImmediate(() => { + return new RunContext(Dummy).on('end', () => {}); + }); + }), + ); }); describe('#toPromise()', () => { @@ -323,16 +367,19 @@ describe('RunContext', () => { 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(temporaryDirectory)); - }); + it( + 'accepts optional `cb` to be invoked with resolved `dir`', + promisify(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(temporaryDirectory)); + }); - context.inDir(temporaryDirectory, callback).on('end', done); - }); + context.inDir(temporaryDirectory, callback).on('end', done); + }), + ); it('throws error at additional calls with dirPath', () => { assert(context.inDir(temporaryDirectory)); @@ -353,40 +400,46 @@ describe('RunContext', () => { temporaryDirectory = tmpdir; }); - it('accepts `cb` to be invoked with resolved `dir`', function (done) { - let callbackCalled = false; - context - .inDir(temporaryDirectory) - .doInDir(dirPath => { - callbackCalled = true; - assert.equal(dirPath, temporaryDirectory); - }) - .on('end', () => { - if (callbackCalled) { - done(); - } - }); - }); - - it('accepts multiples call with `cb` to be invoked with resolved `dir`', function (done) { - let callbackCalled1 = false; - let callbackCalled2 = false; - context - .inDir(temporaryDirectory) - .doInDir(dirPath => { - callbackCalled1 = true; - assert.equal(dirPath, temporaryDirectory); - }) - .doInDir(dirPath => { - callbackCalled2 = true; - assert.equal(dirPath, temporaryDirectory); - }) - .on('end', () => { - if (callbackCalled1 && callbackCalled2) { - done(); - } - }); - }); + it( + 'accepts `cb` to be invoked with resolved `dir`', + promisify(done => { + let callbackCalled = false; + context + .inDir(temporaryDirectory) + .doInDir(dirPath => { + callbackCalled = true; + assert.equal(dirPath, temporaryDirectory); + }) + .on('end', () => { + if (callbackCalled) { + done(); + } + }); + }), + ); + + it( + 'accepts multiples call with `cb` to be invoked with resolved `dir`', + promisify(done => { + let callbackCalled1 = false; + let callbackCalled2 = false; + context + .inDir(temporaryDirectory) + .doInDir(dirPath => { + callbackCalled1 = true; + assert.equal(dirPath, temporaryDirectory); + }) + .doInDir(dirPath => { + callbackCalled2 = true; + assert.equal(dirPath, temporaryDirectory); + }) + .on('end', () => { + if (callbackCalled1 && callbackCalled2) { + done(); + } + }); + }), + ); }); describe('#cd()', () => { @@ -444,94 +497,121 @@ describe('RunContext', () => { assert.equal(context.inTmpDir(), context); }); - it('accepts optional `cb` to be invoked with resolved `dir`', done => { - const callback = mock.fn(function (dir) { - assert.equal(this, context); - assert(dir.includes(tempDirectory)); - }); + it( + 'accepts optional `cb` to be invoked with resolved `dir`', + promisify(done => { + const callback = mock.fn(function (dir) { + assert.equal(this, context); + assert(dir.includes(tempDirectory)); + }); - context.inTmpDir(callback).on('end', done); - }); + context.inTmpDir(callback).on('end', done); + }), + ); }); describe('#withArguments()', () => { - it('provide arguments to the generator when passed as Array', done => { - context.withArguments(['one', 'two']); - context.on('end', () => { - assert.deepEqual(execSpy.mock.calls[0].this.arguments, ['one', 'two']); - done(); - }); - }); - - it('provide arguments to the generator when passed as String', done => { - context.withArguments('foo bar'); - context.on('end', () => { - assert.deepEqual(execSpy.mock.calls[0].this.arguments, ['foo', 'bar']); - done(); - }); - }); + it( + 'provide arguments to the generator when passed as Array', + promisify(done => { + context.withArguments(['one', 'two']); + context.on('end', () => { + assert.deepEqual(execSpy.mock.calls[0].this.arguments, ['one', 'two']); + done(); + }); + }), + ); + + it( + 'provide arguments to the generator when passed as String', + promisify(done => { + context.withArguments('foo bar'); + context.on('end', () => { + assert.deepEqual(execSpy.mock.calls[0].this.arguments, ['foo', 'bar']); + done(); + }); + }), + ); it('throws when arguments passed is neither a String or an Array', () => { assert.throws(context.withArguments.bind(context, { foo: 'bar' })); }); - it('is chainable', done => { - context.withArguments('foo').withArguments('bar'); - context.on('end', () => { - assert.deepEqual(execSpy.mock.calls[0].this.arguments, ['foo', 'bar']); - done(); - }); - }); + it( + 'is chainable', + promisify(done => { + context.withArguments('foo').withArguments('bar'); + context.on('end', () => { + assert.deepEqual(execSpy.mock.calls[0].this.arguments, ['foo', 'bar']); + done(); + }); + }), + ); }); describe('#withOptions()', () => { - it('provide options to the generator', done => { - context.withOptions({ foo: 'bar' }); - context.on('end', () => { - assert.equal(execSpy.mock.calls[0].this.options.foo, 'bar'); - done(); - }); - }); - - it('allow default settings to be overriden', done => { - context.withOptions({ - 'skip-install': false, - force: false, - }); - context.on('end', () => { - assert.equal(execSpy.mock.calls[0].this.options.skipInstall, false); - assert.equal(execSpy.mock.calls[0].this.options.force, false); - done(); - }); - }); - - it('camel case options', done => { - context.withOptions({ 'foo-bar': false }); - context.on('end', () => { - assert.equal(execSpy.mock.calls[0].this.options['foo-bar'], false); - assert.equal(execSpy.mock.calls[0].this.options.fooBar, false); - done(); - }); - }); - - it('kebab case options', done => { - context.withOptions({ barFoo: false }); - context.on('end', () => { - assert.equal(execSpy.mock.calls[0].this.options['bar-foo'], false); - assert.equal(execSpy.mock.calls[0].this.options.barFoo, false); - done(); - }); - }); - - it('is chainable', done => { - context.withOptions({ foo: 'bar' }).withOptions({ john: 'doe' }); - context.on('end', () => { - const { options } = execSpy.mock.calls[0].this; - assert.equal(options.foo, 'bar'); - assert.equal(options.john, 'doe'); - done(); - }); - }); + it( + 'provide options to the generator', + promisify(done => { + context.withOptions({ foo: 'bar' }); + context.on('end', () => { + assert.equal(execSpy.mock.calls[0].this.options.foo, 'bar'); + done(); + }); + }), + ); + + it( + 'allow default settings to be overriden', + promisify(done => { + context.withOptions({ + 'skip-install': false, + force: false, + }); + context.on('end', () => { + assert.equal(execSpy.mock.calls[0].this.options.skipInstall, false); + assert.equal(execSpy.mock.calls[0].this.options.force, false); + done(); + }); + }), + ); + + it( + 'camel case options', + promisify(done => { + context.withOptions({ 'foo-bar': false }); + context.on('end', () => { + assert.equal(execSpy.mock.calls[0].this.options['foo-bar'], false); + assert.equal(execSpy.mock.calls[0].this.options.fooBar, false); + done(); + }); + }), + ); + + it( + 'kebab case options', + promisify(done => { + context.withOptions({ barFoo: false }); + context.on('end', () => { + assert.equal(execSpy.mock.calls[0].this.options['bar-foo'], false); + assert.equal(execSpy.mock.calls[0].this.options.barFoo, false); + done(); + }); + }), + ); + + it( + 'is chainable', + promisify(done => { + context.withOptions({ foo: 'bar' }).withOptions({ john: 'doe' }); + context.on('end', () => { + const { options } = execSpy.mock.calls[0].this; + assert.equal(options.foo, 'bar'); + assert.equal(options.john, 'doe'); + done(); + }); + }), + ); }); describe('#withAnswers()', () => { @@ -670,35 +750,44 @@ describe('RunContext', () => { }); describe('#withGenerators()', () => { - it('register paths', done => { - context.withGenerators([require.resolve('./fixtures/generator-simple/app')]).on('ready', async () => { - assert(await context.env.get('simple:app')); - done(); - }); - }); + it( + 'register paths', + promisify(done => { + context.withGenerators([require.resolve('./fixtures/generator-simple/app')]).on('ready', async () => { + assert(await context.env.get('simple:app')); + done(); + }); + }), + ); it('register paths with namespaces', async () => { await context.withGenerators([[require.resolve('./fixtures/generator-simple/app'), { namespace: 'foo:bar' }]]).build(); assert(await context.env.get('foo:bar')); }); - it('register mocked generator', done => { - context.withGenerators([[helpers.createDummyGenerator(), { namespace: 'dummy:gen' }]]).on('ready', async () => { - assert(await context.env.get('dummy:gen')); - done(); - }); - }); - - it('allow multiple calls', done => { - context - .withGenerators([require.resolve('./fixtures/generator-simple/app')]) - .withGenerators([[helpers.createDummyGenerator(), { namespace: 'dummy:gen' }]]) - .on('ready', async () => { + it( + 'register mocked generator', + promisify(done => { + context.withGenerators([[helpers.createDummyGenerator(), { namespace: 'dummy:gen' }]]).on('ready', async () => { assert(await context.env.get('dummy:gen')); - assert(await context.env.get('simple:app')); done(); }); - }); + }), + ); + + it( + 'allow multiple calls', + promisify(done => { + context + .withGenerators([require.resolve('./fixtures/generator-simple/app')]) + .withGenerators([[helpers.createDummyGenerator(), { namespace: 'dummy:gen' }]]) + .on('ready', async () => { + assert(await context.env.get('dummy:gen')); + assert(await context.env.get('simple:app')); + done(); + }); + }), + ); }); describe('#withSpawnMock()', () => { @@ -764,52 +853,61 @@ describe('RunContext', () => { }); describe('#withEnvironment()', () => { - it('register paths', done => { - context - .withEnvironment(environment => { - environment.register(require.resolve('./fixtures/generator-simple/app')); - return environment; - }) - .on('ready', async () => { - assert(await context.env.get('simple:app')); - done(); - }); - }); + it( + 'register paths', + promisify(done => { + context + .withEnvironment(environment => { + environment.register(require.resolve('./fixtures/generator-simple/app')); + return environment; + }) + .on('ready', async () => { + assert(await context.env.get('simple:app')); + done(); + }); + }), + ); }); describe('#withLocalConfig()', () => { - it('provides config to the generator', done => { - context - .withLocalConfig({ - some: true, - data: 'here', - }) - .on('ready', () => { - assert.equal(context.generator.config.get('some'), true); - assert.equal(context.generator.config.get('data'), 'here'); - done(); - }); - }); + it( + 'provides config to the generator', + promisify(done => { + context + .withLocalConfig({ + some: true, + data: 'here', + }) + .on('ready', () => { + assert.equal(context.generator.config.get('some'), true); + assert.equal(context.generator.config.get('data'), 'here'); + done(); + }); + }), + ); }); describe('#_createRunResultOptions()', () => { - it('creates RunResult configuration', done => { - context - .withLocalConfig({ - some: true, - data: 'here', - }) - .on('ready', () => { - const options = context._createRunResultOptions(); - assert.equal(options.env, context.env); - assert.equal(options.memFs, context.env.sharedFs); - assert.equal(options.oldCwd, context.oldCwd); - assert.equal(options.cwd, context.targetDirectory); - assert.equal(options.envOptions, context.envOptions); - assert.equal(options.mockedGenerators, context.mockedGenerators); - assert.deepEqual(options.settings, context.settings); - done(); - }); - }); + it( + 'creates RunResult configuration', + promisify(done => { + context + .withLocalConfig({ + some: true, + data: 'here', + }) + .on('ready', () => { + const options = context._createRunResultOptions(); + assert.equal(options.env, context.env); + assert.equal(options.memFs, context.env.sharedFs); + assert.equal(options.oldCwd, context.oldCwd); + assert.equal(options.cwd, context.targetDirectory); + assert.equal(options.envOptions, context.envOptions); + assert.equal(options.mockedGenerators, context.mockedGenerators); + assert.deepEqual(options.settings, context.settings); + done(); + }); + }), + ); }); }); diff --git a/test/run-result.spec.ts b/test/run-result.spec.ts index ad7b3b7..66631e9 100644 --- a/test/run-result.spec.ts +++ b/test/run-result.spec.ts @@ -5,7 +5,7 @@ import process from 'node:process'; import { mock } from 'node:test'; import { create as createMemFs } from 'mem-fs'; import { create as createMemFsEditor } from 'mem-fs-editor'; -import { afterEach, beforeEach, describe, it } from 'esmocha'; +import { afterEach, before as beforeAll, beforeEach, describe, it } from 'esmocha'; import RunContext from '../src/run-context.js'; import RunResult from '../src/run-result.js'; import helpers from '../src/helpers.js'; @@ -28,7 +28,7 @@ describe('run-result', () => { const cwd = {}; const options = { memFs, cwd }; let runResult; - before(() => { + beforeAll(() => { runResult = new RunResult(options as any); }); it('loads memFs option', () => { @@ -43,7 +43,7 @@ describe('run-result', () => { const optionValue = {}; const options = {}; let runResult; - before(() => { + beforeAll(() => { options[optionName] = optionValue; runResult = new RunResult(options as any); }); @@ -202,7 +202,7 @@ describe('run-result', () => { let cwd; const oldCwd = {}; let runContext; - before(() => { + beforeAll(() => { cwd = process.cwd(); runContext = new RunResult({ memFs,