From 461c7a38f04ac578fc7291a77d1eb77a90ecb84f Mon Sep 17 00:00:00 2001 From: Darren Haken Date: Tue, 25 Apr 2017 16:04:57 +0100 Subject: [PATCH] Change Mocha to not need a babel build to run (#349) * Change Mocha to not need a babel build to run - Also add tests around normalizeUrl * PR 359 Apply changes due to comments - Remove babelrc as its in the package.json - Change tdd npm task to use gulp - Remove source map support file from import list for normalizeUrlSpec - Change gulp tdd task to run mocha on first run 359 Apply changes due to comments - Remove babelrc as its in the package.json - Change tdd npm task to use gulp - Remove source map support file from import list for normalizeUrlSpec - Change gulp tdd task to run mocha on first run 359 Apply changes due to comments - Remove babelrc as its in the package.json - Change tdd npm task to use gulp - Remove source map support file from import list for normalizeUrlSpec - Change gulp tdd task to run mocha on first run --- gulp/tests/mocha.js | 26 +++++++++++++------ package.json | 4 ++- src/options/normalizeUrl.js | 19 ++++++++------ .../{getIcon-spec.js => getIconSpec.js} | 0 test/module/{index-spec.js => indexSpec.js} | 0 ...serAgent-spec.js => inferUserAgentSpec.js} | 0 test/module/options/normalizeUrlSpec.js | 25 ++++++++++++++++++ 7 files changed, 57 insertions(+), 17 deletions(-) rename test/module/{getIcon-spec.js => getIconSpec.js} (100%) rename test/module/{index-spec.js => indexSpec.js} (100%) rename test/module/{inferUserAgent-spec.js => inferUserAgentSpec.js} (100%) create mode 100644 test/module/options/normalizeUrlSpec.js diff --git a/gulp/tests/mocha.js b/gulp/tests/mocha.js index 2a9f9d5..bb67128 100644 --- a/gulp/tests/mocha.js +++ b/gulp/tests/mocha.js @@ -2,21 +2,31 @@ import gulp from 'gulp'; import PATHS from './../helpers/src-paths'; import istanbul from 'gulp-istanbul'; +import {Instrumenter} from 'isparta'; import mocha from 'gulp-mocha'; -gulp.task('mocha', ['build'], done => { - gulp.src(PATHS.CLI_DEST_JS) - .pipe(istanbul({includeUntested: true})) +gulp.task('mocha', done => { + gulp.src([PATHS.CLI_SRC_JS, '!src/cli.js']) + .pipe(istanbul({ + instrumenter: Instrumenter, + includeUntested: true + })) + .pipe(istanbul.hookRequire()) // Force `require` to return covered files .on('finish', () => { - return gulp.src(PATHS.TEST_DEST_JS, {read: false}) - .pipe(mocha()) + return gulp.src(PATHS.TEST_SRC, {read: false}) + .pipe(mocha({ + compilers: 'js:babel-core/register', + recursive: true + })) .pipe(istanbul.writeReports({ dir: './coverage', reporters: ['lcov'], reportOpts: {dir: './coverage'} })) - .on('finish', () => { - done(); - }); + .on('end', done); }); }); + +gulp.task('tdd', ['mocha'], () => { + return gulp.watch(['src/**/*.js', 'test/**/*.js'], ['mocha']); +}); diff --git a/package.json b/package.json index 1e3574c..aa92937 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,9 @@ "scripts": { "dev-up": "npm install && (cd app && npm install) && npm run build", "test": "gulp test", + "tdd": "gulp tdd", "lint": "eslint .", - "ci": "gulp test && npm run lint", + "ci": "gulp build test && npm run lint", "clean": "gulp clean", "build": "gulp build", "watch": "while true ; do gulp watch ; done", @@ -74,6 +75,7 @@ "gulp-istanbul": "^1.1.1", "gulp-mocha": "^4.3.0", "gulp-sourcemaps": "^2.6.0", + "isparta": "^4.0.0", "require-dir": "^0.3.0", "run-sequence": "^1.1.5", "webpack-stream": "^3.1.0" diff --git a/src/options/normalizeUrl.js b/src/options/normalizeUrl.js index 4ba08df..0fe1263 100644 --- a/src/options/normalizeUrl.js +++ b/src/options/normalizeUrl.js @@ -1,23 +1,26 @@ import url from 'url'; import validator from 'validator'; -function normalizeUrl(testUrl) { - // add protocol if protocol not found - let normalized = testUrl; - const parsed = url.parse(normalized); +function appendProtocol(testUrl) { + const parsed = url.parse(testUrl); if (!parsed.protocol) { - normalized = 'http://' + normalized; + return `http://${testUrl}`; } + return testUrl; +} + +function normalizeUrl(testUrl) { + const urlWithProtocol = appendProtocol(testUrl); const validatorOptions = { require_protocol: true, require_tld: false, allow_trailing_dot: true // mDNS addresses, https://github.com/jiahaog/nativefier/issues/308 }; - if (!validator.isURL(normalized, validatorOptions)) { - throw `Your Url: "${normalized}" is invalid!`; + if (!validator.isURL(urlWithProtocol, validatorOptions)) { + throw `Your Url: "${urlWithProtocol}" is invalid!`; } - return normalized; + return urlWithProtocol; } export default normalizeUrl; diff --git a/test/module/getIcon-spec.js b/test/module/getIconSpec.js similarity index 100% rename from test/module/getIcon-spec.js rename to test/module/getIconSpec.js diff --git a/test/module/index-spec.js b/test/module/indexSpec.js similarity index 100% rename from test/module/index-spec.js rename to test/module/indexSpec.js diff --git a/test/module/inferUserAgent-spec.js b/test/module/inferUserAgentSpec.js similarity index 100% rename from test/module/inferUserAgent-spec.js rename to test/module/inferUserAgentSpec.js diff --git a/test/module/options/normalizeUrlSpec.js b/test/module/options/normalizeUrlSpec.js new file mode 100644 index 0000000..6734529 --- /dev/null +++ b/test/module/options/normalizeUrlSpec.js @@ -0,0 +1,25 @@ +import normalizeUrl from '../../../src/options/normalizeUrl'; +import chai from 'chai'; +const assert = chai.assert; +const expect = chai.expect; + +describe('Normalize URL', () => { + + describe('given a valid URL without a protocol', () => { + it('should allow the url', () => { + assert.equal(normalizeUrl('http://www.google.com'), 'http://www.google.com'); + }); + }); + + describe('given a valid URL without a protocol', () => { + it('should allow the url and prepend the HTTP protocol', () => { + assert.equal(normalizeUrl('www.google.com'), 'http://www.google.com'); + }); + }); + + describe('given an invalid URL', () => { + it('should throw an exception', () => { + expect(() => normalizeUrl('http://ssddfoo bar')).to.throw('Your Url: "http://ssddfoo bar" is invalid!'); + }); + }); +});