Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@
"prettier": "3.8.3",
"prettier-plugin-packagejson": "^3.0.2",
"rimraf": "^6.1.3",
"sinon": "^21.1.2",
"sinon-test": "^3.1.6",
"strip-ansi": "^7.2.0",
"typescript": "6.0.3",
"yeoman-assert": "^3.1.1",
Expand Down
5 changes: 2 additions & 3 deletions test/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import assert from 'node:assert';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { stub } from 'sinon';
import { beforeEach, describe, esmocha, expect, it } from 'esmocha';
import { TestAdapter } from 'yeoman-test';
import { prepareCommand } from '../src/commands.ts';
Expand Down Expand Up @@ -57,7 +56,7 @@ describe('environment (command)', () => {

beforeEach(async () => {
environment = new Environment({ skipInstall: true, dryRun: true });
environment.adapter.log = stub();
environment.adapter.log = esmocha.fn();
await environment.register(path.join(__dirname, 'fixtures/generator-commands/generators/options'));
});

Expand Down Expand Up @@ -128,7 +127,7 @@ describe('environment (command)', () => {

beforeEach(() => {
environment = new Environment({ skipInstall: true, dryRun: true });
environment.adapter.log = stub();
environment.adapter.log = esmocha.fn();
environment.register(path.join(__dirname, 'fixtures/generator-commands/generators/arguments'));
});

Expand Down
98 changes: 48 additions & 50 deletions test/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import process from 'node:process';
import util from 'node:util';
import { fileURLToPath } from 'node:url';
import { createRequire } from 'node:module';
import { after, afterEach, before, beforeEach, describe, expect, it } from 'esmocha';
import { after, afterEach, before, beforeEach, describe, esmocha, expect, it } from 'esmocha';
import { QueuedAdapter } from '@yeoman/adapter';
import { spy, stub } from 'sinon';
import type { SinonSpy, SinonStub } from 'sinon';
import assert from 'yeoman-assert';
import Environment, { createEnv as createEnvironment } from '../src/index.ts';
import { resolveModulePath } from '../src/util/resolve.ts';
Expand All @@ -27,8 +25,8 @@ const GROUPED_QUEUE_VERSION = require('grouped-queue/package.json').version;
for (const generatorVersion of allVersions) {
const Generator = await importGenerator(generatorVersion);
type GeneratorCtor = typeof Generator;
type GeneratorRunSpy = SinonSpy;
type GeneratorMockStub = SinonStub;
type GeneratorRunSpy = ReturnType<typeof esmocha.fn>;
type GeneratorMockStub = ReturnType<typeof esmocha.fn>;

describe(`Environment with ${generatorVersion}`, () => {
let mockedDefault: GeneratorMockStub;
Expand Down Expand Up @@ -58,7 +56,7 @@ for (const generatorVersion of allVersions) {
});

MockedGenerator = class MockedGenerator extends Generator {};
mockedDefault = stub();
mockedDefault = esmocha.fn();
MockedGenerator.prototype.mockedDefault = mockedDefault;
});

Expand Down Expand Up @@ -268,33 +266,33 @@ for (const generatorVersion of allVersions) {
});

it('should schedule generator queue', async function () {
env.queueTask = spy();
env.queueTask = esmocha.fn();
await env.composeWith('stub');
assert(env.queueTask.calledOnce);
assert(env.queueTask.getCall(0).firstArg === 'environment:run');
expect(env.queueTask).toHaveBeenCalledTimes(1);
expect((env.queueTask as any).mock.calls[0][0]).toBe('environment:run');
});

describe('passing false schedule parameter', () => {
it('should not schedule generator', async function () {
env.queueTask = spy();
env.queueTask = esmocha.fn();
await env.composeWith('stub', { generatorArgs: [], schedule: false });
if (isGreaterThan6(generatorVersion)) {
assert(env.queueTask.calledOnce);
assert(env.queueTask.getCall(0).firstArg !== 'environment:run');
expect(env.queueTask).toHaveBeenCalledTimes(1);
expect((env.queueTask as any).mock.calls[0][0]).not.toBe('environment:run');
} else {
assert(env.queueTask.notCalled);
expect(env.queueTask).not.toHaveBeenCalled();
}
});
});
describe('passing function schedule parameter', () => {
it('returning false should not schedule generator', async function () {
env.queueTask = spy();
env.queueTask = esmocha.fn();
await env.composeWith('stub', { generatorArgs: [], schedule: () => false });
if (isGreaterThan6(generatorVersion)) {
assert(env.queueTask.calledOnce);
assert(env.queueTask.getCall(0).firstArg !== 'environment:run');
expect(env.queueTask).toHaveBeenCalledTimes(1);
expect((env.queueTask as any).mock.calls[0][0]).not.toBe('environment:run');
} else {
assert(env.queueTask.notCalled);
expect(env.queueTask).not.toHaveBeenCalled();
}
});
});
Expand Down Expand Up @@ -394,7 +392,7 @@ for (const generatorVersion of allVersions) {
};

const runName = isLegacyVersion(generatorVersion) ? 'run' : 'queueTasks';
runMethod = spy(Generator.prototype, runName);
runMethod = esmocha.spyOn(Generator.prototype as any, runName as any);
env.register(stubGenerator, { namespace: 'stub:run' });
env.register(writingStubGenerator, { namespace: 'writingstub:run' });
env.register(promiseFailingStubGenerator, { namespace: 'promisefailingstub:run' });
Expand All @@ -403,12 +401,12 @@ for (const generatorVersion of allVersions) {
});

afterEach(function () {
runMethod.restore();
runMethod.mockRestore();
});

it('runs a registered generator', async function () {
return env.run(['stub:run']).then(() => {
assert.ok(runMethod.calledOnce);
expect(runMethod).toHaveBeenCalledTimes(1);
});
});

Expand Down Expand Up @@ -445,7 +443,7 @@ for (const generatorVersion of allVersions) {
const arguments_ = ['stub:run', 'module'];
const options = { skipInstall: true };
return env.run(arguments_, options).then(() => {
assert.ok(runMethod.calledOnce);
expect(runMethod).toHaveBeenCalledTimes(1);
assert.equal(capturedArgs[0], 'module');
assert.equal(capturedArgs[1].skipInstall, true);
});
Expand All @@ -454,7 +452,7 @@ for (const generatorVersion of allVersions) {
it('can take string as args', async function () {
const arguments_ = 'stub:run module';
return env.run(arguments_).then(() => {
assert.ok(runMethod.calledOnce);
expect(runMethod).toHaveBeenCalledTimes(1);
assert.equal(capturedArgs[0], 'module');
});
});
Expand All @@ -466,7 +464,7 @@ for (const generatorVersion of allVersions) {
throw new Error('not supposed to happen');
},
error => {
assert.ok(runMethod.notCalled);
expect(runMethod).not.toHaveBeenCalled();
assert.ok(error.message.includes('Must provide at least one argument, the generator namespace to invoke.'));
},
);
Expand All @@ -492,7 +490,7 @@ for (const generatorVersion of allVersions) {

it('generator error event emits error event when no callback passed', function (done) {
env.on('error', error => {
assert.ok(runMethod.calledOnce);
expect(runMethod).toHaveBeenCalledTimes(1);
assert.ok(error instanceof Error);
assert.equal(error.message, 'some error');
done();
Expand All @@ -504,7 +502,7 @@ for (const generatorVersion of allVersions) {

it('generator failing task emits error', function (done) {
env.on('error', error => {
assert.ok(runMethod.calledOnce);
expect(runMethod).toHaveBeenCalledTimes(1);
assert.ok(error instanceof Error);
assert.equal(error.message, 'some error');
done();
Expand Down Expand Up @@ -577,16 +575,16 @@ for (const generatorVersion of allVersions) {
describe('#run() a ts generator', () => {
beforeEach(async function () {
await env.register(path.join(__dirname, './fixtures/generator-ts/generators/app/index.ts'), { namespace: 'ts:app' });
runMethod = spy((await env.get('ts:app')).prototype, 'exec');
runMethod = esmocha.spyOn((await env.get('ts:app')).prototype as any, 'exec' as any);
});

afterEach(function () {
runMethod.restore();
runMethod.mockRestore();
});

it('runs a registered generator', async function () {
return env.run(['ts:app']).then(() => {
assert.ok(runMethod.calledOnce);
expect(runMethod).toHaveBeenCalledTimes(1);
});
});
});
Expand All @@ -595,27 +593,27 @@ for (const generatorVersion of allVersions) {
beforeEach(async function () {
await env.register(path.join(__dirname, './fixtures/generator-common-js/generators/cjs/index.cjs'), { namespace: 'common-js:cjs' });
const Generator = await env.get('common-js:cjs');
runMethod = spy(Generator.prototype, 'default');
postConstruct = spy(Generator.prototype, '_postConstruct');
runMethod = esmocha.spyOn(Generator.prototype as any, 'default' as any);
postConstruct = esmocha.spyOn(Generator.prototype as any, '_postConstruct' as any);
});

afterEach(function () {
runMethod.restore();
postConstruct.restore();
runMethod.mockRestore();
postConstruct.mockRestore();
});

it('runs a registered generator', async function () {
await env.run(['common-js:cjs']);
assert.ok(runMethod.calledOnce);
expect(runMethod).toHaveBeenCalledTimes(1);
});
it('calls generator _postConstruct method', async function () {
return env.run(['common-js:cjs']).then(() => {
assert.ok(postConstruct.calledOnce);
expect(postConstruct).toHaveBeenCalledTimes(1);
});
});
it('should not call generator _postConstruct method with help option', async function () {
return env.run(['common-js:cjs'], { help: true }).then(() => {
assert.ok(postConstruct.notCalled);
expect(postConstruct).not.toHaveBeenCalled();
});
});
});
Expand All @@ -625,45 +623,45 @@ for (const generatorVersion of allVersions) {
beforeEach(async function () {
await env.register(path.join(__dirname, './fixtures/generator-esm/generators/app/index.js'), { namespace: 'esm:app' });
const esmClass = await env.get('esm:app');
runMethod = spy(esmClass.prototype, 'default');
postConstruct = spy(esmClass.prototype, '_postConstruct');
runMethod = esmocha.spyOn(esmClass.prototype as any, 'default' as any);
postConstruct = esmocha.spyOn(esmClass.prototype as any, '_postConstruct' as any);
});

afterEach(function () {
runMethod.restore();
postConstruct.restore();
runMethod.mockRestore();
postConstruct.mockRestore();
});

it('runs a registered generator', async function () {
return env.run(['esm:app']).then(() => {
assert.ok(runMethod.calledOnce);
expect(runMethod).toHaveBeenCalledTimes(1);
});
});
it('calls generator _postConstruct method', async function () {
return env.run(['esm:app']).then(() => {
assert.ok(postConstruct.calledOnce);
expect(postConstruct).toHaveBeenCalledTimes(1);
});
});
it('should not call generator _postConstruct method with help option', async function () {
return env.run(['esm:app'], { help: true }).then(() => {
assert.ok(postConstruct.notCalled);
expect(postConstruct).not.toHaveBeenCalled();
});
});
});
describe('with mjs extension', () => {
beforeEach(async function () {
await env.register(path.join(__dirname, './fixtures/generator-esm/generators/mjs/index.mjs'), { namespace: 'esm:mjs' });
const esmClass = await env.get('esm:mjs');
runMethod = spy(esmClass.prototype, 'default');
runMethod = esmocha.spyOn(esmClass.prototype as any, 'default' as any);
});

afterEach(function () {
runMethod.restore();
runMethod.mockRestore();
});

it('runs a registered generator', async function () {
return env.run(['esm:mjs']).then(() => {
assert.ok(runMethod.calledOnce);
expect(runMethod).toHaveBeenCalledTimes(1);
});
});
});
Expand All @@ -675,7 +673,7 @@ for (const generatorVersion of allVersions) {

it('runs a registered generator', async function () {
return env.run(['esm:create']).then(() => {
assert.ok(mockedDefault.calledOnce);
expect(mockedDefault).toHaveBeenCalledTimes(1);
});
});
});
Expand All @@ -688,7 +686,7 @@ for (const generatorVersion of allVersions) {

it('runs a registered generator', async function () {
return env.run(['esm:create-inherited']).then(() => {
assert.ok(mockedDefault.calledOnce);
expect(mockedDefault).toHaveBeenCalledTimes(1);
});
});
});
Expand Down Expand Up @@ -754,7 +752,7 @@ for (const generatorVersion of allVersions) {
describe('#getPackagePath and #getPackagePaths()', () => {
beforeEach(async function () {
env.alias(/^prefix-(.*)$/, '$1');
simpleDummy = spy();
simpleDummy = esmocha.fn();
simplePath = path.join(__dirname, 'fixtures/generator-simple');
assert.equal(env.namespaces().length, 0, 'env should be empty');
await env.register(simplePath, { namespace: 'fixtures:generator-simple', packagePath: simplePath });
Expand All @@ -777,8 +775,8 @@ for (const generatorVersion of allVersions) {

describe('#register()', () => {
beforeEach(async function () {
simpleDummy = spy();
resolvedDummy = spy();
simpleDummy = esmocha.fn();
resolvedDummy = esmocha.fn();
completeDummy = function () {};
util.inherits(completeDummy, Generator);
env.register(simpleDummy, { namespace: 'dummy:simple' });
Expand Down
Loading
Loading