-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
241 additions
and
15 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
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 @@ | ||
export default app => { | ||
app.on('server', server => { | ||
app.serverKeepAliveTimeout = server.keepAliveTimeout || 5000; | ||
}); | ||
}; |
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,17 @@ | ||
import { AccessLevel, Inject, SingletonProto } from '@eggjs/tegg'; | ||
|
||
interface Tracer { | ||
traceId: string; | ||
} | ||
|
||
@SingletonProto({ | ||
accessLevel: AccessLevel.PUBLIC, | ||
}) | ||
export class LogService { | ||
@Inject() | ||
private readonly tracer: Tracer; | ||
|
||
getTracerId() { | ||
return this.tracer.traceId; | ||
} | ||
} |
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,6 @@ | ||
{ | ||
"name": "foo", | ||
"eggModule": { | ||
"name": "foo" | ||
} | ||
} |
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 @@ | ||
export default { | ||
keys: '123', | ||
}; |
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,18 @@ | ||
export default { | ||
teggConfig: { | ||
package: '@eggjs/tegg-config', | ||
enable: true, | ||
}, | ||
tegg: { | ||
package: '@eggjs/tegg-plugin', | ||
enable: true, | ||
}, | ||
teggController: { | ||
package: '@eggjs/tegg-controller-plugin', | ||
enable: true, | ||
}, | ||
tracer: { | ||
package: 'egg-tracer', | ||
enable: true, | ||
}, | ||
}; |
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,7 @@ | ||
{ | ||
"name": "app", | ||
"egg": { | ||
"typescript": true | ||
}, | ||
"type": "module" | ||
} |
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,70 @@ | ||
import assert from 'node:assert'; | ||
import { ContextDelegation } from 'egg'; | ||
// import { app } from '../../../../src/bootstrap.js'; | ||
import { app } from '../../../../dist/esm/bootstrap.js'; | ||
|
||
describe('test/hooks.test.ts', () => { | ||
let beforeCtx; | ||
let afterCtx; | ||
const beforeEachCtxList: Record<string, ContextDelegation> = {}; | ||
const afterEachCtxList: Record<string, ContextDelegation> = {}; | ||
const itCtxList: Record<string, ContextDelegation> = {}; | ||
|
||
before(async () => { | ||
beforeCtx = app.currentContext; | ||
}); | ||
|
||
after(() => { | ||
afterCtx = app.currentContext; | ||
assert(beforeCtx); | ||
assert(beforeCtx !== itCtxList.foo); | ||
assert(itCtxList.foo !== itCtxList.bar); | ||
assert(afterCtx === beforeCtx); | ||
assert(beforeEachCtxList.foo === afterEachCtxList.foo); | ||
assert(beforeEachCtxList.foo === itCtxList.foo); | ||
}); | ||
|
||
describe('foo', () => { | ||
beforeEach(() => { | ||
beforeEachCtxList.foo = app.currentContext as ContextDelegation; | ||
}); | ||
|
||
it('should work', () => { | ||
itCtxList.foo = app.currentContext as ContextDelegation; | ||
}); | ||
|
||
afterEach(() => { | ||
afterEachCtxList.foo = app.currentContext as ContextDelegation; | ||
}); | ||
}); | ||
|
||
describe('bar', () => { | ||
beforeEach(() => { | ||
beforeEachCtxList.bar = app.currentContext as ContextDelegation; | ||
}); | ||
|
||
it('should work', () => { | ||
itCtxList.bar = app.currentContext as ContextDelegation; | ||
}); | ||
|
||
afterEach(() => { | ||
afterEachCtxList.bar = app.currentContext as ContextDelegation; | ||
}); | ||
}); | ||
|
||
describe('multi it', () => { | ||
const itCtxList: Array<ContextDelegation> = []; | ||
|
||
it('should work 1', () => { | ||
itCtxList.push(app.currentContext as ContextDelegation); | ||
}); | ||
|
||
it('should work 2', () => { | ||
itCtxList.push(app.currentContext as ContextDelegation); | ||
}); | ||
|
||
after(() => { | ||
assert(itCtxList[0] !== itCtxList[1]); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
test/fixtures/tegg-app-esm/test/multi_mock_context.test.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,14 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import { app } from '../../../../dist/commonjs/bootstrap'; | ||
|
||
describe('test/multi_mock_context.test.ts', () => { | ||
describe('mockContext', () => { | ||
it('should only reused once', async () => { | ||
const currentContext = app.currentContext; | ||
const ctx1 = app.mockContext(); | ||
const ctx2 = app.mockContext(); | ||
assert.strictEqual(currentContext, ctx1); | ||
assert.notStrictEqual(ctx2, ctx1); | ||
}); | ||
}); | ||
}); |
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,23 @@ | ||
import { strict as assert } from 'node:assert'; | ||
// import { app } from '../../../../src/bootstrap.js'; | ||
import { app } from '../../../../dist/commonjs/bootstrap'; | ||
import { LogService } from '../app/modules/foo/LogService'; | ||
|
||
describe('test/tegg.test.ts', () => { | ||
describe('async function', () => { | ||
it('should work', async () => { | ||
const logService = await app.getEggObject(LogService); | ||
assert(logService.getTracerId()); | ||
}); | ||
}); | ||
|
||
describe('callback function', () => { | ||
it('should work', done => { | ||
app.mockModuleContextScope(async () => { | ||
const logService = await app.getEggObject(LogService); | ||
assert(logService.getTracerId()); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
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,56 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import { ContextDelegation } from 'egg'; | ||
// import { app, mm } from '../../../../src/bootstrap.js'; | ||
import { app, mm } from '../../../../dist/commonjs/bootstrap'; | ||
import { LogService } from '../app/modules/foo/LogService'; | ||
|
||
describe('test/tegg_context.test.ts', () => { | ||
let ctx: ContextDelegation; | ||
let logService: LogService; | ||
before(async () => { | ||
logService = await app.getEggObject(LogService); | ||
}); | ||
|
||
describe('mock ctx property', () => { | ||
it('should mock ctx work', async () => { | ||
ctx = await app.mockModuleContext(); | ||
mm(ctx.tracer, 'traceId', 'mockTraceId'); | ||
const traceId = logService.getTracerId(); | ||
assert.strictEqual(traceId, 'mockTraceId'); | ||
}); | ||
}); | ||
|
||
describe.skip('mockModuleContextWithData', () => { | ||
beforeEach(async () => { | ||
const ctx = await app.mockModuleContext({ | ||
tracer: { | ||
traceId: 'mock_with_data', | ||
}, | ||
headers: { | ||
'user-agent': 'mock_agent', | ||
}, | ||
}); | ||
assert.strictEqual(ctx.tracer.traceId, 'mock_with_data'); | ||
assert.strictEqual(ctx.get('user-agent'), 'mock_agent'); | ||
}); | ||
|
||
it('should mock ctx work', () => { | ||
const traceId = logService.getTracerId(); | ||
assert.strictEqual(traceId, 'mock_with_data'); | ||
}); | ||
}); | ||
|
||
describe.skip('mockModuleContextWithHeaders', () => { | ||
beforeEach(async () => { | ||
await app.mockModuleContext({ | ||
headers: { | ||
'user-agent': 'mock_agent', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should mock ctx work', () => { | ||
assert.strictEqual(app.currentContext!.get('user-agent'), 'mock_agent'); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
{ | ||
"extends": "@eggjs/tsconfig" | ||
} |
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,2 @@ | ||
import 'egg'; | ||
import '@eggjs/tegg-plugin'; |
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