Skip to content

Commit

Permalink
fix: fix mockContext with headers (#153)
Browse files Browse the repository at this point in the history
mock headers when reuse context.
  • Loading branch information
killagu committed Jan 29, 2023
1 parent 0e8dba5 commit 5e3e816
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 11 deletions.
10 changes: 9 additions & 1 deletion app/extend/application.js
Expand Up @@ -37,6 +37,13 @@ module.exports = {
* ```
*/
mockContext(data, options) {
function mockRequest(req) {
for (const key in (data.headers) || {}) {
mm(req.headers, key, data.headers[key]);
mm(req.headers, key.toLowerCase(), data.headers[key]);
}
}

options = Object.assign({ mockCtxStorage: true }, options);
data = data || {};

Expand All @@ -54,6 +61,7 @@ module.exports = {

if (options.reuseCtxStorage !== false) {
if (this.currentContext && !this.currentContext[REUSED_CTX]) {
mockRequest(this.currentContext.request.req);
this.currentContext[REUSED_CTX] = true;
return this.currentContext;
}
Expand All @@ -70,7 +78,7 @@ module.exports = {
mockCtxStorage: false,
reuseCtxStorage: false,
});
await this.ctxStorage.run(ctx, fn, ctx);
return await this.ctxStorage.run(ctx, fn, ctx);
},

/**
Expand Down
18 changes: 11 additions & 7 deletions lib/inject_context.js
Expand Up @@ -70,14 +70,15 @@ function injectContext(mocha) {
const app = await appHandler.getApp(suite);
await app.ready();
const mockContextFun = app.mockModuleContextScope || app.mockContextScope;
let testPassed = false;
try {
await mockContextFun.call(app, async function() {
await new Promise((resolve, reject) => {
runTests.call(self, suite, errSuite => {
if (errSuite) {
return reject(errSuite);
testPassed = await mockContextFun.call(app, async function() {
return await new Promise(resolve => {
runTests.call(self, suite, err => {
if (err) {
return resolve(false);
}
return resolve();
return resolve(true);
});
});
});
Expand All @@ -86,7 +87,10 @@ function injectContext(mocha) {
console.error(err);
return done(suite);
}
return next(i + 1);
if (testPassed) {
return next(i + 1);
}
return done(suite);
}
next(0).catch(e => {
e.message = '[egg-mock] inject context error: ' + e.message;
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/failed-app/config/config.default.js
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
keys: '123',
};
3 changes: 3 additions & 0 deletions test/fixtures/failed-app/package.json
@@ -0,0 +1,3 @@
{
"name": "app"
}
32 changes: 32 additions & 0 deletions test/fixtures/failed-app/test/index.test.js
@@ -0,0 +1,32 @@
const assert = require('assert');
const { app } = require('../../../../bootstrap');

describe('test/index.test.ts', () => {
describe('hook error', () => {
beforeEach(() => {
assert.fail('failed hook');
});

it('should fail', () => {
// eslint-disable-next-line no-undef
assert(app.currentContext);
assert.fail('failed case');
});
});

describe('mockContext failed', () => {
before(() => {
app.mockContextScope = () => {
throw new Error('mockContextScope error');
};
});

it('foo', () => {
//...
});

it('should not run', () => {
console.log('it should not run');
});
});
});
18 changes: 18 additions & 0 deletions test/fixtures/tegg-app/test/tegg_context.test.ts
Expand Up @@ -30,13 +30,31 @@ describe('test/tegg_context.test.ts', () => {
tracer: {
traceId: 'mock_with_data',
},
headers: {
'user-agent': 'mock_agent',
},
});
assert(ctx.tracer.traceId === 'mock_with_data');
assert(ctx.get('user-agent') === 'mock_agent');
});

it('should mock ctx work', () => {
const traceId = logService.getTracerId();
assert(traceId === 'mock_with_data');
});
});

describe('mockModuleContextWithHeaders', () => {
beforeEach(async () => {
await app.mockModuleContext({
headers: {
'user-agent': 'mock_agent',
},
});
});

it('should mock ctx work', () => {
assert(app.currentContext.get('user-agent') === 'mock_agent');
});
});
});
25 changes: 22 additions & 3 deletions test/inject_ctx.test.js
Expand Up @@ -15,9 +15,9 @@ describe('test/inject_ctx.test.js', () => {
EGG_FRAMEWORK: require.resolve('egg'),
},
})
.debug()
// .debug()
.expect('code', 0)
.expect('stdout', /9 passing/)
.expect('stdout', /10 passing/)
.end();
});

Expand All @@ -31,9 +31,28 @@ describe('test/inject_ctx.test.js', () => {
], {
cwd: fixture,
})
.debug()
// .debug()
.expect('code', 0)
// .expect('stdout', /9 passing/)
.end();
});

it('failed case', async () => {
const fixture = path.join(__dirname, 'fixtures/failed-app');

await coffee.fork(require.resolve('egg-bin/bin/egg-bin'), [
'test',
'-r', require.resolve('../register'),
'--full-trace',
], {
cwd: fixture,
env: {
EGG_FRAMEWORK: require.resolve('egg'),
},
})
// .debug()
.expect('code', 1)
.expect('stderr', /run mock context error: mockContextScope error/)
.end();
});
});

0 comments on commit 5e3e816

Please sign in to comment.