diff --git a/packages/unleash-api/package.json b/packages/unleash-api/package.json index 830bf9b3e2..9f08389594 100644 --- a/packages/unleash-api/package.json +++ b/packages/unleash-api/package.json @@ -37,13 +37,13 @@ "start:dev:pg-chain": "export DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres ; db-migrate up && npm run start:dev", "db-migrate": "db-migrate up", "db-migrate:down": "db-migrate down", - "test": "export PORT=4243 ; mocha test/**/*.js && npm run test:coverage", + "test": "export PORT=4243 ; mocha --recursive test && npm run test:coverage", "test:unit": "mocha test/unit/**/*.js ", "test:ci": "npm run db-migrate && npm run test", "test:watch": "mocha --watch test test/*", "test:pg-virtualenv": "pg_virtualenv npm run test:pg-virtualenv-chai", "test:pg-virtualenv-chain": "export TEST_DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres ; npm run db-migrate-testdb && npm test", - "test:coverage": "istanbul cover ./node_modules/mocha/bin/_mocha test/**/*.js --report lcovonly -- -R spec", + "test:coverage": "istanbul cover ./node_modules/mocha/bin/_mocha test --report lcovonly -- -R spec --recursive", "test:coverage-report": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, "dependencies": { diff --git a/packages/unleash-api/test/unit/routes/feature.test.js b/packages/unleash-api/test/unit/routes/feature.test.js index e4695ab16f..671b8226f0 100644 --- a/packages/unleash-api/test/unit/routes/feature.test.js +++ b/packages/unleash-api/test/unit/routes/feature.test.js @@ -1,25 +1,30 @@ 'use strict'; +const clientMetricsStore = require('./mocks/fake-metrics-store'); +const featureToggleStore = require('./mocks/fake-feature-toggle-store'); +const strategyStore = require('./mocks/fake-strategies-store'); + const supertest = require('supertest'); -const BPromise = require('bluebird'); -BPromise.promisifyAll(supertest); const assert = require('assert'); const sinon = require('sinon'); let request; -let featureDb; describe('Unit: The features api', () => { beforeEach(done => { - featureDb = createFeatureDb(); + featureToggleStore.reset(); const app = require('../../../app')({ baseUriPath: '', - db: sinon.stub(), - eventDb: sinon.stub(), - eventStore: sinon.stub(), - featureDb, - strategyDb: sinon.stub(), + stores: { + db: sinon.stub(), + eventStore: sinon.stub(), + featureToggleStore, + clientMetricsStore, + strategyStore, + clientStrategyStore: sinon.stub(), + clientInstanceStore: sinon.stub(), + }, }); request = supertest(app); @@ -38,7 +43,7 @@ describe('Unit: The features api', () => { }); it('should get one getFeature', (done) => { - featureDb.addFeature( { name: 'test', strategies: [{ name: 'default' }] } ); + featureToggleStore.addFeature( { name: 'test', strategies: [{ name: 'default' }] } ); request .get('/features') @@ -51,7 +56,7 @@ describe('Unit: The features api', () => { }); it('should add version numbers for /features', (done) => { - featureDb.addFeature( { name: 'test', strategies: [{ name: 'default' }] } ); + featureToggleStore.addFeature( { name: 'test', strategies: [{ name: 'default' }] } ); request .get('/features') @@ -63,11 +68,3 @@ describe('Unit: The features api', () => { }); }); }); - -function createFeatureDb () { - const _features = []; - return { - getFeatures: () => BPromise.resolve(_features), - addFeature: (feature) => _features.push(feature), - }; -} diff --git a/packages/unleash-api/test/unit/routes/mocks/fake-feature-toggle-store.js b/packages/unleash-api/test/unit/routes/mocks/fake-feature-toggle-store.js new file mode 100644 index 0000000000..7997284c06 --- /dev/null +++ b/packages/unleash-api/test/unit/routes/mocks/fake-feature-toggle-store.js @@ -0,0 +1,11 @@ +'use strict'; + +const _features = []; + +module.exports = { + getFeatures: () => Promise.resolve(_features), + addFeature: (feature) => _features.push(feature), + reset: () => { + _features.lengyh = 0; + }, +}; diff --git a/packages/unleash-api/test/unit/routes/mocks/fake-metrics-store.js b/packages/unleash-api/test/unit/routes/mocks/fake-metrics-store.js new file mode 100644 index 0000000000..94c134f068 --- /dev/null +++ b/packages/unleash-api/test/unit/routes/mocks/fake-metrics-store.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = { + getMetricsLastHour: () => Promise.resolve([]), +}; diff --git a/packages/unleash-api/test/unit/routes/mocks/fake-strategies-store.js b/packages/unleash-api/test/unit/routes/mocks/fake-strategies-store.js new file mode 100644 index 0000000000..4f5042ffa0 --- /dev/null +++ b/packages/unleash-api/test/unit/routes/mocks/fake-strategies-store.js @@ -0,0 +1,12 @@ +'use strict'; + +const _strategies = [{ name: 'default', parameters: {} }]; + + +module.exports = { + getStrategies: () => Promise.resolve(_strategies), + addStrategy: (strat) => _strategies.push(strat), + reset: () => { + _strategies.length = 0; + }, +}; diff --git a/packages/unleash-api/test/unit/routes/strategies.test.js b/packages/unleash-api/test/unit/routes/strategies.test.js index 2b5ae03bb5..5c6f48468d 100644 --- a/packages/unleash-api/test/unit/routes/strategies.test.js +++ b/packages/unleash-api/test/unit/routes/strategies.test.js @@ -1,25 +1,30 @@ 'use strict'; +const clientMetricsStore = require('./mocks/fake-metrics-store'); +const featureToggleStore = require('./mocks/fake-feature-toggle-store'); +const strategyStore = require('./mocks/fake-strategies-store'); + const supertest = require('supertest'); -const BPromise = require('bluebird'); -BPromise.promisifyAll(supertest); const assert = require('assert'); const sinon = require('sinon'); let request; -let stratDb; describe('Unit: The strategies api', () => { beforeEach(done => { - stratDb = createStrategyDb(); + strategyStore.reset(); const app = require('../../../app')({ baseUriPath: '', - db: sinon.stub(), - eventDb: sinon.stub(), - eventStore: sinon.stub(), - featureDb: sinon.stub(), - strategyDb: stratDb, + stores: { + db: sinon.stub(), + eventStore: sinon.stub(), + featureToggleStore, + clientMetricsStore, + strategyStore, + clientStrategyStore: sinon.stub(), + clientInstanceStore: sinon.stub(), + }, }); request = supertest(app); @@ -37,11 +42,3 @@ describe('Unit: The strategies api', () => { }); }); }); - -function createStrategyDb () { - const _strategies = [{ name: 'default', parameters: {} }]; - return { - getStrategies: () => BPromise.resolve(_strategies), - addStrategy: (strat) => _strategies.push(strat), - }; -}