Skip to content

Commit

Permalink
test: use assert instead of should (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and atian25 committed May 3, 2017
1 parent 89b4df9 commit 1a027ad
Show file tree
Hide file tree
Showing 23 changed files with 294 additions and 253 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"rds": "^0.1.0",
"rimraf": "^2.6.1",
"runscript": "^1.2.1",
"should": "^11.2.1",
"spy": "^1.0.0",
"supertest": "^3.0.0",
"taffydb": "^2.7.3",
Expand Down
8 changes: 5 additions & 3 deletions test/app/extend/agent.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const assert = require('assert');

const mm = require('egg-mock');
const utils = require('../../utils');

Expand All @@ -16,12 +18,12 @@ describe('test/app/extend/agent.test.js', () => {

it('should add singleton success', function* () {
let config = yield app.agent.dataService.get('second').getConfig();
config.foo.should.equal('bar');
config.foo2.should.equal('bar2');
assert(config.foo === 'bar');
assert(config.foo2 === 'bar2');

const ds = app.agent.dataService.createInstance({ foo: 'barrr' });
config = yield ds.getConfig();
config.foo.should.equal('barrr');
assert(config.foo === 'barrr');
});
});

Expand Down
37 changes: 20 additions & 17 deletions test/app/extend/application.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const assert = require('assert');

const request = require('supertest');
const sleep = require('mz-modules/sleep');
const fs = require('fs');
Expand All @@ -16,19 +18,19 @@ describe('test/app/extend/application.test.js', () => {
after(() => app.close());

it('should alias app.logger => app.loggers.logger', () => {
app.logger.should.equal(app.loggers.logger);
assert(app.logger === app.loggers.logger);
});

it('should alias app.coreLooger => app.loggers.coreLooger', () => {
app.coreLogger.should.equal(app.loggers.coreLogger);
assert(app.coreLogger === app.loggers.coreLogger);
});

it('should alias app.getLogger(\'coreLogger\') => app.loggers.coreLooger', () => {
app.getLogger('coreLogger').should.equal(app.loggers.coreLogger);
assert(app.getLogger('coreLogger') === app.loggers.coreLogger);
});

it('should alias app.getLogger(\'noexist\') => null', () => {
(app.getLogger('noexist') === null).should.be.true();
assert(app.getLogger('noexist') === null);
});
});

Expand All @@ -41,13 +43,13 @@ describe('test/app/extend/application.test.js', () => {
after(() => app.close());

it('should inspect app properties', () => {
app.inspect().should.have.properties([
assert([
'name', 'baseDir',
'env', 'subdomainOffset',
'controller', 'middlewares', 'serviceClasses',
'config', 'httpclient', 'loggers',
]);
app.inspect().name.should.equal('demo');
].every(p => Object.prototype.hasOwnProperty.call(app.inspect(), p)));
assert(app.inspect().name === 'demo');
});
});

Expand Down Expand Up @@ -97,10 +99,10 @@ describe('test/app/extend/application.test.js', () => {
},
url: '/foobar?ok=1',
});
ctx.ip.should.equal('10.0.0.1');
ctx.url.should.equal('/foobar?ok=1');
ctx.socket.remoteAddress.should.equal('10.0.0.1');
ctx.socket.remotePort.should.equal(7001);
assert(ctx.ip === '10.0.0.1');
assert(ctx.url === '/foobar?ok=1');
assert(ctx.socket.remoteAddress === '10.0.0.1');
assert(ctx.socket.remotePort === 7001);
});
});

Expand All @@ -114,12 +116,12 @@ describe('test/app/extend/application.test.js', () => {

it('should add singleton success', function* () {
let config = yield app.dataService.get('first').getConfig();
config.foo.should.equal('bar');
config.foo1.should.equal('bar1');
assert(config.foo === 'bar');
assert(config.foo1 === 'bar1');

const ds = app.dataService.createInstance({ foo: 'barrr' });
config = yield ds.getConfig();
config.foo.should.equal('barrr');
assert(config.foo === 'barrr');
});
});

Expand All @@ -139,9 +141,10 @@ describe('test/app/extend/application.test.js', () => {
yield sleep(5000);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
log.should.match(/mock background run at app result file size: \d+/);
fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8')
.should.match(/\[egg:background] task:saveUserInfo success \(\d+ms\)/);
assert(/mock background run at app result file size: \d+/.test(log));
assert(
/\[egg:background] task:saveUserInfo success \(\d+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
);
});
});
});
72 changes: 38 additions & 34 deletions test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ describe('test/app/extend/context.test.js', () => {
yield sleep(5000);

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');
assert(errorContent.includes('nodejs.Error: error foo'));
assert(errorContent.includes('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');
assert(loggerContent.includes('info foo'));
assert(loggerContent.includes('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');
assert(coreLoggerContent.includes('core info foo'));
assert(coreLoggerContent.includes('core warn foo'));
});

it('env=unittest: level => info', function* () {
Expand All @@ -61,19 +61,19 @@ describe('test/app/extend/context.test.js', () => {
yield sleep(5000);

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.]+\/-\/\d+ms GET \/logger\?message=foo]/);
assert(errorContent.includes('nodejs.Error: error foo'));
assert(errorContent.includes('nodejs.Error: core error foo'));
assert(/\[123123\/[\d.]+\/-\/\d+ms GET \/logger\?message=foo]/.test(errorContent));

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');
assert(!loggerContent.includes('debug foo'));
assert(loggerContent.includes('info foo'));
assert(loggerContent.includes('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');
assert(!coreLoggerContent.includes('core debug foo'));
assert(coreLoggerContent.includes('core info foo'));
assert(coreLoggerContent.includes('core warn foo'));
});

it('env=prod: level => info', function* () {
Expand All @@ -90,18 +90,18 @@ describe('test/app/extend/context.test.js', () => {
yield sleep(5000);

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');
assert(errorContent.includes('nodejs.Error: error foo'));
assert(errorContent.includes('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');
assert(!loggerContent.includes('debug foo'));
assert(loggerContent.includes('info foo'));
assert(loggerContent.includes('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');
assert(!coreLoggerContent.includes('core debug foo'));
assert(coreLoggerContent.includes('core info foo'));
assert(coreLoggerContent.includes('core warn foo'));
});
});

Expand All @@ -126,7 +126,9 @@ describe('test/app/extend/context.test.js', () => {

yield sleep(100);
const logPath = utils.getFilepath('apps/get-logger/logs/get-logger/a.log');
fs.readFileSync(logPath, 'utf8').should.match(/\[-\/127.0.0.1\/-\/\d+ms GET \/logger] aaa/);
assert(
/\[-\/127.0.0.1\/-\/\d+ms GET \/logger] aaa/.test(fs.readFileSync(logPath, 'utf8'))
);
});
});

Expand Down Expand Up @@ -239,9 +241,10 @@ describe('test/app/extend/context.test.js', () => {
yield sleep(5000);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
log.should.match(/background run result file size: \d+/);
fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8')
.should.match(/\[egg:background] task:saveUserInfo success \(\d+ms\)/);
assert(/background run result file size: \d+/.test(log));
assert(
/\[egg:background] task:saveUserInfo success \(\d+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
);
});

it('should run background task error', function* () {
Expand All @@ -253,9 +256,10 @@ describe('test/app/extend/context.test.js', () => {
yield sleep(5000);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'common-error.log'), 'utf8');
log.should.match(/ENOENT: no such file or directory/);
fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8')
.should.match(/\[egg:background] task:mockError fail \(\d+ms\)/);
assert(/ENOENT: no such file or directory/.test(log));
assert(
/\[egg:background] task:mockError fail \(\d+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
);
});
});

Expand All @@ -272,13 +276,13 @@ describe('test/app/extend/context.test.js', () => {
const localServer = yield utils.startLocalServer();
const context = app.mockContext();
const res = yield context.curl(`${localServer}/foo/bar`);
res.status.should.equal(200);
assert(res.status === 200);
});

it('should curl as promise ok', () => {
return utils.startLocalServer()
.then(localServer => app.mockContext().curl(`${localServer}/foo/bar`))
.then(res => res.status.should.equal(200));
.then(res => assert(res.status === 200));
});
});

Expand Down Expand Up @@ -311,8 +315,8 @@ describe('test/app/extend/context.test.js', () => {
const context = app.mockContext();
context.locals = { a: 'a', b: 'b' };
context.state = { a: 'aa', c: 'cc' };
context.state.should.eql({ a: 'aa', b: 'b', c: 'cc' });
context.state.should.equal(context.locals);
assert.deepEqual(context.state, { a: 'aa', b: 'b', c: 'cc' });
assert(context.state === context.locals);
});
});

Expand Down
Loading

0 comments on commit 1a027ad

Please sign in to comment.