Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix unstable test #133

Merged
merged 1 commit into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: node_js
node_js:
- '4'
- '6'
- '7'
install:
- npm i npminstall && npminstall
script:
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '7'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"estraverse": "^4.1.1",
"formstream": "^1.0.0",
"glob": "^7.1.1",
"ko-sleep": "^1.0.2",
"koa": "^1.2.4",
"koa-router": "^5.4.0",
"merge-descriptors": "^1.0.1",
Expand Down Expand Up @@ -117,6 +118,6 @@
"node": ">= 4.0.0"
},
"ci": {
"version": "4, 6"
"version": "4, 6, 7"
}
}
2 changes: 1 addition & 1 deletion test/app/extend/agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('test/app/extend/agent.test.js', () => {
let app;
afterEach(() => app.close());

it.skip('should not log in unittest env', function* () {
it('should not log in unittest env', function* () {
mm.env('unittest');
app = utils.app('apps/agent-instrument');
yield app.ready();
Expand Down
152 changes: 68 additions & 84 deletions test/app/extend/context.test.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,102 @@
'use strict';

const should = require('should');
const fs = require('fs');
const path = require('path');
const mm = require('egg-mock');
const request = require('supertest');
const rimraf = require('rimraf');
const utils = require('../../utils');

describe('test/app/extend/context.test.js', () => {

afterEach(mm.restore);

describe('ctx.logger', () => {
const baseDir = utils.getFilepath('apps/demo');

beforeEach(() => {
rimraf.sync(path.join(baseDir, 'logs'));
});

let app;
afterEach(() => app.close());

it('env=local: level => debug', done => {
it('env=local: level => debug', function* () {
mm.env('local');
mm(process.env, 'EGG_LOG', 'none');
app = utils.app('apps/demo');
yield app.ready();
const logdir = app.config.logger.dir;

request(app.callback())
yield request(app.callback())
.get('/logger?message=foo')
.expect('logger', err => {
should.not.exists(err);

const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
errorContent.should.containEql('nodejs.Error: error foo');
errorContent.should.containEql('nodejs.Error: core error foo');

const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8');
loggerContent.should.containEql('debug foo');
loggerContent.should.containEql('info foo');
loggerContent.should.containEql('warn foo');

const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8');
coreLoggerContent.should.containEql('core debug foo');
coreLoggerContent.should.containEql('core info foo');
coreLoggerContent.should.containEql('core warn foo');
done();
});
});

it('env=unittest: level => info', done => {
mm.env('unittest');
app = utils.app('apps/demo');
app.ready(() => {
const logdir = app.config.logger.dir;

app.mockContext({
userId: '123123',
tracer: {
traceId: '456456',
},
});
.expect('logger');

request(app.callback())
.get('/logger?message=foo')
.expect('logger', err => {
should.not.exists(err);
const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
errorContent.should.containEql('nodejs.Error: error foo');
errorContent.should.containEql('nodejs.Error: core error foo');

const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
errorContent.should.containEql('nodejs.Error: error foo');
errorContent.should.containEql('nodejs.Error: core error foo');
errorContent.should.match(/\[123123\/[\d\.]+\/456456\/\d+ms GET \/logger\?message=foo]/);
const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8');
loggerContent.should.containEql('debug foo');
loggerContent.should.containEql('info foo');
loggerContent.should.containEql('warn foo');

const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8');
loggerContent.should.not.containEql('debug foo');
loggerContent.should.containEql('info foo');
loggerContent.should.containEql('warn foo');
const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8');
coreLoggerContent.should.containEql('core debug foo');
coreLoggerContent.should.containEql('core info foo');
coreLoggerContent.should.containEql('core warn foo');
});

const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8');
coreLoggerContent.should.not.containEql('core debug foo');
coreLoggerContent.should.containEql('core info foo');
coreLoggerContent.should.containEql('core warn foo');
it('env=unittest: level => info', function* () {
mm.env('unittest');
app = utils.app('apps/demo');
yield app.ready();
const logdir = app.config.logger.dir;

done();
});
app.mockContext({
userId: '123123',
tracer: {
traceId: '456456',
},
});

yield request(app.callback())
.get('/logger?message=foo')
.expect('logger');

const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
errorContent.should.containEql('nodejs.Error: error foo');
errorContent.should.containEql('nodejs.Error: core error foo');
errorContent.should.match(/\[123123\/[\d\.]+\/456456\/\d+ms GET \/logger\?message=foo]/);

const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8');
loggerContent.should.not.containEql('debug foo');
loggerContent.should.containEql('info foo');
loggerContent.should.containEql('warn foo');

const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8');
coreLoggerContent.should.not.containEql('core debug foo');
coreLoggerContent.should.containEql('core info foo');
coreLoggerContent.should.containEql('core warn foo');
});

it('env=prod: level => info', done => {
it('env=prod: level => info', function* () {
mm.env('unittest');
app = utils.app('apps/demo');
yield app.ready();
const logdir = app.config.logger.dir;

request(app.callback())
yield request(app.callback())
.get('/logger?message=foo')
.expect('logger', err => {
should.not.exists(err);
.expect('logger');

const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
errorContent.should.containEql('nodejs.Error: error foo');
errorContent.should.containEql('nodejs.Error: core error foo');
const errorContent = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
errorContent.should.containEql('nodejs.Error: error foo');
errorContent.should.containEql('nodejs.Error: core error foo');

const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8');
loggerContent.should.not.containEql('debug foo');
loggerContent.should.containEql('info foo');
loggerContent.should.containEql('warn foo');
const loggerContent = fs.readFileSync(path.join(logdir, 'demo-web.log'), 'utf8');
loggerContent.should.not.containEql('debug foo');
loggerContent.should.containEql('info foo');
loggerContent.should.containEql('warn foo');

const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8');
coreLoggerContent.should.not.containEql('core debug foo');
coreLoggerContent.should.containEql('core info foo');
coreLoggerContent.should.containEql('core warn foo');

done();
});
const coreLoggerContent = fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8');
coreLoggerContent.should.not.containEql('core debug foo');
coreLoggerContent.should.containEql('core info foo');
coreLoggerContent.should.containEql('core warn foo');
});
});

Expand Down Expand Up @@ -291,20 +274,21 @@ describe('test/app/extend/context.test.js', () => {
afterEach(mm.restore);

it('should curl ok', function* () {
this.timeout(10000);
const context = app.mockContext();
const res = yield context.curl('https://a.alipayobjects.com/aliBridge/1.0.0/aliBridge.min.js');
const res = yield context.curl('https://a.alipayobjects.com/aliBridge/1.0.0/aliBridge.min.js', {
timeout: 10000,
});
res.status.should.equal(200);
});
}).timeout(10000);
});

describe('ctx.realStatus', () => {
let app;
beforeEach(() => {
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
afterEach(() => app.close());
after(() => app.close());
afterEach(mm.restore);

it('should get from status ok', () => {
Expand All @@ -323,11 +307,11 @@ describe('test/app/extend/context.test.js', () => {

describe('ctx.state', () => {
let app;
beforeEach(() => {
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
afterEach(() => app.close());
after(() => app.close());
afterEach(mm.restore);

it('should delegate ctx.locals', () => {
Expand Down
Loading