diff --git a/.istanbul.yml b/.istanbul.yml new file mode 100644 index 000000000..488665778 --- /dev/null +++ b/.istanbul.yml @@ -0,0 +1,2 @@ +instrumentation: + root: ./server diff --git a/bin/coverage.js b/bin/coverage.js new file mode 100644 index 000000000..39e07cd8a --- /dev/null +++ b/bin/coverage.js @@ -0,0 +1,43 @@ +fs = require('fs') +child_process = require('child_process') + +// Have to convert all CS server files to JS for coverage to work. +// Walk through folders in server/ and compile each .coffee file, +// keeping a list of generated files so they can be deleted later. + +var directories = ['./server'] +var convertedFiles = []; +console.log('Convert server coffeescript files.') + +while(directories.length) { + directory = directories.pop() + console.log('*', directory) + + fs.readdirSync(directory).forEach((fileOrDir) => { + absPath = directory + '/' + fileOrDir + stat = fs.statSync(absPath) + + // .coffee => .js + if(stat.isFile() && fileOrDir.endsWith('.coffee')) { + child_process.execSync(`coffee -c ${absPath}`) + convertedFiles.push(absPath.replace('.coffee', '.js')) + } + + // Add to list of directories to walk + if(stat.isDirectory()) { + directories.push(absPath); + } + }) +} + +// Run Istanbul +console.log(`Converted ${convertedFiles.length} server coffeescript files. Running tests...`) +child_process.execSync('istanbul cover ./node_modules/jasmine/bin/jasmine.js') + +// Cleanup +console.log('Coverage report generated. Deleting converted files...') +for (file of convertedFiles) { + fs.unlinkSync(file) +} + +console.log('Done.') diff --git a/package.json b/package.json index 6d9e41567..a14f9f239 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,8 @@ "smoke-local": "COCO_SMOKE_DOMAIN='local' nightwatch", "smoke-next": "COCO_SMOKE_DOMAIN='next' nightwatch", "smoke-prod": "COCO_SMOKE_DOMAIN='prod' nightwatch", - "smoke-staging": "COCO_SMOKE_DOMAIN='staging' nightwatch" + "smoke-staging": "COCO_SMOKE_DOMAIN='staging' nightwatch", + "coverage": "node ./bin/coverage.js" }, "main": "index.js", "keywords": [ @@ -111,6 +112,7 @@ "css-brunch": "^1.7.0", "fs-extra": "^0.26.2", "http-proxy": "^1.13.2", + "istanbul": "^0.4.5", "jade-brunch": "1.7.5", "jasmine": "2.4.1", "javascript-brunch": "> 1.0 < 1.8", diff --git a/server/routes/db/product.coffee b/server/routes/db/product.coffee index c1b854aea..38fbbdd33 100644 --- a/server/routes/db/product.coffee +++ b/server/routes/db/product.coffee @@ -1,19 +1,19 @@ Product = require '../../models/Product' errors = require '../../commons/errors' config = require '../../../server_config' +wrap = require 'co-express' -module.exports.get = (req, res) -> +module.exports.get = wrap (req, res) -> - Product.find().lean().exec (err, products) -> - return errors.serverError(res) if err - names = (product.name for product in products) - unless config.isProduction - for product in initProducts - if not _.contains(names, product.name) - # upsert products in initProducts if they DNE - products.push(product) - new Product(product).save _.noop - res.send(products) + products = yield Product.find() + names = (product.name for product in products) + unless config.isProduction + for product in initProducts + if not _.contains(names, product.name) + # upsert products in initProducts if they DNE + products.push(product) + new Product(product).save _.noop + res.send(products) ### Stub data, used in tests and dev environment.