From c25ed008ac98efc7b88f4436dfe0582f0b012404 Mon Sep 17 00:00:00 2001 From: Mourad Date: Tue, 8 Mar 2016 17:53:09 +0100 Subject: [PATCH] feat(config): Add configuration for adding javascript version. Add the configuration to add a javascript version tag to the loaded scripts. Only applied when the Firefox browser is run. Closes #1719. --- docs/config/01-configuration-file.md | 12 ++++ lib/middleware/karma.js | 23 +++++- test/client/karma.conf.js | 2 + test/e2e/support/tag/tag.js | 14 ++++ test/e2e/support/tag/test-with-version.js | 6 ++ test/e2e/support/tag/test-without-version.js | 6 ++ test/e2e/tag.feature | 75 ++++++++++++++++++++ 7 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 test/e2e/support/tag/tag.js create mode 100644 test/e2e/support/tag/test-with-version.js create mode 100644 test/e2e/support/tag/test-without-version.js create mode 100644 test/e2e/tag.feature diff --git a/docs/config/01-configuration-file.md b/docs/config/01-configuration-file.md index fcb81e930..81a9a8ccb 100644 --- a/docs/config/01-configuration-file.md +++ b/docs/config/01-configuration-file.md @@ -610,6 +610,18 @@ All of Karma's urls get prefixed with the `urlRoot`. This is helpful when using sometimes you might want to proxy a url that is already taken by Karma. +## jsVersion +**Type:** Number + +**Default:** `0` + +**Description:** The JavaScript version to use in the FireFox browser. + +If `> 0`, Karma will add a JavaScript version tag to the included .js-files. + +Note: This will only be applied to the FireFox browser. It is currently the only browser that supports the version tag. + + [plugins]: plugins.html [config/files]: files.html [config/browsers]: browsers.html diff --git a/lib/middleware/karma.js b/lib/middleware/karma.js index 0c965a521..9c3168d72 100644 --- a/lib/middleware/karma.js +++ b/lib/middleware/karma.js @@ -14,6 +14,7 @@ var path = require('path') var util = require('util') var url = require('url') +var useragent = require('useragent') var log = require('../logger').create('middleware:karma') @@ -61,6 +62,17 @@ var getXUACompatibleUrl = function (url) { return value } +var isFirefox = function (req) { + if (!(req && req.headers)) { + return false + } + + // Browser check + var firefox = useragent.is(req.headers['user-agent']).firefox + + return firefox +} + var createKarmaMiddleware = function ( filesPromise, serveStaticFile, @@ -74,6 +86,7 @@ var createKarmaMiddleware = function ( var client = injector.get('config.client') var customContextFile = injector.get('config.customContextFile') var customDebugFile = injector.get('config.customDebugFile') + var jsVersion = injector.get('config.jsVersion') var requestUrl = request.normalizedUrl.replace(/\?.*/, '') @@ -160,7 +173,15 @@ var createKarmaMiddleware = function ( return util.format(LINK_TAG_HTML, filePath) } - return util.format(SCRIPT_TAG, SCRIPT_TYPE[fileExt] || 'text/javascript', filePath) + // The script tag to be placed + var scriptType = (SCRIPT_TYPE[fileExt] || 'text/javascript') + + // In case there is a JavaScript version specified and this is a Firefox browser + if (jsVersion && jsVersion > 0 && isFirefox(request)) { + scriptType += ';version=' + jsVersion + } + + return util.format(SCRIPT_TAG, scriptType, filePath) }) // TODO(vojta): don't compute if it's not in the template diff --git a/test/client/karma.conf.js b/test/client/karma.conf.js index 4422ca7cb..1ffff3041 100644 --- a/test/client/karma.conf.js +++ b/test/client/karma.conf.js @@ -156,6 +156,8 @@ module.exports = function (config) { forceJSONP: true, + jsVersion: 0, + browserStack: { project: 'Karma' } diff --git a/test/e2e/support/tag/tag.js b/test/e2e/support/tag/tag.js new file mode 100644 index 000000000..296a6615a --- /dev/null +++ b/test/e2e/support/tag/tag.js @@ -0,0 +1,14 @@ +/* eslint-disable no-unused-vars */ +var isFirefox = function () { + return typeof InstallTrigger !== 'undefined' +} + +var containsJsTag = function () { + var scripts = document.getElementsByTagName('script') + for (var i = 0; i < scripts.length; i++) { + if (scripts[i].type.indexOf(';version=') > -1) { + return true + } + } + return false +} diff --git a/test/e2e/support/tag/test-with-version.js b/test/e2e/support/tag/test-with-version.js new file mode 100644 index 000000000..b8b5cbb47 --- /dev/null +++ b/test/e2e/support/tag/test-with-version.js @@ -0,0 +1,6 @@ +/* globals containsJsTag, isFirefox */ +describe('JavaScript version tag', function () { + it('should add the version tag, if Firefox is used', function () { + expect(containsJsTag()).toBe(isFirefox()) + }) +}) diff --git a/test/e2e/support/tag/test-without-version.js b/test/e2e/support/tag/test-without-version.js new file mode 100644 index 000000000..5809dc906 --- /dev/null +++ b/test/e2e/support/tag/test-without-version.js @@ -0,0 +1,6 @@ +/* globals containsJsTag */ +describe('JavaScript version tag', function () { + it('should not add the version tag for every browser', function () { + expect(containsJsTag()).toBe(false) + }) +}) diff --git a/test/e2e/tag.feature b/test/e2e/tag.feature new file mode 100644 index 000000000..00e352886 --- /dev/null +++ b/test/e2e/tag.feature @@ -0,0 +1,75 @@ +Feature: JavaScript Tag + In order to use Karma + As a person who wants to write great tests + I want to add a JavaScript version tag in Firefox only. + + Scenario: Execute a test in Firefox with version, with JavaScript tag + Given a configuration with: + """ + files = ['tag/tag.js', 'tag/test-with-version.js']; + browsers = ['Firefox'] + jsVersion = 1.8 + plugins = [ + 'karma-jasmine', + 'karma-firefox-launcher' + ] + """ + When I start Karma + Then it passes with: + """ + . + Firefox + """ + @not-jenkins + Scenario: Execute a test in Chrome with version, without JavaScript tag + Given a configuration with: + """ + files = ['tag/tag.js', 'tag/test-with-version.js']; + browsers = ['Chrome']; + jsVersion = 1.8; + plugins = [ + 'karma-jasmine', + 'karma-chrome-launcher' + ]; + """ + When I start Karma + Then it passes with: + """ + . + Chrome + """ + + Scenario: Execute a test in Firefox without version, without JavaScript tag + Given a configuration with: + """ + files = ['tag/tag.js', 'tag/test-without-version.js']; + browsers = ['Firefox'] + plugins = [ + 'karma-jasmine', + 'karma-firefox-launcher' + ] + """ + When I start Karma + Then it passes with: + """ + . + Firefox + """ + @not-jenkins + Scenario: Execute a test in Chrome without version, without JavaScript tag + Given a configuration with: + """ + files = ['tag/tag.js', 'tag/test-without-version.js']; + browsers = ['Chrome']; + plugins = [ + 'karma-jasmine', + 'karma-chrome-launcher' + ]; + """ + When I start Karma + Then it passes with: + """ + . + Chrome + """ + \ No newline at end of file