Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 28, 2024
1 parent 1de4e4f commit 14871dd
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 15 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.1",
"@eggjs/bin": "^7.0.0",
"@eggjs/tegg": "^3.2.2",
"@eggjs/tegg-config": "^3.2.2",
"@eggjs/tegg-controller-plugin": "^3.2.2",
Expand All @@ -75,7 +76,7 @@
"clean": "rimraf dist",
"lint": "eslint --cache src test --ext .ts",
"pretest": "npm run clean && npm run lint -- --fix && npm run prepublishOnly",
"test": "egg-bin test -p",
"test": "egg-bin test",
"posttest": "npm run clean",
"test-local": "egg-bin test",
"preci": "npm run clean && npm run lint && npm run prepublishOnly",
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/tegg-app-esm/app.js
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;
});
};
17 changes: 17 additions & 0 deletions test/fixtures/tegg-app-esm/app/modules/foo/LogService.ts
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;
}
}
6 changes: 6 additions & 0 deletions test/fixtures/tegg-app-esm/app/modules/foo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "foo",
"eggModule": {
"name": "foo"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/tegg-app-esm/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
keys: '123',
};
18 changes: 18 additions & 0 deletions test/fixtures/tegg-app-esm/config/plugin.js
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,
},
};
7 changes: 7 additions & 0 deletions test/fixtures/tegg-app-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "app",
"egg": {
"typescript": true
},
"type": "module"
}
70 changes: 70 additions & 0 deletions test/fixtures/tegg-app-esm/test/hooks.test.ts
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 test/fixtures/tegg-app-esm/test/multi_mock_context.test.ts
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);
});
});
});
23 changes: 23 additions & 0 deletions test/fixtures/tegg-app-esm/test/tegg.test.ts
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();
});
});
});
});
56 changes: 56 additions & 0 deletions test/fixtures/tegg-app-esm/test/tegg_context.test.ts
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');
});
});
});
3 changes: 3 additions & 0 deletions test/fixtures/tegg-app-esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@eggjs/tsconfig"
}
2 changes: 2 additions & 0 deletions test/fixtures/tegg-app-esm/typing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'egg';
import '@eggjs/tegg-plugin';
29 changes: 15 additions & 14 deletions test/inject_ctx.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { strict as assert } from 'node:assert';
// import { strict as assert } from 'node:assert';
import path from 'node:path';
import coffee from 'coffee';
import { importResolve } from '@eggjs/utils';
import { getFixtures } from './helper.js';

describe('test/inject_ctx.test.ts', () => {
const eggBinFile = importResolve('egg-bin/dist/bin/cli');
describe.skip('test/inject_ctx.test.ts', () => {
const eggBinFile = path.join(importResolve('@eggjs/bin/package.json'), '../bin/run.js');

it('should export register', () => {
assert.equal(importResolve('./dist/commonjs/register.js'), getFixtures('../../dist/commonjs/register.js'));
assert.equal(importResolve('./dist/commonjs/register'), getFixtures('../../dist/commonjs/register.js'));
assert.equal(importResolve('./dist/esm/register'), getFixtures('../../dist/esm/register.js'));
assert.equal(importResolve('./dist/esm/register.js'), getFixtures('../../dist/esm/register.js'));
});
// it('should export register', () => {
// assert.equal(importResolve('./dist/commonjs/register.js'), getFixtures('../../dist/commonjs/register.js'));
// assert.equal(importResolve('./dist/commonjs/register'), getFixtures('../../dist/commonjs/register.js'));
// assert.equal(importResolve('./dist/esm/register'), getFixtures('../../dist/esm/register.js'));
// assert.equal(importResolve('./dist/esm/register.js'), getFixtures('../../dist/esm/register.js'));
// });

it('should inject ctx to runner with commonjs', async () => {
it.skip('should inject ctx to runner with commonjs', async () => {
const fixture = getFixtures('tegg-app');

await coffee.fork(eggBinFile, [
'test',
'-r', importResolve('./dist/commonjs/register.js'),
'-r', getFixtures('../../dist/commonjs/register.js'),
], {
cwd: fixture,
env: {
Expand All @@ -31,13 +32,13 @@ describe('test/inject_ctx.test.ts', () => {
.end();
});

it.skip('should inject ctx to runner with esm', async () => {
const fixture = getFixtures('tegg-app');
it('should inject ctx to runner with esm', async () => {
const fixture = getFixtures('tegg-app-esm');

await coffee.fork(eggBinFile, [
'test',
'test/hooks.test.ts',
'-r', importResolve('./dist/esm/register.js'),
'-r', getFixtures('../../dist/esm/register.js'),
], {
cwd: fixture,
env: {
Expand Down

0 comments on commit 14871dd

Please sign in to comment.