Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli-plugin-vitest): add new plugin to generate project with vitest #372

Merged
merged 1 commit into from
Mar 3, 2024
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
23 changes: 0 additions & 23 deletions .nycrc

This file was deleted.

4 changes: 4 additions & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ module.exports = {
text: 'Eslint',
link: 'https://github.com/tsedio/tsed-cli/tree/master/packages/cli-plugin-eslint'
},
{
text: 'Vitest',
link: 'https://github.com/tsedio/tsed-cli/tree/master/packages/cli-plugin-vitest'
},
{
text: 'Jest',
link: 'https://github.com/tsedio/tsed-cli/tree/master/packages/cli-plugin-jest'
Expand Down
Binary file added docs/.vuepress/public/vitest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ features:
- title: tsed generate
details: Generate provider, controllers, services and pipes with a simple command. The CLI will also create simple test shells for all of these.
- title: tsed add
details: Install a CLI plugins to extend the CLI capabilities. Out-of-the-box support tslint, prettier, mocha, jest, passport, etc...
details: Install a CLI plugins to extend the CLI capabilities. Out-of-the-box support tslint, prettier, vitest, mocha, jest, passport, etc...
contributors:
classes: bg-gray-lighter mb-10
title: Our awesome <b>contributors</b>
Expand Down Expand Up @@ -74,6 +74,9 @@ frameworks:
- title: Koa.js
href: https://koajs.com/
src: /koa.svg
- title: Vitest
href: https://vitest.dev/
src: /vitest.png
- title: Jest
href: https://jestjs.io/
src: /jest.svg
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
coverageThreshold: {
global: {
statements: 73.68,
branches: 70.93,
branches: 70.84,
functions: 52.15,
lines: 73.68
}
Expand Down
13 changes: 13 additions & 0 deletions packages/cli-plugin-vitest/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules
docs
docs-references
lib
dist
coverage
.nyc_output
*-lock.json
*.lock
benchmarks.*
**/generated

**/*.js
1 change: 1 addition & 0 deletions packages/cli-plugin-vitest/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@tsed/eslint/node.js");
4 changes: 4 additions & 0 deletions packages/cli-plugin-vitest/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src
test
tsconfig.compile.json
tsconfig.json
15 changes: 15 additions & 0 deletions packages/cli-plugin-vitest/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html

module.exports = {
...require("@tsed/jest-config"),
roots: ["<rootDir>/src", "<rootDir>/test"],
coverageThreshold: {
global: {
statements: 78.94,
branches: 37.5,
functions: 60,
lines: 78.94
}
}
};
35 changes: 35 additions & 0 deletions packages/cli-plugin-vitest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@tsed/cli-plugin-vitest",
"version": "5.0.2",
"description": "Ts.ED CLI plugin. Add Jest support",
"source": "./src/index.ts",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"typings": "./lib/types/index.d.ts",
"exports": {
"types": "./lib/types/index.d.ts",
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js",
"default": "./lib/esm/index.js"
},
"scripts": {
"build": "yarn build:ts",
"build:ts": "tsc --build tsconfig.json && tsc --build tsconfig.esm.json && cp scripts/templateDir.esm.js lib/esm/utils/templateDir.js",
"lint": "eslint '**/*.{ts,js}'",
"lint:fix": "eslint '**/*.{ts,js}' --fix",
"test": "cross-env NODE_ENV=test yarn jest --max-workers=2 --passWithNoTests && jest-coverage-thresholds-bumper"
},
"devDependencies": {
"@tsed/cli": "5.0.3",
"@tsed/cli-core": "5.0.3",
"@tsed/eslint": "5.0.3",
"@tsed/jest-config": "5.0.3",
"@tsed/typescript": "5.0.3",
"cross-env": "7.0.3",
"eslint": "8.22.0",
"jest": "29.5.0"
},
"dependencies": {
"tslib": "2.3.1"
}
}
31 changes: 31 additions & 0 deletions packages/cli-plugin-vitest/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# @tsed/cli-plugin-jest

<p style="text-align: center" align="center">
<a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
</p>

[![Build & Release](https://github.com/tsedio/tsed-cli/workflows/Build%20&%20Release/badge.svg?branch=master)](https://github.com/tsedio/tsed-cli/actions?query=workflow%3A%22Build+%26+Release%22)

> Ts.ED CLI plugin. Add Jest support

## Features

Please refer to the [documentation](https://cli.tsed.io/) for more details.

## Installation

```bash
npm install -g @tsed/cli-plugin-jest
```

## Contributors
Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html)

<a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>


## Backers

Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]

<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
5 changes: 5 additions & 0 deletions packages/cli-plugin-vitest/scripts/templateDir.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {getTemplateDirectory} from "@tsed/cli-core";
import path from "node:path";
import {fileURLToPath} from "node:url";

export const TEMPLATE_DIR = getTemplateDirectory(path.dirname(fileURLToPath(import.meta.url)));
42 changes: 42 additions & 0 deletions packages/cli-plugin-vitest/src/CliPluginVitestModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {Module, OnAdd, ProjectPackageJson} from "@tsed/cli-core";
import {RuntimesModule} from "@tsed/cli";
import {Inject} from "@tsed/di";
import {VitestGenerateHook} from "./hooks/VitestGenerateHook";
import {VitestInitHook} from "./hooks/VitestInitHook";

@Module({
imports: [VitestInitHook, VitestGenerateHook]
})
export class CliPluginVitestModule {
@Inject()
runtimes: RuntimesModule;

@Inject()
packageJson: ProjectPackageJson;

@OnAdd("@tsed/cli-plugin-vitest")
install() {
this.addScripts();
this.addDevDependencies();
}

addScripts() {
const runtime = this.runtimes.get();

this.packageJson.addScripts({
test: `${runtime.run("test:lint")} && ${runtime.run("test:coverage")} `,
"test:unit": "cross-env NODE_ENV=test vitest run",
"test:watch": "cross-env NODE_ENV=test vitest",
"test:coverage": `cross-env NODE_ENV=test vitest run --coverage`
});
}

addDevDependencies() {
this.packageJson.addDevDependencies({
vitest: "latest",
"unplugin-swc": "latest",
"@vitest/coverage-v8": "latest",
"@swc/core": "latest"
});
}
}
68 changes: 68 additions & 0 deletions packages/cli-plugin-vitest/src/hooks/VitestGenerateHook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {GenerateCmdContext} from "@tsed/cli";
import normalizePath from "normalize-path";
import {Inject, Injectable, OnExec, SrcRendererService, Tasks} from "@tsed/cli-core";

import {TEMPLATE_DIR} from "../utils/templateDir";

@Injectable()
export class VitestGenerateHook {
@Inject()
srcRenderService: SrcRendererService;

@OnExec("generate")
onGenerateExec(ctx: GenerateCmdContext): Tasks {
const {symbolPath} = ctx;
const {specTemplate, integrationTemplate, relativeSrcPath} = this.mapOptions(ctx);

return [
{
title: `Generate ${ctx.type} spec file to '${symbolPath}.spec.ts'`,
enabled() {
return !(ctx.type === "server" || ctx.type.includes(":dataSource") || ctx.type.includes(":connection"));
},
task: () =>
this.srcRenderService.render(
specTemplate,
{...ctx, relativeSrcPath},
{
output: `${symbolPath}.spec.ts`,
templateDir: TEMPLATE_DIR
}
)
},
{
title: `Generate ${ctx.type} integration file '${symbolPath}.integration.spec.ts'`,
enabled() {
return ["controller", "server"].includes(ctx.type);
},
task: () =>
this.srcRenderService.render(
integrationTemplate,
{...ctx, relativeSrcPath},
{
output: `${symbolPath}.integration.spec.ts`,
templateDir: TEMPLATE_DIR
}
)
}
];
}

private mapOptions(options: GenerateCmdContext) {
const type = [options.type, options.templateType].filter(Boolean).join(".");

const specTemplate = this.srcRenderService.templateExists(`generate/${type}.spec.hbs`, {templateDir: TEMPLATE_DIR})
? `generate/${type}.spec.hbs`
: "generate/generic.spec.hbs";

const integrationTemplate = this.srcRenderService.templateExists(`generate/${type}.integration.hbs`, {templateDir: TEMPLATE_DIR})
? `generate/${type}.integration.hbs`
: "generate/generic.integration.hbs";

return {
specTemplate,
integrationTemplate,
relativeSrcPath: normalizePath(this.srcRenderService.relativeFrom(`${options.symbolPath}.integration.spec.ts`))
};
}
}
39 changes: 39 additions & 0 deletions packages/cli-plugin-vitest/src/hooks/VitestInitHook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
Inject,
Injectable,
OnExec,
ProjectPackageJson,
RootRendererService,
ScriptsRendererService,
SrcRendererService
} from "@tsed/cli-core";
import {TEMPLATE_DIR} from "../utils/templateDir";

@Injectable()
export class VitestInitHook {
@Inject()
protected packageJson: ProjectPackageJson;

@Inject()
protected srcRenderer: SrcRendererService;

@Inject()
protected rootRenderer: RootRendererService;

@Inject()
protected scriptsRenderer: ScriptsRendererService;

@OnExec("init")
onInitExec() {
return [
{
title: "Generate files for vitest",
task: (ctx: any) => {
return this.rootRenderer.renderAll(["vitest.config.ts.hbs"], ctx, {
templateDir: `${TEMPLATE_DIR}/init`
});
}
}
];
}
}
5 changes: 5 additions & 0 deletions packages/cli-plugin-vitest/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {CliPluginVitestModule} from "./CliPluginVitestModule";

export * from "./utils/templateDir";

export default CliPluginVitestModule;
3 changes: 3 additions & 0 deletions packages/cli-plugin-vitest/src/utils/templateDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {getTemplateDirectory} from "@tsed/cli-core";

export const TEMPLATE_DIR = getTemplateDirectory(__dirname);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect, describe, it } from "vitest";
import { {{symbolName}} } from "./{{symbolPathBasename}}";

describe("{{symbolName}}", () => {
it("should do something", () => {
@{{symbolName}}({})
class Test {
}

expect(typeof {{symbolName}}).toBe("function")
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, describe, it } from "vitest";
import { Store } from "@tsed/core";
import { {{symbolName}} } from "./{{symbolPathBasename}}";

describe("{{symbolName}}", () => {
it("should store options", () => {
class Test {
@{{symbolName}}({options: "options"})
method() param: string){}
}

const store = Store.fromMethod(Test, "method");

expect(store.get({{symbolName}})).toEqual({options: "options"});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, describe, it } from "vitest";
import { {{symbolName}} } from "./{{symbolPathBasename}}";

describe("{{symbolName}}", () => {
it("should do something", () => {
class Test {
@{{symbolName}}({})
method(){}
}

expect(typeof {{symbolName}}).toBe("function")
expect(typeof {{symbolName}}()).toBe("function")
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, describe, it } from "vitest";
import { Store } from "@tsed/core";
import { {{symbolName}} } from "./{{symbolPathBasename}}";

describe("{{symbolName}}", () => {
it("should store options", () => {
class Test {
method(@{{symbolName}}({options: "options"}) param: string){}
}

const store = Store.from(Test, "method", 0)

expect(store.get({{symbolName}})).toEqual({options: "options"});
});
});
Loading
Loading