Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): Add configuration for adding javascript version. #7

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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,
Expand All @@ -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(/\?.*/, '')

Expand Down Expand Up @@ -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 && isFirefox(request)) {
scriptType += ';version=' + jsVersion
}

return util.format(SCRIPT_TAG, scriptType, filePath)
})

// TODO(vojta): don't compute if it's not in the template
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/support/tag/tag.js
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions test/e2e/support/tag/test-with-version.js
Original file line number Diff line number Diff line change
@@ -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())
})
})
6 changes: 6 additions & 0 deletions test/e2e/support/tag/test-without-version.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
75 changes: 75 additions & 0 deletions test/e2e/tag.feature
Original file line number Diff line number Diff line change
@@ -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
"""

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
"""

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
"""