-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli-plugin-vitest): add new plugin to generate project with vitest
- Loading branch information
Showing
34 changed files
with
634 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("@tsed/eslint/node.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
src | ||
test | ||
tsconfig.compile.json | ||
tsconfig.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "workspace:*", | ||
"@tsed/cli-core": "workspace:*", | ||
"@tsed/eslint": "workspace:*", | ||
"@tsed/jest-config": "workspace:*", | ||
"@tsed/typescript": "workspace:*", | ||
"cross-env": "7.0.3", | ||
"eslint": "8.22.0", | ||
"jest": "29.5.0" | ||
}, | ||
"dependencies": { | ||
"tslib": "2.3.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
packages/cli-plugin-vitest/src/hooks/VitestGenerateHook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`)) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
}); | ||
} | ||
} | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
12 changes: 12 additions & 0 deletions
12
packages/cli-plugin-vitest/templates/generate/decorator.class.spec.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/cli-plugin-vitest/templates/generate/decorator.endpoint.spec.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
packages/cli-plugin-vitest/templates/generate/decorator.method.spec.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
packages/cli-plugin-vitest/templates/generate/decorator.param.spec.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}); | ||
}); | ||
}); |
Oops, something went wrong.