diff --git a/packages/unleash-api/package.json b/packages/unleash-api/package.json index 00b7e6ba5b..7ecff6c41e 100644 --- a/packages/unleash-api/package.json +++ b/packages/unleash-api/package.json @@ -35,12 +35,12 @@ "start:dev:pg": "pg_virtualenv npm run start:dev:pg-chain", "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", - "test": "export PORT=4243 ; mocha test test/*.js && npm run test:coverage", + "test": "export PORT=4243 ; mocha test/**/*.js && npm run test:coverage", "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 --report lcovonly -- -R spec", + "test:coverage": "istanbul cover ./node_modules/mocha/bin/_mocha test/**/*.js --report lcovonly -- -R spec", "test:coverage-report": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, "dependencies": { @@ -64,6 +64,7 @@ "istanbul": "^0.4.3", "mocha": "^2.4.5", "mocha-lcov-reporter": "1.2.0", + "sinon": "^1.17.5", "supertest": "^1.2.0", "supervisor": "^0.10.0", "unleash-frontend": "1.0.0-alpha.2" diff --git a/packages/unleash-api/test/databaseConfig.js b/packages/unleash-api/test/e2e/databaseConfig.js similarity index 100% rename from packages/unleash-api/test/databaseConfig.js rename to packages/unleash-api/test/e2e/databaseConfig.js diff --git a/packages/unleash-api/test/eventApiSpec.js b/packages/unleash-api/test/e2e/eventApiSpec.js similarity index 100% rename from packages/unleash-api/test/eventApiSpec.js rename to packages/unleash-api/test/e2e/eventApiSpec.js diff --git a/packages/unleash-api/test/featureApiSpec.js b/packages/unleash-api/test/e2e/featureApiSpec.js similarity index 99% rename from packages/unleash-api/test/featureApiSpec.js rename to packages/unleash-api/test/e2e/featureApiSpec.js index 0dc7ab8537..1d245fca0b 100644 --- a/packages/unleash-api/test/featureApiSpec.js +++ b/packages/unleash-api/test/e2e/featureApiSpec.js @@ -1,5 +1,5 @@ 'use strict'; -const logger = require('../lib/logger'); +const logger = require('../../lib/logger'); const assert = require('assert'); const specHelper = require('./specHelper'); const request = specHelper.request; diff --git a/packages/unleash-api/test/featureArchiveApiSpec.js b/packages/unleash-api/test/e2e/featureArchiveApiSpec.js similarity index 100% rename from packages/unleash-api/test/featureArchiveApiSpec.js rename to packages/unleash-api/test/e2e/featureArchiveApiSpec.js diff --git a/packages/unleash-api/test/routerSpec.js b/packages/unleash-api/test/e2e/routerSpec.js similarity index 100% rename from packages/unleash-api/test/routerSpec.js rename to packages/unleash-api/test/e2e/routerSpec.js diff --git a/packages/unleash-api/test/specHelper.js b/packages/unleash-api/test/e2e/specHelper.js similarity index 90% rename from packages/unleash-api/test/specHelper.js rename to packages/unleash-api/test/e2e/specHelper.js index 408f47f501..1033b4591b 100644 --- a/packages/unleash-api/test/specHelper.js +++ b/packages/unleash-api/test/e2e/specHelper.js @@ -4,14 +4,14 @@ process.env.NODE_ENV = 'test'; const BPromise = require('bluebird'); let request = require('supertest'); const databaseUri = require('./databaseConfig').getDatabaseUri(); -const knex = require('../lib/db/dbPool')(databaseUri); -const eventDb = require('../lib/db/event')(knex); -const EventStore = require('../lib/eventStore'); +const knex = require('../../lib/db/dbPool')(databaseUri); +const eventDb = require('../../lib/db/event')(knex); +const EventStore = require('../../lib/eventStore'); const eventStore = new EventStore(eventDb); -const featureDb = require('../lib/db/feature')(knex, eventStore); -const strategyDb = require('../lib/db/strategy')(knex, eventStore); +const featureDb = require('../../lib/db/feature')(knex, eventStore); +const strategyDb = require('../../lib/db/strategy')(knex, eventStore); -const app = require('../app')({ +const app = require('../../app')({ baseUriPath: '', db: knex, eventDb, diff --git a/packages/unleash-api/test/strategyApiSpec.js b/packages/unleash-api/test/e2e/strategyApiSpec.js similarity index 100% rename from packages/unleash-api/test/strategyApiSpec.js rename to packages/unleash-api/test/e2e/strategyApiSpec.js diff --git a/packages/unleash-api/test/eventDifferTest.js b/packages/unleash-api/test/unit/eventDiffer.test.js similarity index 96% rename from packages/unleash-api/test/eventDifferTest.js rename to packages/unleash-api/test/unit/eventDiffer.test.js index be3c66436f..a40e3b3b21 100644 --- a/packages/unleash-api/test/eventDifferTest.js +++ b/packages/unleash-api/test/unit/eventDiffer.test.js @@ -1,6 +1,6 @@ 'use strict'; -const eventDiffer = require('../lib/eventDiffer'); -const eventType = require('../lib/eventType'); +const eventDiffer = require('../../lib/eventDiffer'); +const eventType = require('../../lib/eventType'); const assert = require('assert'); describe('eventDiffer', () => { diff --git a/packages/unleash-api/test/unit/routes/feature.test.js b/packages/unleash-api/test/unit/routes/feature.test.js new file mode 100644 index 0000000000..7ecd03c8ef --- /dev/null +++ b/packages/unleash-api/test/unit/routes/feature.test.js @@ -0,0 +1,60 @@ +'use strict'; + +let 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(); + + const app = require('../../../app')({ + baseUriPath: '', + db: sinon.stub(), + eventDb: sinon.stub(), + eventStore: sinon.stub(), + featureDb: featureDb, + strategyDb: sinon.stub(), + }); + + request = supertest(app); + done(); + }); + + it('should get empty getFeatures', (done) => { + request + .get('/features') + .expect('Content-Type', /json/) + .expect(200) + .end((err, res) => { + assert(res.body.features.length === 0); + done(); + }); + }); + + it('should get one getFeature', (done) => { + featureDb.addFeature( { name: 'test', strategies: [{ name: 'default' }] } ); + + request + .get('/features') + .expect('Content-Type', /json/) + .expect(200) + .end((err, res) => { + assert(res.body.features.length === 1); + done(); + }); + }); +}); + +function createFeatureDb () { + let _features = []; + return { + getFeatures: () => BPromise.resolve(_features), + addFeature: (feature) => _features.push(feature), + }; +}