Skip to content

Commit

Permalink
migrate to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Sep 7, 2024
1 parent f0b9cbd commit 0fffbf5
Show file tree
Hide file tree
Showing 18 changed files with 5,154 additions and 7,224 deletions.
12,170 changes: 5,038 additions & 7,132 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@
"@types/semver": "^7.5.3",
"@types/sinon": "^17.0.1",
"@types/text-table": "^0.2.3",
"@vitest/coverage-v8": "^2.0.5",
"@yeoman/adapter": "^1.4.0",
"@yeoman/eslint": "^0.2.0",
"@yeoman/transform": "^1.2.0",
"c8": "^8.0.1",
"cpy-cli": "^5.0.0",
"ejs": "^3.1.9",
"esmocha": "^1.0.1",
"inquirer": "^9.2.11",
"jsdoc": "^4.0.2",
"nock": "^13.3.4",
Expand All @@ -93,8 +92,9 @@
"sinon": "^18.0.0",
"tui-jsdoc-template": "^1.2.2",
"typescript": "^5.2.2",
"vitest": "^2.0.5",
"yeoman-assert": "^3.1.1",
"yeoman-environment": "^4.0.0",
"yeoman-environment": "^4.4.1",
"yeoman-test": "^9.0.0"
},
"peerDependencies": {
Expand Down
10 changes: 9 additions & 1 deletion src/actions/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,9 @@ export abstract class TasksMixin {
{ Generator, resolved = Generator.resolved }: { Generator?: any; resolved: string },
options: EnvironmentComposeOptions<G> = {},
) {
if (!resolved) {
throw new Error('Generator path property is not a string');
}
const generatorNamespace = this.env.namespace(resolved);
const findGenerator = async () => {
const generatorImport = await import(resolved);
Expand All @@ -618,7 +621,12 @@ export abstract class TasksMixin {
return typeof generatorImport.default === 'function' ? generatorImport.default : generatorImport;
};

Generator = Generator ?? (await findGenerator());
try {
Generator = Generator ?? (await findGenerator());
} catch {
throw new Error('Missing Generator property');
}

Generator.namespace = generatorNamespace;
Generator.resolved = resolved;
return this.env.composeWith<G>(Generator, options);
Expand Down
36 changes: 14 additions & 22 deletions test/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
import { createRequire } from 'node:module';
import process from 'node:process';
import { Buffer } from 'node:buffer';
import { afterEach, beforeEach, describe, esmocha, expect, it } from 'esmocha';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { extend } from 'lodash-es';
import { assert as sinonAssert, fake as sinonFake, spy as sinonSpy } from 'sinon';
import { passthrough } from '@yeoman/transform';
Expand All @@ -31,7 +31,7 @@ describe('Base', () => {
let dummy;

beforeEach(async () => {
await helpers.prepareTemporaryDir();
await helpers.prepareTemporaryDir({ cwd: tmpdir, tmpdir: true }).run();

env = createEnv();
// Ignore error forwarded to environment
Expand Down Expand Up @@ -900,26 +900,18 @@ describe('Base', () => {
});

describe('object as first argument', () => {
it('fails for missing Generator property', () => {
it('fails for missing Generator property', async () => {
const gen = dummy;
assert.rejects(
() =>
gen.composeWith({
path: 'foo-path',
}),
error => error.message.includes('Missing Generator property'),
);
await expect(gen.composeWith({
path: 'foo-path',
})).rejects.toThrow('Missing Generator property');
});

it('fails for missing path property', () => {
it('fails for missing path property', async () => {
const gen = dummy;
assert.rejects(
() =>
gen.composeWith({
Generator: GenCompose,
}),
error => error.message.includes('path property is not a string'),
);
await expect(gen.composeWith({
Generator: GenCompose,
})).rejects.toThrow('path property is not a string');
});
});

Expand Down Expand Up @@ -1217,7 +1209,7 @@ describe('Base', () => {
.on('method:createSomething', assertEvent('method:createSomething'))
.on('method:createSomethingElse', assertEvent('method:createSomethingElse'));

angular.run();
angular.run().catch(() => {});
}));

it('only call the end event once (bug #402)', () =>
Expand Down Expand Up @@ -1252,7 +1244,7 @@ describe('Base', () => {
isFirstEndEvent = false;
});

generatorOnce.run();
generatorOnce.run().catch(() => {});
}));

it('triggers end event after all generators methods are ran (#709)', () =>
Expand All @@ -1278,7 +1270,7 @@ describe('Base', () => {
'skip-install': true,
});

generatorEnd.run();
generatorEnd.run().catch(() => {});
}));
});

Expand Down Expand Up @@ -1526,7 +1518,7 @@ describe('Base', () => {
run: false,
});

const secondTask = esmocha.fn();
const secondTask = vi.fn();
gen.queueTask({
method: secondTask,
queueName,
Expand Down
2 changes: 1 addition & 1 deletion test/deprecate.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import assert from 'node:assert';
import { afterEach, beforeEach, describe, it } from 'esmocha';
import { afterEach, beforeEach, describe, it } from 'vitest';
import chalk from 'chalk';
import sinon, { type SinonSpy } from 'sinon';
import deprecate from '../src/util/deprecate.js';
Expand Down
42 changes: 21 additions & 21 deletions test/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import os from 'node:os';
import path from 'node:path';
import assert from 'node:assert';
import { TestAdapter } from '@yeoman/adapter/testing';
import { after, before, describe, it } from 'esmocha';
import type { SinonStub} from 'sinon';
import { afterAll, beforeAll, describe, it } from 'vitest';
import type { SinonStub } from 'sinon';
import { stub as sinonStub } from 'sinon';
import Environment from 'yeoman-environment';
import helpers from 'yeoman-test';
Expand All @@ -17,11 +17,12 @@ describe('Generator with environment version', () => {
let dummy: Base;
let getVersionStub: SinonStub;

before(helpers.setUpTestDirectory(tmpdir));
beforeAll(async () => {
await helpers.prepareTemporaryDir().run();
});

describe('mocked 3.0.0', () => {
before(function () {
this.timeout(100_000);
beforeAll(() => {
env = new Environment({ skipInstall: true, adapter: new TestAdapter() });
env.getVersion = env.getVersion || (() => {});
getVersionStub = sinonStub(env, 'getVersion');
Expand All @@ -35,9 +36,9 @@ describe('Generator with environment version', () => {
'skip-install': true,
skipCheckEnv: true,
});
});
}, 100_000);

after(() => {
afterAll(() => {
getVersionStub.restore();
});

Expand All @@ -50,7 +51,7 @@ describe('Generator with environment version', () => {
});

describe('with required environment', () => {
before(() => {
beforeAll(() => {
getVersionStub.returns('3.0.1');
});

Expand All @@ -59,11 +60,11 @@ describe('Generator with environment version', () => {
});

describe('with ignoreVersionCheck', () => {
before(() => {
beforeAll(() => {
dummy.options.ignoreVersionCheck = true;
});

after(() => {
afterAll(() => {
dummy.options.ignoreVersionCheck = false;
});

Expand All @@ -82,7 +83,7 @@ describe('Generator with environment version', () => {
});

describe('with less than required environment', () => {
before(() => {
beforeAll(() => {
getVersionStub.returns('3.0.0');
});

Expand All @@ -100,11 +101,11 @@ describe('Generator with environment version', () => {
});

describe('with ignoreVersionCheck', () => {
before(() => {
beforeAll(() => {
dummy.options.ignoreVersionCheck = true;
});

after(() => {
afterAll(() => {
dummy.options.ignoreVersionCheck = false;
});

Expand All @@ -129,7 +130,7 @@ describe('Generator with environment version', () => {
});

describe('with less than required inquirer', () => {
before(() => {
beforeAll(() => {
getVersionStub.withArgs('inquirer').returns('7.1.0');
});

Expand All @@ -147,11 +148,11 @@ describe('Generator with environment version', () => {
});

describe('with ignoreVersionCheck', () => {
before(() => {
beforeAll(() => {
dummy.options.ignoreVersionCheck = true;
});

after(() => {
afterAll(() => {
dummy.options.ignoreVersionCheck = false;
});

Expand All @@ -172,8 +173,7 @@ describe('Generator with environment version', () => {
});

describe('mocked 2.8.1', () => {
before(function () {
this.timeout(100_000);
beforeAll(() => {
env = new Environment({ skipInstall: true, adapter: new TestAdapter() });
env.getVersion = undefined;

Expand All @@ -186,7 +186,7 @@ describe('Generator with environment version', () => {
skipCheckEnv: true,
'skip-install': true,
});
});
}, 100_000);

describe('#checkEnvironmentVersion', () => {
describe('without args', () => {
Expand All @@ -199,11 +199,11 @@ describe('Generator with environment version', () => {
});

describe('with ignoreVersionCheck', () => {
before(() => {
beforeAll(() => {
dummy.options.ignoreVersionCheck = true;
});

after(() => {
afterAll(() => {
dummy.options.ignoreVersionCheck = false;
});

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/generator-defaults/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// object, which means that you can use the API as if you were extending
// `Base`.

import Base from '../../../../src/index.js';
import Base from '../../../../dist/index.js';
import options from './options.js';
import prompts from './prompts.js';

Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/generator-defaults/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixtures/generator-mocha/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// It works with simple generator, if you need to do a bit more complex
// stuff, extends from Generator.Base and defines your generator steps
// in several methods.
import Base from '../../../src/index.js';
import Base from '../../../dist/index.js';

class Generator extends Base {
notEmpty() {}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/options-generator/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// object, which means that you can use the API as if you were extending
// `Base`.

import Base from '../../../src/index.js';
import Base from '../../../dist/index.js';

export default class Generator extends Base {
constructor(args, opts) {
Expand Down
9 changes: 4 additions & 5 deletions test/fs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'node:assert';
import path from 'node:path';
import { afterEach, before, beforeEach, describe, esmocha, it } from 'esmocha';
import { afterEach, beforeAll, beforeEach, describe, it, vi } from 'vitest';
import { TestAdapter } from '@yeoman/adapter/testing';
import { type SinonStub, stub as sinonStub } from 'sinon';
import type { Data as TemplateData } from 'ejs';
Expand Down Expand Up @@ -91,10 +91,9 @@ describe('generators.Base (actions/fs)', () => {
let gen: Base;
let base: BaseGenerator;

before(function () {
this.timeout(10_000);
beforeAll(() => {
gen = new Base({ env: createEnv(), resolved: 'unknown', help: true });
});
}, 10_000);

beforeEach(() => {
returns = {};
Expand All @@ -103,7 +102,7 @@ describe('generators.Base (actions/fs)', () => {
// Why not use a sinonStub for base.config as is done in #renderTemplate and #renderTemplateAsync below?
// base get config is not being tested in any way below.
// @ts-expect-error Config is a string (not a symbol) and we know it exists on base https://github.com/DefinitelyTyped/DefinitelyTyped/issues/33173
esmocha.spyOn(base, 'config', 'get').mockReturnValue({
vi.spyOn(base, 'config', 'get').mockReturnValue({
getAll() {
return configGetAll;
},
Expand Down
6 changes: 3 additions & 3 deletions test/generators-compose-workflow.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os from 'node:os';
import path from 'node:path';
import { beforeEach, describe, it } from 'esmocha';
import { beforeEach, describe, it } from 'vitest';
import { mkdirSync } from 'node:fs';
import { TestAdapter } from '@yeoman/adapter/testing';
import type { SinonSpy } from 'sinon';
Expand Down Expand Up @@ -32,9 +32,9 @@ describe('Multiples generators', () => {
let spyEnd2: SinonSpy;
let spyExec3: SinonSpy;

beforeEach(helpers.setUpTestDirectory(tmpdir));
beforeEach(async () => {
await helpers.prepareTemporaryDir().run();

beforeEach(() => {
env = createEnv();
mkdirSync(resolveddir, { recursive: true });
Dummy = class extends Base {};
Expand Down
2 changes: 1 addition & 1 deletion test/generators.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EventEmitter from 'node:events';
import path from 'node:path';
import os from 'node:os';
import { beforeEach, describe, it } from 'esmocha';
import { beforeEach, describe, it } from 'vitest';
import { TestAdapter } from '@yeoman/adapter/testing';
import Environment from 'yeoman-environment';
import assert from 'yeoman-assert';
Expand Down
Loading

0 comments on commit 0fffbf5

Please sign in to comment.