diff --git a/.eslintignore b/.eslintignore index 62562b7..618ef2b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,3 @@ +test/fixtures coverage -node_modules +__snapshots__ diff --git a/.eslintrc b/.eslintrc index c799fe5..9bcdb46 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,6 @@ { - "extends": "eslint-config-egg" + "extends": [ + "eslint-config-egg/typescript", + "eslint-config-egg/lib/rules/enforce-node-prefix" + ] } diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md deleted file mode 100644 index b021f10..0000000 --- a/.github/ISSUE_TEMPLATE.md +++ /dev/null @@ -1,21 +0,0 @@ - - -* **Node Version**: -* **Egg Version**: -* **Plugin Name**: -* **Plugin Version**: -* **Platform**: - - diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 48f9944..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,24 +0,0 @@ - - -##### Checklist - - -- [ ] `npm test` passes -- [ ] tests and/or benchmarks are included -- [ ] documentation is changed or added -- [ ] commit message follows commit guidelines - -##### Affected core subsystem(s) - - - -##### Description of change - diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index f38bb73..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: CI - -on: - push: - branches: [ master ] - - pull_request: - branches: [ master ] - - workflow_dispatch: {} - -jobs: - Job: - name: Node.js - uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 - with: - os: 'ubuntu-latest' - version: '14, 16, 18, 20' diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..e7eb885 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,17 @@ +name: CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + Job: + name: Node.js + uses: node-modules/github-actions/.github/workflows/node-test.yml@master + with: + os: 'ubuntu-latest, windows-latest, macos-latest' + version: '18, 20, 22' + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1612587..970aedc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,17 +1,12 @@ name: Release - on: push: branches: [ master ] - workflow_dispatch: {} - jobs: release: name: Node.js - uses: artusjs/github-actions/.github/workflows/node-release.yml@v1 + uses: eggjs/github-actions/.github/workflows/node-release.yml@master secrets: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GIT_TOKEN: ${{ secrets.GIT_TOKEN }} - with: - checkTest: false diff --git a/.gitignore b/.gitignore index 1a5bf14..aa0ca22 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,9 @@ coverage/ run/ .DS_Store *.swp - +test/fixtures/**/run +.tshy* +.eslintcache +dist +package-lock.json +.package-lock.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 92a93a9..6b9c508 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,3 +17,20 @@ ### Features * upgrade all deps to latest versions ([#6](https://github.com/eggjs/egg-tracer/issues/6)) ([516ae2b](https://github.com/eggjs/egg-tracer/commit/516ae2b570e3fceb523d7ca37443310da10ac5ed)) + +--- + + +1.1.0 / 2017-09-07 +================== + +**features** + * [[`02501c6`](http://github.com/eggjs/egg-tracer/commit/02501c6623da0acfaca660b71d12e66afa5d7810)] - feat: support app or agent get tracer (#2) (hui <>) + +**others** + * [[`840724e`](http://github.com/eggjs/egg-tracer/commit/840724e8c5dc31908397004ede4a6e5a52555e0e)] - chore: upgrade deps and fix test (#1) (Haoliang Gao <>) + +1.0.0 / 2016-08-16 +================== + + * first version diff --git a/History.md b/History.md deleted file mode 100644 index a9f2f69..0000000 --- a/History.md +++ /dev/null @@ -1,14 +0,0 @@ - -1.1.0 / 2017-09-07 -================== - -**features** - * [[`02501c6`](http://github.com/eggjs/egg-tracer/commit/02501c6623da0acfaca660b71d12e66afa5d7810)] - feat: support app or agent get tracer (#2) (hui <>) - -**others** - * [[`840724e`](http://github.com/eggjs/egg-tracer/commit/840724e8c5dc31908397004ede4a6e5a52555e0e)] - chore: upgrade deps and fix test (#1) (Haoliang Gao <>) - -1.0.0 / 2016-08-16 -================== - - * first version diff --git a/README.md b/README.md index 17df430..06b02d5 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,28 @@ -# egg-tracer +# @eggjs/tracer [![NPM version][npm-image]][npm-url] [![Test coverage][codecov-image]][codecov-url] [![Known Vulnerabilities][snyk-image]][snyk-url] [![npm download][download-image]][download-url] - -[npm-image]: https://img.shields.io/npm/v/egg-tracer.svg?style=flat-square -[npm-url]: https://npmjs.org/package/egg-tracer -[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-tracer.svg?style=flat-square -[codecov-url]: https://codecov.io/github/eggjs/egg-tracer?branch=master -[snyk-image]: https://snyk.io/test/npm/egg-tracer/badge.svg?style=flat-square -[snyk-url]: https://snyk.io/test/npm/egg-tracer -[download-image]: https://img.shields.io/npm/dm/egg-tracelog.svg?style=flat-square -[download-url]: https://npmjs.org/package/egg-tracer +[![Node.js Version](https://img.shields.io/node/v/@eggjs/tracer.svg?style=flat)](https://nodejs.org/en/download/) +[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com) +![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/eggjs/security) + +[npm-image]: https://img.shields.io/npm/v/@eggjs/tracer.svg?style=flat-square +[npm-url]: https://npmjs.org/package/@eggjs/tracer +[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/tracer.svg?style=flat-square +[codecov-url]: https://codecov.io/github/eggjs/tracer?branch=master +[snyk-image]: https://snyk.io/test/npm/@eggjs/tracer/badge.svg?style=flat-square +[snyk-url]: https://snyk.io/test/npm/@eggjs/tracer +[download-image]: https://img.shields.io/npm/dm/@eggjs/tracer.svg?style=flat-square +[download-url]: https://npmjs.org/package/@eggjs/tracer tracer plugin for egg. ## Install ```bash -npm i egg-tracer +npm i @eggjs/tracer ``` ## Usage @@ -30,7 +33,7 @@ Enable tracer plugin: // config/plugin.js exports.tracer = { enable: true, - package: 'egg-tracer', + package: '@eggjs/tracer', }; ``` @@ -38,7 +41,7 @@ exports.tracer = { ```js // my_tracer.js -const Tracer = require('egg-tracer'); +const { Tracer } = require('@eggjs/tracer'); const counter = 0; @@ -66,3 +69,9 @@ Please open an issue [here](https://github.com/eggjs/egg/issues). ## License [MIT](LICENSE) + +## Contributors + +[![Contributors](https://contrib.rocks/image?repo=eggjs/tracer)](https://github.com/eggjs/tracer/graphs/contributors) + +Made with [contributors-img](https://contrib.rocks). diff --git a/agent.js b/agent.js deleted file mode 100644 index e6e13eb..0000000 --- a/agent.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./index'); diff --git a/app.js b/app.js deleted file mode 100644 index e6e13eb..0000000 --- a/app.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./index'); diff --git a/app/extend/agent.js b/app/extend/agent.js deleted file mode 100644 index 89f19b5..0000000 --- a/app/extend/agent.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('../../lib/index.js'); diff --git a/app/extend/application.js b/app/extend/application.js deleted file mode 100644 index 89f19b5..0000000 --- a/app/extend/application.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('../../lib/index.js'); diff --git a/app/extend/context.js b/app/extend/context.js deleted file mode 100644 index 47e46a2..0000000 --- a/app/extend/context.js +++ /dev/null @@ -1,14 +0,0 @@ -const TRACER = Symbol('tracer'); - -module.exports = { - get tracer() { - if (!this[TRACER]) { - this[TRACER] = new this.app.config.tracer.Class(this); - } - return this[TRACER]; - }, - - get traceId() { - return this.tracer.traceId; - }, -}; diff --git a/config/config.default.js b/config/config.default.js deleted file mode 100644 index 82ba0bf..0000000 --- a/config/config.default.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * tracer config - * @member Config#tracer - * @property {Tracer} Class - tracer class name - */ -exports.tracer = { - Class: require('../lib/tracer'), -}; diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 85cf065..0000000 --- a/index.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -declare module 'egg' { - interface Tracer { - traceId: string; - } - - interface Context { - tracer: Tracer; - traceId: string; - } -} diff --git a/index.js b/index.js deleted file mode 100644 index 86979ab..0000000 --- a/index.js +++ /dev/null @@ -1,8 +0,0 @@ -const isReady = Symbol.for('egg_tracer_is_ready'); - -module.exports = app => { - app.ready(() => { - app[isReady] = true; - }); -}; - diff --git a/index.test-d.ts b/index.test-d.ts deleted file mode 100644 index d85ed35..0000000 --- a/index.test-d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { expectType } from 'tsd'; -import '.'; -import { Context } from 'egg'; - -const ctx = {} as Context; - -expectType(ctx.traceId); -expectType(ctx.tracer.traceId); diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 5f572d3..0000000 --- a/lib/index.js +++ /dev/null @@ -1,20 +0,0 @@ -const cacheTracer = Symbol('before_ready_tracer'); -const isReady = Symbol.for('egg_tracer_is_ready'); - -module.exports = { - get tracer() { - if (this[isReady]) { - return new this.config.tracer.Class({ - app: this, - }); - } - - if (!this[cacheTracer]) { - this[cacheTracer] = new this.config.tracer.Class({ - app: this, - }); - } - - return this[cacheTracer]; - }, -}; diff --git a/lib/tracer.js b/lib/tracer.js deleted file mode 100644 index 891f66f..0000000 --- a/lib/tracer.js +++ /dev/null @@ -1,19 +0,0 @@ - -const { randomUUID } = require('crypto'); - -const TRACE_ID = Symbol('traceId'); - -class Tracer { - constructor(ctx) { - this.ctx = ctx; - } - - get traceId() { - if (!this[TRACE_ID]) { - this[TRACE_ID] = randomUUID(); - } - return this[TRACE_ID]; - } -} - -module.exports = Tracer; diff --git a/package.json b/package.json index 12a14d9..8d6f3a5 100644 --- a/package.json +++ b/package.json @@ -1,21 +1,18 @@ { - "name": "egg-tracer", + "name": "@eggjs/tracer", "version": "2.1.0", + "publishConfig": { + "access": "public" + }, "description": "tracer for egg", "eggPlugin": { - "name": "tracer" + "name": "tracer", + "exports": { + "import": "./dist/esm", + "require": "./dist/commonjs", + "typescript": "./src" + } }, - "main": "lib/tracer.js", - "types": "index.d.ts", - "files": [ - "lib", - "app", - "config", - "agent.js", - "index.js", - "app.js", - "index.d.ts" - ], "keywords": [ "egg", "egg-plugin", @@ -25,26 +22,6 @@ "spanId", "parentSpanId" ], - "dependencies": {}, - "devDependencies": { - "egg": "^3.16.0", - "egg-bin": "^6.4.0", - "egg-mock": "^5.10.6", - "eslint": "^8.41.0", - "eslint-config-egg": "^12.2.1", - "tsd": "^0.28.1" - }, - "engines": { - "node": ">=14.0.0" - }, - "scripts": { - "test": "npm run lint -- --fix && npm run test-local", - "test-local": "egg-bin test", - "cov": "egg-bin cov", - "tsd": "tsd", - "lint": "eslint . && npm run tsd", - "ci": " npm run lint && npm run cov" - }, "repository": { "type": "git", "url": "git+https://github.com/eggjs/egg-tracer.git" @@ -54,5 +31,68 @@ }, "homepage": "https://github.com/eggjs/egg-tracer#readme", "author": "fengmk2 (https://github.com/fengmk2)", - "license": "MIT" + "license": "MIT", + "engines": { + "node": ">= 18.19.0" + }, + "dependencies": { + "@eggjs/core": "^6.2.13" + }, + "devDependencies": { + "@arethetypeswrong/cli": "^0.17.3", + "@eggjs/bin": "7", + "@eggjs/mock": "^6.0.5", + "@eggjs/tsconfig": "1", + "@types/mocha": "10", + "@types/node": "22", + "egg": "^4.0.5", + "egg-logger": "^3.6.1", + "eslint": "8", + "eslint-config-egg": "14", + "glob": "^11.0.1", + "rimraf": "6", + "snap-shot-it": "^7.9.10", + "supertest": "^6.3.3", + "tsd": "^0.31.2", + "tshy": "3", + "tshy-after": "1", + "typescript": "5" + }, + "scripts": { + "lint": "eslint --cache src test --ext .ts", + "pretest": "npm run clean && npm run lint -- --fix", + "test": "egg-bin test", + "preci": "npm run clean && npm run lint", + "ci": "egg-bin cov", + "postci": "npm run prepublishOnly && npm run clean", + "clean": "rimraf dist", + "prepublishOnly": "tshy && tshy-after && attw --pack" + }, + "type": "module", + "tshy": { + "exports": { + ".": "./src/index.ts", + "./package.json": "./package.json" + } + }, + "exports": { + ".": { + "import": { + "types": "./dist/esm/index.d.ts", + "default": "./dist/esm/index.js" + }, + "require": { + "types": "./dist/commonjs/index.d.ts", + "default": "./dist/commonjs/index.js" + } + }, + "./package.json": "./package.json" + }, + "files": [ + "dist", + "src" + ], + "types": "./dist/commonjs/index.d.ts", + "main": "./dist/commonjs/index.js", + "module": "./dist/esm/index.js" } diff --git a/src/agent.ts b/src/agent.ts new file mode 100644 index 0000000..be21f8a --- /dev/null +++ b/src/agent.ts @@ -0,0 +1,3 @@ +import { TracerBoot } from './boot.js'; + +export default TracerBoot; diff --git a/src/app.ts b/src/app.ts new file mode 100644 index 0000000..be21f8a --- /dev/null +++ b/src/app.ts @@ -0,0 +1,3 @@ +import { TracerBoot } from './boot.js'; + +export default TracerBoot; diff --git a/src/app/extend/agent.ts b/src/app/extend/agent.ts new file mode 100644 index 0000000..d60215f --- /dev/null +++ b/src/app/extend/agent.ts @@ -0,0 +1,3 @@ +import TracerApplication from './application.js'; + +export default TracerApplication; diff --git a/src/app/extend/application.ts b/src/app/extend/application.ts new file mode 100644 index 0000000..979533e --- /dev/null +++ b/src/app/extend/application.ts @@ -0,0 +1,27 @@ +import { EggCore } from '@eggjs/core'; +import type { Tracer } from '../../lib/tracer.js'; + +const cacheTracer = Symbol('before_ready_tracer'); +export const isReady = Symbol('egg_tracer_is_ready'); + +export default class TracerApplication extends EggCore { + [cacheTracer]: Tracer | undefined; + + get tracer(): Tracer { + if (this[isReady]) { + return new this.config.tracer.Class(this.createAnonymousContext()); + } + + if (!this[cacheTracer]) { + this[cacheTracer] = new this.config.tracer.Class(this.createAnonymousContext()); + } + + return this[cacheTracer]; + } +} + +declare module '@eggjs/core' { + interface EggCore { + tracer: Tracer; + } +} diff --git a/src/app/extend/context.ts b/src/app/extend/context.ts new file mode 100644 index 0000000..a62b565 --- /dev/null +++ b/src/app/extend/context.ts @@ -0,0 +1,25 @@ +import { Context } from '@eggjs/core'; +import type { Tracer } from '../../lib/tracer.js'; +const TRACER = Symbol('context tracer'); + +export default class TracerContext extends Context { + [TRACER]: Tracer | undefined; + + get tracer(): Tracer { + if (!this[TRACER]) { + this[TRACER] = new this.app.config.tracer.Class(this); + } + return this[TRACER]; + } + + get traceId(): string { + return this.tracer.traceId; + } +} + +declare module '@eggjs/core' { + interface Context { + tracer: Tracer; + traceId: string; + } +} diff --git a/src/boot.ts b/src/boot.ts new file mode 100644 index 0000000..c2d14d0 --- /dev/null +++ b/src/boot.ts @@ -0,0 +1,10 @@ +import type { EggCore, ILifecycleBoot } from '@eggjs/core'; +import { isReady } from './app/extend/application.js'; + +export class TracerBoot implements ILifecycleBoot { + constructor(private app: EggCore) {} + + async didLoad() { + this.app[isReady] = true; + } +} diff --git a/src/config/config.default.ts b/src/config/config.default.ts new file mode 100644 index 0000000..0373def --- /dev/null +++ b/src/config/config.default.ts @@ -0,0 +1,22 @@ +import { Tracer } from '../lib/tracer.js'; + +/** + * tracer config + * @member Config#tracer + * @property {Tracer} Class - tracer class name + */ +interface TracerConfig { + Class: typeof Tracer; +} + +export default { + tracer: { + Class: Tracer, + } as TracerConfig, +}; + +declare module '@eggjs/core' { + interface EggAppConfig { + tracer: TracerConfig; + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..91d73d7 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,8 @@ +import './config/config.default.js'; +import './app/extend/application.js'; +import './app/extend/agent.js'; +import './app/extend/context.js'; + +import { Tracer } from './lib/tracer.js'; + +export { Tracer }; diff --git a/src/lib/tracer.ts b/src/lib/tracer.ts new file mode 100644 index 0000000..3ac9191 --- /dev/null +++ b/src/lib/tracer.ts @@ -0,0 +1,18 @@ +import { randomUUID } from 'node:crypto'; +import type { Context } from '@eggjs/core'; + +export class Tracer { + readonly ctx: Context; + #traceId: string | undefined; + + constructor(ctx: Context) { + this.ctx = ctx; + } + + get traceId() { + if (!this.#traceId) { + this.#traceId = randomUUID(); + } + return this.#traceId; + } +} diff --git a/src/typings/index.d.ts b/src/typings/index.d.ts new file mode 100644 index 0000000..53c65c7 --- /dev/null +++ b/src/typings/index.d.ts @@ -0,0 +1,4 @@ +// make sure to import egg typings and let typescript know about it +// @see https://github.com/whxaxes/blog/issues/11 +// and https://www.typescriptlang.org/docs/handbook/declaration-merging.html +import 'egg'; diff --git a/test/error.tracer.test.js b/test/error.tracer.test.js deleted file mode 100644 index 5b99066..0000000 --- a/test/error.tracer.test.js +++ /dev/null @@ -1,32 +0,0 @@ -const mm = require('egg-mock'); -const assert = require('assert'); - -describe('test/error.tracer.test.js', () => { - let app; - before(() => { - app = mm.app({ - baseDir: 'apps/error-tracer-test', - }); - return app.ready(); - }); - - after(() => app.close()); - - afterEach(mm.restore); - - it('should get app, agent tracer is null', () => { - assert(app.appBeforeReadyTracers.length === 0); - assert(app.agent.agentBeforeReadyTracers.length === 0); - - assert(app.appAfterReadyTracers.length === 0); - assert(app.agent.agentAfterReadyTracers.length === 0); - }); - - it('should GET /', () => { - return app.httpRequest() - .get('/') - .expect('x-trace-id', /\w{13}/) - .expect('hi, egg') - .expect(200); - }); -}); diff --git a/test/error.tracer.test.ts b/test/error.tracer.test.ts new file mode 100644 index 0000000..d69bbfd --- /dev/null +++ b/test/error.tracer.test.ts @@ -0,0 +1,32 @@ +import { strict as assert } from 'node:assert'; +import { mm, MockApplication } from '@eggjs/mock'; + +describe('test/error.tracer.test.ts', () => { + let app: MockApplication; + before(async () => { + app = mm.app({ + baseDir: 'apps/error-tracer-test', + }); + await app.ready(); + }); + + after(() => app.close()); + + afterEach(mm.restore); + + it('should get app, agent tracer', () => { + assert.equal(app.appBeforeReadyTracers.length, 3); + assert.equal(app.agent.agentBeforeReadyTracers.length, 3); + + assert.equal(app.appAfterReadyTracers.length, 3); + assert.equal(app.agent.agentAfterReadyTracers.length, 3); + }); + + it('should GET /', () => { + return app.httpRequest() + .get('/') + .expect('x-trace-id', /\w{13}/) + .expect('hi, egg') + .expect(200); + }); +}); diff --git a/test/fixtures/apps/error-tracer-test/agent.js b/test/fixtures/apps/error-tracer-test/agent.js index 38bd453..0d84cc3 100644 --- a/test/fixtures/apps/error-tracer-test/agent.js +++ b/test/fixtures/apps/error-tracer-test/agent.js @@ -9,7 +9,6 @@ module.exports = agent => { agentBeforeReadyTracers.push(agent.tracer); agentBeforeReadyTracers.push(agent.tracer); agentBeforeReadyTracers.push(agent.tracer); - } catch (e) { agent.coreLogger.warn(e); } diff --git a/test/index.test-d.ts b/test/index.test-d.ts new file mode 100644 index 0000000..637285b --- /dev/null +++ b/test/index.test-d.ts @@ -0,0 +1,16 @@ +import { expectType } from 'tsd'; +import { Tracer } from '../src/index.js'; +import { Context, Application, Agent } from 'egg'; + +const ctx = {} as Context; +const app = {} as Application; +const agent = {} as Agent; + +expectType(ctx.traceId); +expectType(ctx.tracer.traceId); +expectType(ctx.tracer); + +expectType(app.tracer.traceId); +expectType(agent.tracer.traceId); +expectType(app.tracer); +expectType(agent.tracer); diff --git a/test/index.test.js b/test/index.test.js deleted file mode 100644 index 8c6d8c4..0000000 --- a/test/index.test.js +++ /dev/null @@ -1,8 +0,0 @@ -const assert = require('assert'); -const Tracer = require('..'); - -describe('index.test.js', () => { - it('should work with lib', () => { - assert(typeof Tracer === 'function'); - }); -}); diff --git a/test/index.test.ts b/test/index.test.ts new file mode 100644 index 0000000..22bf085 --- /dev/null +++ b/test/index.test.ts @@ -0,0 +1,8 @@ +import { strict as assert } from 'node:assert'; +import { Tracer } from '../src/index.js'; + +describe('test/index.test.ts', () => { + it('should work with lib', () => { + assert.equal(typeof Tracer, 'function'); + }); +}); diff --git a/test/plugin.test.js b/test/plugin.test.ts similarity index 62% rename from test/plugin.test.js rename to test/plugin.test.ts index c91d4ef..3b04503 100644 --- a/test/plugin.test.js +++ b/test/plugin.test.ts @@ -1,7 +1,7 @@ -const mm = require('egg-mock'); +import { mm, MockApplication } from '@eggjs/mock'; -describe('test/plugin.test.js', () => { - let app; +describe('test/plugin.test.ts', () => { + let app: MockApplication; before(async () => { app = mm.app({ baseDir: 'apps/plugin-test', @@ -16,7 +16,7 @@ describe('test/plugin.test.js', () => { it('should GET /', () => { return app.httpRequest() .get('/') - .expect('x-trace-id', /\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/) + .expect('x-trace-id', /^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/) .expect('hi, egg') .expect(200); }); diff --git a/test/tracer.test.js b/test/tracer.test.js deleted file mode 100644 index 803b926..0000000 --- a/test/tracer.test.js +++ /dev/null @@ -1,83 +0,0 @@ -const mm = require('egg-mock'); -const assert = require('assert'); - -describe('test/tracer.test.js', () => { - let app; - before(() => { - app = mm.app({ - baseDir: 'apps/plugin-test', - }); - return app.ready(); - }); - - after(() => app.close()); - - afterEach(mm.restore); - - it('should get app, agent tracer', () => { - assert(app[Symbol.for('egg_tracer_is_ready')]); - assert(app.agent[Symbol.for('egg_tracer_is_ready')]); - - let [ appTracer_1, appTracer_2, appTracer_3 ] = app.appBeforeReadyTracers; - let [ agentTracer_1, agentTracer_2, agentTracer_3 ] = app.agent.agentBeforeReadyTracers; - - assert(appTracer_1 === appTracer_2); - assert(appTracer_1 === appTracer_3); - - assert(appTracer_1.traceId); - assert(appTracer_2.traceId); - assert(appTracer_3.traceId); - - assert(appTracer_1 === appTracer_2); - assert(appTracer_1 === appTracer_3); - - assert(appTracer_1.traceId === appTracer_2.traceId); - assert(appTracer_1.traceId === appTracer_3.traceId); - - assert(agentTracer_1.traceId); - assert(agentTracer_2.traceId); - assert(agentTracer_3.traceId); - - assert(agentTracer_1 === agentTracer_2); - assert(agentTracer_1 === agentTracer_3); - - assert(agentTracer_1.traceId); - assert(agentTracer_2.traceId); - assert(agentTracer_3.traceId); - - assert(agentTracer_1.traceId === agentTracer_2.traceId); - assert(agentTracer_1.traceId === agentTracer_3.traceId); - - // app ready - [ appTracer_1, appTracer_2, appTracer_3 ] = app.appAfterReadyTracers; - - assert(appTracer_1 !== appTracer_2); - assert(appTracer_1 !== appTracer_3); - - assert(appTracer_1.traceId); - assert(appTracer_2.traceId); - assert(appTracer_3.traceId); - - assert(appTracer_1 !== appTracer_2); - assert(appTracer_1 !== appTracer_3); - - assert(appTracer_1.traceId !== appTracer_2.traceId); - assert(appTracer_1.traceId !== appTracer_3.traceId); - - // agent ready - [ agentTracer_1, agentTracer_2, agentTracer_3 ] = app.agent.agentAfterReadyTracers; - assert(agentTracer_1.traceId); - assert(agentTracer_2.traceId); - assert(agentTracer_3.traceId); - - assert(agentTracer_1 !== agentTracer_2); - assert(agentTracer_1 !== agentTracer_3); - - assert(agentTracer_1.traceId); - assert(agentTracer_2.traceId); - assert(agentTracer_3.traceId); - - assert(agentTracer_1.traceId !== agentTracer_2.traceId); - assert(agentTracer_1.traceId !== agentTracer_3.traceId); - }); -}); diff --git a/test/tracer.test.ts b/test/tracer.test.ts new file mode 100644 index 0000000..888dab7 --- /dev/null +++ b/test/tracer.test.ts @@ -0,0 +1,84 @@ +import { strict as assert } from 'node:assert'; +import { mm, MockApplication } from '@eggjs/mock'; +import { isReady } from '../src/app/extend/application.js'; + +describe('test/tracer.test.ts', () => { + let app: MockApplication; + before(() => { + app = mm.app({ + baseDir: 'apps/plugin-test', + }); + return app.ready(); + }); + + after(() => app.close()); + + afterEach(mm.restore); + + it('should get app, agent tracer', () => { + assert(app[isReady]); + assert(app.agent[isReady]); + + let [ appTracer_1, appTracer_2, appTracer_3 ] = app.appBeforeReadyTracers; + let [ agentTracer_1, agentTracer_2, agentTracer_3 ] = app.agent.agentBeforeReadyTracers; + + assert.equal(appTracer_1, appTracer_2); + assert.equal(appTracer_1, appTracer_3); + + assert(appTracer_1.traceId); + assert(appTracer_2.traceId); + assert(appTracer_3.traceId); + + assert.equal(appTracer_1, appTracer_2); + assert.equal(appTracer_1, appTracer_3); + + assert.equal(appTracer_1.traceId, appTracer_2.traceId); + assert.equal(appTracer_1.traceId, appTracer_3.traceId); + + assert(agentTracer_1.traceId); + assert(agentTracer_2.traceId); + assert(agentTracer_3.traceId); + + assert.equal(agentTracer_1, agentTracer_2); + assert.equal(agentTracer_1, agentTracer_3); + + assert(agentTracer_1.traceId); + assert(agentTracer_2.traceId); + assert(agentTracer_3.traceId); + + assert.equal(agentTracer_1.traceId, agentTracer_2.traceId); + assert.equal(agentTracer_1.traceId, agentTracer_3.traceId); + + // app ready + [ appTracer_1, appTracer_2, appTracer_3 ] = app.appAfterReadyTracers; + + assert.notEqual(appTracer_1, appTracer_2); + assert.notEqual(appTracer_1, appTracer_3); + + assert(appTracer_1.traceId); + assert(appTracer_2.traceId); + assert(appTracer_3.traceId); + + assert.notEqual(appTracer_1, appTracer_2); + assert.notEqual(appTracer_1, appTracer_3); + + assert.notEqual(appTracer_1.traceId, appTracer_2.traceId); + assert.notEqual(appTracer_1.traceId, appTracer_3.traceId); + + // agent ready + [ agentTracer_1, agentTracer_2, agentTracer_3 ] = app.agent.agentAfterReadyTracers; + assert(agentTracer_1.traceId); + assert(agentTracer_2.traceId); + assert(agentTracer_3.traceId); + + assert.notEqual(agentTracer_1, agentTracer_2); + assert.notEqual(agentTracer_1, agentTracer_3); + + assert(agentTracer_1.traceId); + assert(agentTracer_2.traceId); + assert(agentTracer_3.traceId); + + assert.notEqual(agentTracer_1.traceId, agentTracer_2.traceId); + assert.notEqual(agentTracer_1.traceId, agentTracer_3.traceId); + }); +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..ff41b73 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "@eggjs/tsconfig", + "compilerOptions": { + "strict": true, + "noImplicitAny": true, + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext" + } +}