Skip to content

Commit

Permalink
[add] Test framework based on ts-jest & swagger-typescript-api (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery authored Sep 14, 2024
1 parent b8459fd commit bdc14b6
Show file tree
Hide file tree
Showing 26 changed files with 2,910 additions and 608 deletions.
6 changes: 2 additions & 4 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
ReadMe.md
node_modules/
dist/
.data/
.*/
start.sh
.husky/
.github/
.vscode/
.eslintrc.json
*.pem
26 changes: 15 additions & 11 deletions .github/workflows/deploy-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ jobs:
cat > .env <<EOF
${{ secrets.ENV_FILE }}
EOF
- uses: docker/setup-qemu-action@v2
- uses: docker/setup-buildx-action@v2
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3

- name: Build Image
uses: docker/build-push-action@v3
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64
Expand All @@ -47,23 +47,27 @@ jobs:
mv ${{ env.ARTIFACT_PATH }} ./docker-compose.yml ./start.sh ./build
- name: Transport Image
uses: garygrossgarten/github-action-scp@release
uses: appleboy/scp-action@v0.1.7
if: ${{ env.HOST }}
with:
local: ./build
remote: /tmp
source: ./build/*.*
target: /tmp/${{ env.BOX_NAME }}
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
privateKey: ${{ secrets.SSH_KEY }}
password: ${{ secrets.SSH_KEY }}
strip_components: 1

- name: Run Image
uses: garygrossgarten/github-action-ssh@release
if: ${{ env.HOST }}
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
privateKey: ${{ secrets.SSH_KEY }}
password: ${{ secrets.SSH_KEY }}
command: |
mv /tmp/docker-compose.yml /tmp/start.sh ~/
chmod +x ~/start.sh
sudo ~/start.sh ${{ env.ARTIFACT_PATH }}
mkdir -p ~/${{ env.BOX_NAME }}
cd /tmp/${{ env.BOX_NAME }}
mv docker-compose.yml start.sh ~/${{ env.BOX_NAME }}
cd ~/${{ env.BOX_NAME }}
chmod +x start.sh
echo '${{ secrets.SSH_KEY }}' | sudo -S ./start.sh /tmp/${{ env.BOX_NAME }}/${{ env.ARTIFACT_PATH }}
15 changes: 10 additions & 5 deletions .github/workflows/publish-type.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ jobs:
contents: write
packages: write
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v4
with:
version: 9
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
registry-url: https://npm.pkg.github.com
cache: pnpm
- name: Inject Environment variables
run: |
cat > .env <<EOF
${{ secrets.ENV_FILE }}
EOF
- name: Install, Build & Publish
run: |
pnpm i && pnpm build
pnpm i && npm test
cd type/
npm publish
env:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
node_modules/
package-lock.json
yarn.lock
test/client.ts
dist/
type/*.d.ts
.env*
Expand Down
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npm test
npm run lint
2 changes: 1 addition & 1 deletion .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npm run build
npm test
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Reference: https://pnpm.io/docker#example-1-build-a-bundle-in-a-docker-container

FROM node:18-slim AS base
FROM node:20-slim AS base
RUN apt-get update && \
apt-get install curl -y --no-install-recommends
ENV PNPM_HOME="/pnpm"
Expand Down
38 changes: 20 additions & 18 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,32 @@
4. ORM framework: [TypeORM][12]
5. API document: [Swagger][13]
6. Mock API: [OpenAPI backend][14]
7. Test framework: [Jest][15]

## Major features

1. [API entry & Health checking](source/controller/Base.ts)
2. [User & Session](source/controller/User.ts)
3. [OAuth sign in](source/controller/OAuth.ts)
- recommend to use with [Next SSR middleware][15]
- recommend to use with [Next SSR middleware][16]
4. [Activity logging](source/controller/ActivityLog.ts)

## Best practice

1. Install GitHub apps in your organization or account:

1. [Probot settings][16]: set up Issue labels & Pull Request rules
2. [PR badge][17]: set up Online [VS Code][18] editor entries in Pull Request description
1. [Probot settings][17]: set up Issue labels & Pull Request rules
2. [PR badge][18]: set up Online [VS Code][19] editor entries in Pull Request description

2. Click the **[<kbd>Use this template</kbd>][19] button** on the top of this GitHub repository's home page, then create your own repository in the app-installed namespace above
2. Click the **[<kbd>Use this template</kbd>][20] button** on the top of this GitHub repository's home page, then create your own repository in the app-installed namespace above

3. Click the **[<kbd>Open in GitHub codespaces</kbd>][8] button** on the top of ReadMe file, then an **online VS Code development environment** will be started immediately

4. Recommend to add a [Notification step in GitHub actions][20] for your Team IM app
4. Recommend to add a [Notification step in GitHub actions][21] for your Team IM app

5. Remind the PMs & users of your product to submit **Feature/Enhancement** requests or **Bug** reports with [Issue forms][21] instead of IM messages or Mobile Phone calls
5. Remind the PMs & users of your product to submit **Feature/Enhancement** requests or **Bug** reports with [Issue forms][22] instead of IM messages or Mobile Phone calls

6. Collect all these issues into [Project kanbans][22], then create **Pull requests** & add `closes #issue_number` into its description for automation
6. Collect all these issues into [Project kanbans][23], then create **Pull requests** & add `closes #issue_number` into its description for automation

## API Usage

Expand Down Expand Up @@ -72,7 +73,7 @@ pnpm i
pnpm dev
```

or just press <kbd>F5</kbd> key in [VS Code][18].
or just press <kbd>F5</kbd> key in [VS Code][19].

### Migration

Expand Down Expand Up @@ -107,15 +108,15 @@ pnpm container

```shell
git checkout master
git tag v0.9.0 # this version tag comes from ./package.json
git tag v1.0.0 # this version tag comes from ./package.json
git push origin master --tags
```

### Publish Type Package

```shell
git checkout master
git tag type-v0.9.0 # this version tag comes from ./type/package.json
git tag type-v1.0.0 # this version tag comes from ./type/package.json
git push origin master --tags
```

Expand All @@ -133,11 +134,12 @@ git push origin master --tags
[12]: https://typeorm.io/
[13]: https://swagger.io/
[14]: https://github.com/anttiviljami/openapi-backend
[15]: https://github.com/idea2app/Next-SSR-middleware
[16]: https://github.com/apps/settings
[17]: https://pullrequestbadge.com/
[17]: https://code.visualstudio.com/
[19]: https://github.com/new?template_name=REST-Node-ts&template_owner=idea2app
[20]: https://github.com/kaiyuanshe/kaiyuanshe.github.io/blob/bb4675a56bf1d6b207231313da5ed0af7cf0ebd6/.github/workflows/pull-request.yml#L32-L56
[21]: https://github.com/idea2app/REST-Node-ts/issues/new/choose
[22]: https://github.com/idea2app/REST-Node-ts/projects
[15]: https://jestjs.io/
[16]: https://github.com/idea2app/Next-SSR-middleware
[17]: https://github.com/apps/settings
[18]: https://pullrequestbadge.com/
[19]: https://code.visualstudio.com/
[20]: https://github.com/new?template_name=REST-Node-ts&template_owner=idea2app
[21]: https://github.com/kaiyuanshe/kaiyuanshe.github.io/blob/bb4675a56bf1d6b207231313da5ed0af7cf0ebd6/.github/workflows/pull-request.yml#L32-L56
[22]: https://github.com/idea2app/REST-Node-ts/issues/new/choose
[23]: https://github.com/idea2app/REST-Node-ts/projects
49 changes: 34 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"source": "source/index.ts",
"main": "dist/index.js",
"engines": {
"node": ">=18"
"node": ">=20"
},
"dependencies": {
"@koa/cors": "^5.0.0",
"@koa/multer": "^3.0.2",
"@koa/router": "^13.0.0",
"@koa/router": "^13.0.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"cross-env": "^7.0.3",
Expand All @@ -33,33 +33,44 @@
"koa-mount": "^4.0.0",
"koa2-swagger-ui": "^5.10.0",
"koagger": "^0.3.0",
"marked": "^14.0.0",
"mobx-restful": "^1.0.0",
"marked": "^14.1.2",
"mobx-github": "^0.3.4",
"mobx-restful": "^1.0.1",
"pg": "^8.12.0",
"pg-connection-string": "^2.6.4",
"reflect-metadata": "^0.2.2",
"routing-controllers": "^0.10.4",
"routing-controllers-openapi": "^4.0.0",
"tslib": "^2.6.3",
"tslib": "^2.7.0",
"typeorm": "npm:@helveg/typeorm@^0.3.20",
"undici": "^6.19.8",
"web-utility": "^4.4.0"
},
"devDependencies": {
"@octokit/openapi-types": "^22.2.0",
"@types/jest": "^29.5.13",
"@types/jsonwebtoken": "^9.0.6",
"@types/koa": "^2.15.0",
"@types/koa-logger": "^3.1.5",
"@types/node": "^18.19.44",
"@typescript-eslint/eslint-plugin": "^8.1.0",
"@typescript-eslint/parser": "^8.1.0",
"@types/node": "^20.16.5",
"@typescript-eslint/eslint-plugin": "^8.5.0",
"@typescript-eslint/parser": "^8.5.0",
"eslint": "^8.57.0",
"eslint-plugin-simple-import-sort": "^12.1.1",
"husky": "^9.1.4",
"lint-staged": "^15.2.9",
"husky": "^9.1.6",
"jest": "^29.7.0",
"koajax": "^3.0.2",
"lint-staged": "^15.2.10",
"prettier": "^3.3.3",
"sqlite3": "^5.1.7",
"ts-node-dev": "^2.0.0",
"typescript": "~5.5.4"
"start-server-and-test": "^2.0.7",
"swagger-typescript-api": "^13.0.22",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "~5.6.2"
},
"resolutions": {
"native-file-system-adapter": "npm:@tech_query/native-file-system-adapter@^3.0.1"
},
"prettier": {
"singleQuote": true,
Expand All @@ -70,10 +81,18 @@
"lint-staged": {
"*.{md,json,yml,ts}": "prettier --write"
},
"jest": {
"preset": "ts-jest"
},
"scripts": {
"prepare": "husky",
"dev": "ts-node-dev source/",
"test": "lint-staged",
"prepare": "husky || true",
"dev": "cross-env TS_NODE_TRANSPILE_ONLY=true node -r ts-node/register --watch source/",
"lint": "lint-staged",
"build-test": "swagger-typescript-api -p http://localhost:8080/docs/spec -o test/ -n client.ts",
"start-test": "rm -rf .data/ && node dist/",
"jest": "jest",
"debug": "server-test start-test 8080 jest",
"test": "npm run build && server-test start-test 8080 build-test && npm run debug",
"build": "rm -rf dist/ type/*.d.ts && tsc && mv dist/model/*.d.ts type/",
"start": "cross-env NODE_ENV=production node dist/",
"typeorm": "typeorm-ts-node-commonjs -d source/model/index.ts",
Expand Down
Loading

0 comments on commit bdc14b6

Please sign in to comment.