Skip to content

Commit 225c0e5

Browse files
committed
feat(config): Allow custom context and debug files, with feature test and some specs.
1 parent 0f1b1ec commit 225c0e5

9 files changed

Lines changed: 121 additions & 13 deletions

File tree

docs/config/01-configuration-file.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,23 @@ upon the completion of running the tests. Setting this to false is useful when
222222

223223
**Description:** How many browser Karma launches in parallel.
224224

225-
Especially on sevices like SauceLabs and Browserstack it makes sense to only launch a limited amount of browsers at once, and only start more when those have finished. Using this configuration you can sepcify how many browsers should be running at once at any given point in time.
225+
Especially on services like SauceLabs and Browserstack it makes sense to only launch a limited amount of browsers at once, and only start more when those have finished. Using this configuration you can specify how many browsers should be running at once at any given point in time.
226+
227+
228+
## customContextFile
229+
**Type:** string
230+
231+
**Default:** `null`
232+
233+
**Description:** If `null` (default), uses karma's own `context.html` file.
234+
235+
236+
## customDebugFile
237+
**Type:** string
238+
239+
**Default:** `null`
240+
241+
**Description:** If `null` (default), uses karma's own `debug.html` file.
226242

227243

228244
## customHeaders

lib/config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,15 @@ var normalizeConfig = function (config, configFilePath) {
109109

110110
config.files = config.files.map(createPatternObject).map(createPatternMapper(basePathResolve))
111111
config.exclude = config.exclude.map(basePathResolve)
112+
config.customContextFile = config.customContextFile && basePathResolve(config.customContextFile)
113+
config.customDebugFile = config.customDebugFile && basePathResolve(config.customDebugFile)
112114

113115
// normalize paths on windows
114116
config.basePath = helper.normalizeWinPath(config.basePath)
115117
config.files = config.files.map(createPatternMapper(helper.normalizeWinPath))
116118
config.exclude = config.exclude.map(helper.normalizeWinPath)
119+
config.customContextFile = helper.normalizeWinPath(config.customContextFile)
120+
config.customDebugFile = helper.normalizeWinPath(config.customDebugFile)
117121

118122
// normalize urlRoot
119123
config.urlRoot = normalizeUrlRoot(config.urlRoot)
@@ -235,6 +239,8 @@ var Config = function () {
235239
this.httpsServerConfig = {}
236240
this.basePath = ''
237241
this.files = []
242+
this.customContextFile = null
243+
this.customDebugFile = null
238244
this.exclude = []
239245
this.logLevel = constant.LOG_INFO
240246
this.colors = true

lib/middleware/karma.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ var getXUACompatibleUrl = function (url) {
5959
return value
6060
}
6161

62-
var createKarmaMiddleware = function (filesPromise, serveStaticFile,
63-
/* config.basePath */ basePath, /* config.urlRoot */ urlRoot, /* config.client */ client) {
62+
var createKarmaMiddleware = function (filesPromise, serveStaticFile, serveFile,
63+
/* config.basePath */ basePath, /* config.urlRoot */ urlRoot, /* config.client */ client,
64+
/* config.customContextFile */ customContextFile, /* config.customDebugFile */ customDebugFile) {
6465
return function (request, response, next) {
6566
var requestUrl = request.normalizedUrl.replace(/\?.*/, '')
6667

@@ -103,9 +104,24 @@ var createKarmaMiddleware = function (filesPromise, serveStaticFile,
103104

104105
// serve context.html - execution context within the iframe
105106
// or debug.html - execution context without channel to the server
106-
if (requestUrl === '/context.html' || requestUrl === '/debug.html') {
107+
var isRequestingContextFile = requestUrl === '/context.html'
108+
var isRequestingDebugFile = requestUrl === '/debug.html'
109+
if (isRequestingContextFile || isRequestingDebugFile) {
107110
return filesPromise.then(function (files) {
108-
serveStaticFile(requestUrl, response, function (data) {
111+
var fileServer
112+
var requestedFileUrl
113+
if (isRequestingContextFile && customContextFile) {
114+
fileServer = serveFile
115+
requestedFileUrl = customContextFile
116+
} else if (isRequestingDebugFile && customDebugFile) {
117+
fileServer = serveFile
118+
requestedFileUrl = customDebugFile
119+
} else {
120+
fileServer = serveStaticFile
121+
requestedFileUrl = requestUrl
122+
}
123+
124+
fileServer(requestedFileUrl, response, function (data) {
109125
common.setNoCacheHeaders(response)
110126

111127
var scriptTags = files.included.map(function (file) {
@@ -178,8 +194,9 @@ var createKarmaMiddleware = function (filesPromise, serveStaticFile,
178194
}
179195
}
180196

181-
createKarmaMiddleware.$inject = ['filesPromise', 'serveStaticFile',
182-
'config.basePath', 'config.urlRoot', 'config.client']
197+
createKarmaMiddleware.$inject = ['filesPromise', 'serveStaticFile', 'serveFile',
198+
'config.basePath', 'config.urlRoot', 'config.client', 'config.customContextFile',
199+
'config.customDebugFile']
183200

184201
// PUBLIC API
185202
exports.create = createKarmaMiddleware

test/e2e/custom-context.feature

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Feature: Custom Context File
2+
In order to use Karma
3+
As a person who wants to write great tests
4+
I want Karma to use a custom context file
5+
6+
Scenario: Custom context.html file
7+
Given a configuration with:
8+
"""
9+
files = ['context/*.js'];
10+
browsers = ['PhantomJS'];
11+
plugins = [
12+
'karma-jasmine',
13+
'karma-phantomjs-launcher'
14+
];
15+
customContextFile = 'context/context2.html'
16+
"""
17+
When I start Karma
18+
Then it passes with:
19+
"""
20+
.
21+
PhantomJS
22+
"""

test/e2e/steps/core_steps.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ module.exports = function coreSteps () {
121121
actualOutput = lines.join('\n')
122122
}
123123

124-
if (actualOutput.indexOf(expectedOutput) === 0) {
124+
if (actualOutput.indexOf(expectedOutput) >= 0) {
125125
return callback()
126126
}
127127

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<!--
3+
This is the execution context.
4+
Loaded within the iframe.
5+
Reloaded before every execution run.
6+
-->
7+
<html>
8+
<head>
9+
<title></title>
10+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
11+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
12+
</head>
13+
<body>
14+
<!-- The scripts need to be in the body DOM element, as some test running frameworks need the body
15+
to have already been created so they can insert their magic into it. For example, if loaded
16+
before body, Angular Scenario test framework fails to find the body and crashes and burns in
17+
an epic manner. -->
18+
<div id="custom-context"></div>
19+
<script type="text/javascript">
20+
// sets window.__karma__ and overrides console and error handling
21+
// Use window.opener if this was opened by someone else - in a new window
22+
if (window.opener) {
23+
window.opener.karma.setupContext(window);
24+
} else {
25+
window.parent.karma.setupContext(window);
26+
}
27+
28+
// All served files with the latest timestamps
29+
%MAPPINGS%
30+
</script>
31+
<!-- Dynamically replaced with <script> tags -->
32+
%SCRIPTS%
33+
<script type="text/javascript">
34+
window.__karma__.loaded();
35+
</script>
36+
</body>
37+
</html>

test/e2e/support/context/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('custom context file', function () {
2+
it('should be able to find custom DOM elements', function () {
3+
expect(document.querySelector('#custom-context') == null).toBe(false)
4+
})
5+
})

test/unit/config.spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ describe('config', () => {
261261
it('should convert patterns to objects and set defaults', () => {
262262
var config = normalizeConfigWithDefaults({
263263
basePath: '/base',
264-
files: ['a/*.js', {pattern: 'b.js', watched: false, included: false}, {pattern: 'c.js'}]
264+
files: ['a/*.js', {pattern: 'b.js', watched: false, included: false}, {pattern: 'c.js'}],
265+
customContextFile: 'context.html',
266+
customDebugFile: 'debug.html'
265267
})
266268

267269
expect(config.files.length).to.equal(3)
@@ -283,6 +285,9 @@ describe('config', () => {
283285
expect(file.included).to.equal(true)
284286
expect(file.served).to.equal(true)
285287
expect(file.watched).to.equal(true)
288+
289+
expect(config.customContextFile).to.equal(resolveWinPath('/base/context.html'))
290+
expect(config.customDebugFile).to.equal(resolveWinPath('/base/debug.html'))
286291
})
287292

288293
it('should normalize preprocessors to an array', () => {

test/unit/middleware/karma.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('middleware.karma', () => {
4040
response = new HttpResponseMock()
4141
filesDeferred = helper.defer()
4242
serveFile = createServeFile(fsMock, '/karma/static')
43-
handler = createKarmaMiddleware(filesDeferred.promise, serveFile, '/base/path', '/__karma__/', clientConfig)
43+
handler = createKarmaMiddleware(filesDeferred.promise, serveFile, null, '/base/path', '/__karma__/', clientConfig)
4444
})
4545

4646
// helpers
@@ -92,7 +92,7 @@ describe('middleware.karma', () => {
9292
})
9393

9494
it('should serve client.html', (done) => {
95-
handler = createKarmaMiddleware(null, serveFile, '/base', '/')
95+
handler = createKarmaMiddleware(null, serveFile, null, '/base', '/')
9696

9797
response.once('end', () => {
9898
expect(nextSpy).not.to.have.been.called
@@ -104,7 +104,7 @@ describe('middleware.karma', () => {
104104
})
105105

106106
it('should serve /?id=xxx', (done) => {
107-
handler = createKarmaMiddleware(null, serveFile, '/base', '/')
107+
handler = createKarmaMiddleware(null, serveFile, null, '/base', '/')
108108

109109
response.once('end', () => {
110110
expect(nextSpy).not.to.have.been.called
@@ -116,7 +116,7 @@ describe('middleware.karma', () => {
116116
})
117117

118118
it('should serve /?x-ua-compatible with replaced values', (done) => {
119-
handler = createKarmaMiddleware(null, serveFile, '/base', '/')
119+
handler = createKarmaMiddleware(null, serveFile, null, '/base', '/')
120120

121121
response.once('end', () => {
122122
expect(nextSpy).not.to.have.been.called

0 commit comments

Comments
 (0)