Turn on "no-unused-expressions" rule for eslint

Fix unit-tests relying on property-based assertions to use function
calls instead, using "dirty-chai" to modify chai's property checkers.
This commit is contained in:
Miroslav Bajtoš 2016-11-22 15:30:04 +01:00
parent c3ab932012
commit 1ec7cf0a37
23 changed files with 63 additions and 54 deletions

View File

@ -5,7 +5,6 @@
"ignoreComments": true, "ignoreComments": true,
"ignoreUrls": true, "ignoreUrls": true,
"ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)" "ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)"
}], }]
"no-unused-expressions": "off",
} }
} }

View File

@ -66,6 +66,7 @@
"browserify": "^13.1.0", "browserify": "^13.1.0",
"chai": "^3.5.0", "chai": "^3.5.0",
"cookie-parser": "^1.3.4", "cookie-parser": "^1.3.4",
"dirty-chai": "^1.2.2",
"es5-shim": "^4.1.0", "es5-shim": "^4.1.0",
"eslint-config-loopback": "^5.0.0", "eslint-config-loopback": "^5.0.0",
"express-session": "^1.14.0", "express-session": "^1.14.0",

View File

@ -159,7 +159,7 @@ describe('access control - integration', function() {
roleModel.registerResolver('$dynamic-role', function(role, context, callback) { roleModel.registerResolver('$dynamic-role', function(role, context, callback) {
if (!(context && context.accessToken && context.accessToken.userId)) { if (!(context && context.accessToken && context.accessToken.userId)) {
return process.nextTick(function() { return process.nextTick(function() {
callback && callback(null, false); if (callback) callback(null, false);
}); });
} }
var accessToken = context.accessToken; var accessToken = context.accessToken;
@ -216,7 +216,7 @@ describe('access control - integration', function() {
if (context.remotingContext) { if (context.remotingContext) {
count++; count++;
} }
callback && callback(null, false); // Always true if (callback) callback(null, false); // Always true
}); });
}); });
}); });

View File

@ -5,7 +5,7 @@
'use strict'; 'use strict';
var assert = require('assert'); var assert = require('assert');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var cookieParser = require('cookie-parser'); var cookieParser = require('cookie-parser');
var LoopBackContext = require('loopback-context'); var LoopBackContext = require('loopback-context');
var contextMiddleware = require('loopback-context').perRequest; var contextMiddleware = require('loopback-context').perRequest;

View File

@ -14,7 +14,7 @@ var loopback = require('../');
var PersistedModel = loopback.PersistedModel; var PersistedModel = loopback.PersistedModel;
var describe = require('./util/describe'); var describe = require('./util/describe');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var it = require('./util/it'); var it = require('./util/it');
var request = require('supertest'); var request = require('supertest');
@ -91,7 +91,7 @@ describe('app', function() {
app.middleware('routes:before', app.middleware('routes:before',
myHandler = handlerThatAddsHandler('my-handler')); myHandler = handlerThatAddsHandler('my-handler'));
var found = app._findLayerByHandler(myHandler); var found = app._findLayerByHandler(myHandler);
expect(found).to.be.object; expect(found).to.be.an('object');
expect(myHandler).to.equal(found.handle); expect(myHandler).to.equal(found.handle);
expect(found).have.property('phase', 'routes:before'); expect(found).have.property('phase', 'routes:before');
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
@ -112,7 +112,7 @@ describe('app', function() {
wrappedHandler['__NR_handler'] = myHandler; wrappedHandler['__NR_handler'] = myHandler;
app.middleware('routes:before', wrappedHandler); app.middleware('routes:before', wrappedHandler);
var found = app._findLayerByHandler(myHandler); var found = app._findLayerByHandler(myHandler);
expect(found).to.be.object; expect(found).to.be.an('object');
expect(found).have.property('phase', 'routes:before'); expect(found).have.property('phase', 'routes:before');
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
@ -132,7 +132,7 @@ describe('app', function() {
wrappedHandler['__handler'] = myHandler; wrappedHandler['__handler'] = myHandler;
app.middleware('routes:before', wrappedHandler); app.middleware('routes:before', wrappedHandler);
var found = app._findLayerByHandler(myHandler); var found = app._findLayerByHandler(myHandler);
expect(found).to.be.object; expect(found).to.be.an('object');
expect(found).have.property('phase', 'routes:before'); expect(found).have.property('phase', 'routes:before');
executeMiddlewareHandlers(app, function(err) { executeMiddlewareHandlers(app, function(err) {
if (err) return done(err); if (err) return done(err);
@ -380,7 +380,7 @@ describe('app', function() {
executeMiddlewareHandlers(app, '/mountpath/test', function(err) { executeMiddlewareHandlers(app, '/mountpath/test', function(err) {
if (err) return done(err); if (err) return done(err);
expect(mountWasEmitted, 'mountWasEmitted').to.be.true; expect(mountWasEmitted, 'mountWasEmitted').to.be.true();
expect(data).to.eql({ expect(data).to.eql({
mountpath: '/mountpath', mountpath: '/mountpath',
parent: app, parent: app,
@ -666,7 +666,7 @@ describe('app', function() {
remotedClass = sharedClass; remotedClass = sharedClass;
}); });
app.model(Color); app.model(Color);
expect(remotedClass).to.exist; expect(remotedClass).to.exist();
expect(remotedClass).to.eql(Color.sharedClass); expect(remotedClass).to.eql(Color.sharedClass);
}); });
@ -680,9 +680,9 @@ describe('app', function() {
}); });
app.model(Color); app.model(Color);
app.models.Color.disableRemoteMethodByName('findOne'); app.models.Color.disableRemoteMethodByName('findOne');
expect(remoteMethodDisabledClass).to.exist; expect(remoteMethodDisabledClass).to.exist();
expect(remoteMethodDisabledClass).to.eql(Color.sharedClass); expect(remoteMethodDisabledClass).to.eql(Color.sharedClass);
expect(disabledRemoteMethod).to.exist; expect(disabledRemoteMethod).to.exist();
expect(disabledRemoteMethod).to.eql('findOne'); expect(disabledRemoteMethod).to.eql('findOne');
}); });

View File

@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict'; 'use strict';
var expect = require('chai').expect; var expect = require('./helpers/expect');
var loopback = require('../'); var loopback = require('../');
describe('PersistedModel.createChangeStream()', function() { describe('PersistedModel.createChangeStream()', function() {

View File

@ -6,7 +6,7 @@
'use strict'; 'use strict';
var assert = require('assert'); var assert = require('assert');
var async = require('async'); var async = require('async');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var loopback = require('../'); var loopback = require('../');
var Change, TestModel; var Change, TestModel;

View File

@ -6,7 +6,7 @@
'use strict'; 'use strict';
var async = require('async'); var async = require('async');
var loopback = require('../'); var loopback = require('../');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var Checkpoint = loopback.Checkpoint.extend('TestCheckpoint'); var Checkpoint = loopback.Checkpoint.extend('TestCheckpoint');

View File

@ -8,7 +8,7 @@ var loopback = require('../');
var app; var app;
var assert = require('assert'); var assert = require('assert');
var request = require('supertest'); var request = require('supertest');
var expect = require('chai').expect; var expect = require('./helpers/expect');
describe('loopback.errorHandler(options)', function() { describe('loopback.errorHandler(options)', function() {
it('should throw a descriptive error', function() { it('should throw a descriptive error', function() {

12
test/helpers/expect.js Normal file
View File

@ -0,0 +1,12 @@
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var chai = require('chai');
chai.use(require('dirty-chai'));
chai.use(require('sinon-chai'));
module.exports = chai.expect;

View File

@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict'; 'use strict';
var expect = require('chai').expect; var expect = require('./helpers/expect');
var loopback = require('../'); var loopback = require('../');
var net = require('net'); var net = require('net');

View File

@ -1,5 +1,5 @@
'use strict'; 'use strict';
var expect = require('chai').expect; var expect = require('./helpers/expect');
var http = require('http'); var http = require('http');
var loopback = require('..'); var loopback = require('..');
var supertest = require('supertest'); var supertest = require('supertest');
@ -89,7 +89,7 @@ describe('KeyValueModel', function() {
request.get('/CacheItems/ttl-key/ttl') request.get('/CacheItems/ttl-key/ttl')
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.number; expect(res.body).to.be.a('number');
done(); done();
}); });
}); });

View File

@ -9,7 +9,7 @@ var describe = require('./util/describe');
var Domain = require('domain'); var Domain = require('domain');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var loopback = require('../'); var loopback = require('../');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var assert = require('assert'); var assert = require('assert');
describe('loopback', function() { describe('loopback', function() {

View File

@ -6,19 +6,16 @@
'use strict'; 'use strict';
var assert = require('assert'); var assert = require('assert');
var async = require('async'); var async = require('async');
var chai = require('chai');
var describe = require('./util/describe'); var describe = require('./util/describe');
var loopback = require('../'); var loopback = require('../');
var ACL = loopback.ACL; var ACL = loopback.ACL;
var defineModelTestsWithDataSource = require('./util/model-tests'); var defineModelTestsWithDataSource = require('./util/model-tests');
var PersistedModel = loopback.PersistedModel; var PersistedModel = loopback.PersistedModel;
var sinonChai = require('sinon-chai');
var Promise = require('bluebird'); var Promise = require('bluebird');
var TaskEmitter = require('strong-task-emitter'); var TaskEmitter = require('strong-task-emitter');
var request = require('supertest'); var request = require('supertest');
var expect = chai.expect; var expect = require('./helpers/expect');
chai.use(sinonChai);
describe('Model / PersistedModel', function() { describe('Model / PersistedModel', function() {
defineModelTestsWithDataSource({ defineModelTestsWithDataSource({

View File

@ -5,7 +5,7 @@
'use strict'; 'use strict';
var assert = require('assert'); var assert = require('assert');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var loopback = require('../'); var loopback = require('../');
describe('Registry', function() { describe('Registry', function() {

View File

@ -10,7 +10,7 @@ var path = require('path');
var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-integration-app'); var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-integration-app');
var app = require(path.join(SIMPLE_APP, 'server/server.js')); var app = require(path.join(SIMPLE_APP, 'server/server.js'));
var assert = require('assert'); var assert = require('assert');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var debug = require('debug')('loopback:test:relations.integration'); var debug = require('debug')('loopback:test:relations.integration');
var async = require('async'); var async = require('async');
@ -138,7 +138,7 @@ describe('relations - integration', function() {
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.array; expect(res.body).to.be.an('array');
done(); done();
}); });
@ -1410,7 +1410,7 @@ describe('relations - integration', function() {
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(err).to.not.exist; expect(err).to.not.exist();
expect(res.body.name).to.equal('Photo 1'); expect(res.body.name).to.equal('Photo 1');
done(); done();
@ -1492,8 +1492,8 @@ describe('relations - integration', function() {
Book.nestRemoting('chapters'); Book.nestRemoting('chapters');
Image.nestRemoting('book'); Image.nestRemoting('book');
expect(Book.prototype['__findById__pages__notes']).to.be.a.function; expect(Book.prototype['__findById__pages']).to.be.a('function');
expect(Image.prototype['__findById__book__pages']).to.be.a.function; expect(Image.prototype['__get__book']).to.be.a('function');
Page.beforeRemote('prototype.__findById__notes', function(ctx, result, next) { Page.beforeRemote('prototype.__findById__notes', function(ctx, result, next) {
ctx.res.set('x-before', 'before'); ctx.res.set('x-before', 'before');
@ -1563,7 +1563,7 @@ describe('relations - integration', function() {
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.an.array; expect(res.body).to.be.an('array');
expect(res.body).to.have.length(1); expect(res.body).to.have.length(1);
expect(res.body[0].name).to.equal('Page 1'); expect(res.body[0].name).to.equal('Page 1');
@ -1579,7 +1579,7 @@ describe('relations - integration', function() {
expect(res.headers['x-before']).to.equal('before'); expect(res.headers['x-before']).to.equal('before');
expect(res.headers['x-after']).to.equal('after'); expect(res.headers['x-after']).to.equal('after');
expect(res.body).to.be.an.object; expect(res.body).to.be.an('object');
expect(res.body.text).to.equal('Page Note 1'); expect(res.body.text).to.equal('Page Note 1');
done(); done();
@ -1592,7 +1592,7 @@ describe('relations - integration', function() {
.expect(404, function(err, res) { .expect(404, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.error).to.be.an.object; expect(res.body.error).to.be.an('object');
var expected = 'could not find a model with id unknown'; var expected = 'could not find a model with id unknown';
expect(res.body.error.message).to.equal(expected); expect(res.body.error.message).to.equal(expected);
expect(res.body.error.code).to.be.equal('MODEL_NOT_FOUND'); expect(res.body.error.code).to.be.equal('MODEL_NOT_FOUND');
@ -1607,7 +1607,7 @@ describe('relations - integration', function() {
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.an.array; expect(res.body).to.be.an('array');
expect(res.body).to.have.length(1); expect(res.body).to.have.length(1);
expect(res.body[0].name).to.equal('Page 1'); expect(res.body[0].name).to.equal('Page 1');
@ -1621,7 +1621,7 @@ describe('relations - integration', function() {
.end(function(err, res) { .end(function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.an.object; expect(res.body).to.be.an('object');
expect(res.body.name).to.equal('Page 1'); expect(res.body.name).to.equal('Page 1');
done(); done();
@ -1634,7 +1634,7 @@ describe('relations - integration', function() {
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body).to.be.an.array; expect(res.body).to.be.an('array');
expect(res.body).to.have.length(1); expect(res.body).to.have.length(1);
expect(res.body[0].text).to.equal('Page Note 1'); expect(res.body[0].text).to.equal('Page Note 1');
@ -1650,7 +1650,7 @@ describe('relations - integration', function() {
expect(res.headers['x-before']).to.equal('before'); expect(res.headers['x-before']).to.equal('before');
expect(res.headers['x-after']).to.equal('after'); expect(res.headers['x-after']).to.equal('after');
expect(res.body).to.be.an.object; expect(res.body).to.be.an('object');
expect(res.body.text).to.equal('Page Note 1'); expect(res.body.text).to.equal('Page Note 1');
done(); done();
@ -1663,8 +1663,8 @@ describe('relations - integration', function() {
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.headers['x-before']).to.empty; expect(res.headers['x-before']).to.empty();
expect(res.headers['x-after']).to.empty; expect(res.headers['x-after']).to.empty();
done(); done();
}); });

View File

@ -10,7 +10,7 @@ var path = require('path');
var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-integration-app'); var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-integration-app');
var app = require(path.join(SIMPLE_APP, 'server/server.js')); var app = require(path.join(SIMPLE_APP, 'server/server.js'));
var assert = require('assert'); var assert = require('assert');
var expect = require('chai').expect; var expect = require('./helpers/expect');
describe('remoting - integration', function() { describe('remoting - integration', function() {
lt.beforeEach.withApp(app); lt.beforeEach.withApp(app);

View File

@ -8,7 +8,7 @@ var async = require('async');
var debug = require('debug')('test'); var debug = require('debug')('test');
var extend = require('util')._extend; var extend = require('util')._extend;
var loopback = require('../'); var loopback = require('../');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var supertest = require('supertest'); var supertest = require('supertest');
describe('Replication over REST', function() { describe('Replication over REST', function() {

View File

@ -10,7 +10,7 @@ var loopback = require('../');
var Change = loopback.Change; var Change = loopback.Change;
var defineModelTestsWithDataSource = require('./util/model-tests'); var defineModelTestsWithDataSource = require('./util/model-tests');
var PersistedModel = loopback.PersistedModel; var PersistedModel = loopback.PersistedModel;
var expect = require('chai').expect; var expect = require('./helpers/expect');
var debug = require('debug')('test'); var debug = require('debug')('test');
describe('Replication / Change APIs', function() { describe('Replication / Change APIs', function() {
@ -216,7 +216,7 @@ describe('Replication / Change APIs', function() {
SourceModel.changes(FUTURE_CHECKPOINT, {}, function(err, changes) { SourceModel.changes(FUTURE_CHECKPOINT, {}, function(err, changes) {
if (err) return done(err); if (err) return done(err);
expect(changes).to.be.empty; expect(changes).to.be.empty();
done(); done();
}); });

View File

@ -5,7 +5,7 @@
'use strict'; 'use strict';
var assert = require('assert'); var assert = require('assert');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var loopback = require('../'); var loopback = require('../');
var path = require('path'); var path = require('path');
var request = require('supertest'); var request = require('supertest');
@ -207,7 +207,7 @@ describe('loopback.rest', function() {
app.model(AccessToken, {dataSource: 'db'}); app.model(AccessToken, {dataSource: 'db'});
var User = app.registry.getModel('User'); var User = app.registry.getModel('User');
// Speed up the password hashing algorithm for tests // Speed up the password hashing algorithm for tests
User.settings.saltWorkFactor = 4, User.settings.saltWorkFactor = 4;
app.model(User, {dataSource: 'db'}); app.model(User, {dataSource: 'db'});
// NOTE(bajtos) This is puzzling to me. The built-in User & AccessToken // NOTE(bajtos) This is puzzling to me. The built-in User & AccessToken

View File

@ -8,7 +8,7 @@ var assert = require('assert');
var sinon = require('sinon'); var sinon = require('sinon');
var loopback = require('../index'); var loopback = require('../index');
var async = require('async'); var async = require('async');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var Promise = require('bluebird'); var Promise = require('bluebird');
function checkResult(err, result) { function checkResult(err, result) {
@ -144,7 +144,7 @@ describe('role model', function() {
if (err) return done(err); if (err) return done(err);
Role.create({name: 'userRole'}, function(err, role) { Role.create({name: 'userRole'}, function(err, role) {
expect(err).to.exist; expect(err).to.exist();
expect(err).to.have.property('name', 'ValidationError'); expect(err).to.have.property('name', 'ValidationError');
expect(err).to.have.deep.property('details.codes.name'); expect(err).to.have.deep.property('details.codes.name');
expect(err.details.codes.name).to.contain('uniqueness'); expect(err.details.codes.name).to.contain('uniqueness');

View File

@ -9,7 +9,7 @@ var lt = require('./helpers/loopback-testing-helper');
var path = require('path'); var path = require('path');
var SIMPLE_APP = path.join(__dirname, 'fixtures', 'user-integration-app'); var SIMPLE_APP = path.join(__dirname, 'fixtures', 'user-integration-app');
var app = require(path.join(SIMPLE_APP, 'server/server.js')); var app = require(path.join(SIMPLE_APP, 'server/server.js'));
var expect = require('chai').expect; var expect = require('./helpers/expect');
describe('users - integration', function() { describe('users - integration', function() {
lt.beforeEach.withApp(app); lt.beforeEach.withApp(app);
@ -39,7 +39,7 @@ describe('users - integration', function() {
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.id).to.exist; expect(res.body.id).to.exist();
userId = res.body.id; userId = res.body.id;
done(); done();
@ -54,7 +54,7 @@ describe('users - integration', function() {
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.id).to.exist; expect(res.body.id).to.exist();
accessToken = res.body.id; accessToken = res.body.id;
done(); done();
@ -105,7 +105,7 @@ describe('users - integration', function() {
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.id).to.exist; expect(res.body.id).to.exist();
userId = res.body.id; userId = res.body.id;
done(); done();
@ -120,7 +120,7 @@ describe('users - integration', function() {
.expect(200, function(err, res) { .expect(200, function(err, res) {
if (err) return done(err); if (err) return done(err);
expect(res.body.id).to.exist; expect(res.body.id).to.exist();
accessToken = res.body.id; accessToken = res.body.id;
done(); done();

View File

@ -5,7 +5,7 @@
'use strict'; 'use strict';
var assert = require('assert'); var assert = require('assert');
var expect = require('chai').expect; var expect = require('./helpers/expect');
var request = require('supertest'); var request = require('supertest');
var loopback = require('../'); var loopback = require('../');
var User, AccessToken; var User, AccessToken;