Skip to content

Commit b73dbd6

Browse files
nicojsJonathan Ginsburg
authored andcommitted
feat(spec-filter): allow custom specFilter
Allow users to define their own jasmine `specFilter`. ```js jasmine.getEnv().configure({ specFilter: function(spec) { // ... } }) ```
1 parent 58d5d25 commit b73dbd6

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ run a subset of the full set of specs. Complete sharding support needs to be
8181
done in the process that calls karma, and would need to support test result
8282
integration across shards.
8383

84+
## Custom spec filter
85+
86+
Providing a [custom spec filter](https://jasmine.github.io/api/edge/Configuration#specFilter) is also supported.
87+
88+
Example:
89+
90+
```js
91+
// Users are able to set a custom specFilter themselves
92+
93+
jasmine.getEnv().configure({
94+
specFilter: function (spec) {
95+
return spec.getFullName() === 'spec that succeeds'
96+
}
97+
})
98+
99+
describe('spec', () => {
100+
it('that fails', () => {
101+
fail('This spec should not run!')
102+
})
103+
104+
it('that succeeds', () => {
105+
expect(1).toBe(1)
106+
})
107+
})
108+
```
109+
84110
---
85111

86112
For more information on Karma see the [homepage](https://karma-runner.github.io/).

src/adapter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,9 @@ var KarmaSpecFilter = function (clientConfig, jasmineEnv) {
476476
var createSpecFilter = function (config, jasmineEnv) {
477477
var karmaSpecFilter = new KarmaSpecFilter(config, jasmineEnv)
478478

479+
var originalSpecFilter = jasmineEnv.configuration().specFilter
479480
var specFilter = function (spec) {
480-
return karmaSpecFilter.matches(spec)
481+
return originalSpecFilter(spec) && karmaSpecFilter.matches(spec)
481482
}
482483

483484
return specFilter
@@ -502,7 +503,6 @@ function createStartFn (karma, jasmineEnv) {
502503
jasmineEnv = jasmineEnv || window.jasmine.getEnv()
503504

504505
jasmineConfig.specFilter = createSpecFilter(clientConfig, jasmineEnv)
505-
506506
jasmineEnv.configure(jasmineConfig)
507507

508508
window.jasmine.DEFAULT_TIMEOUT_INTERVAL = jasmineConfig.timeoutInterval ||

test/adapter.spec.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,17 +572,21 @@ describe('jasmine adapter', function () {
572572
name: 'test',
573573
id: 1
574574
}
575+
var mockConfiguration = {
576+
specFilter: jasmine.createSpy().and.returnValue(true)
577+
}
575578
mockJasmineEnv = {
576579
topSuite: () => {
577580
return {
578581
children: [mockSpecTest, mockSpecBar]
579582
}
580-
}
583+
},
584+
configuration: () => mockConfiguration
581585
}
582586
specs = mockJasmineEnv.topSuite().children
583587
})
584588

585-
describe(' getGrepSpecsToRun', function () {
589+
describe('getGrepSpecsToRun', function () {
586590
it('should not match without grep arg', function () {
587591
var karmaConfMock = {
588592
args: []
@@ -660,6 +664,16 @@ describe('jasmine adapter', function () {
660664
expect(specFilter(mockSpecTest)).toEqual(true)
661665
expect(specFilter(mockSpecBar)).toEqual(false)
662666
})
667+
it('should still allow a custom spec filter', function () {
668+
var karmaConfMock = {}
669+
mockJasmineEnv.configuration().specFilter.and.returnValue(false)
670+
var specFilter = createSpecFilter(karmaConfMock, mockJasmineEnv)
671+
expect(specFilter(mockSpecTest)).toEqual(false)
672+
expect(specFilter(mockSpecBar)).toEqual(false)
673+
expect(mockJasmineEnv.configuration().specFilter).toHaveBeenCalledTimes(2)
674+
expect(mockJasmineEnv.configuration().specFilter).toHaveBeenCalledWith(mockSpecTest)
675+
expect(mockJasmineEnv.configuration().specFilter).toHaveBeenCalledWith(mockSpecBar)
676+
})
663677
})
664678
})
665679
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = function (config) {
2+
config.set({
3+
frameworks: ['jasmine'],
4+
reporters: ['karma-jasmine'],
5+
6+
files: ['test/*.js'],
7+
8+
browsers: process.env.TRAVIS ? ['Firefox'] : ['Chrome'],
9+
10+
autoWatch: true,
11+
12+
plugins: [
13+
'karma-firefox-launcher',
14+
'karma-chrome-launcher',
15+
require.resolve('../../../')
16+
]
17+
})
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Users are able to set a custom specFilter themselves
2+
// karma-jasmine will allow them to do so.
3+
4+
jasmine.getEnv().configure({
5+
specFilter: function (spec) {
6+
return spec.getFullName() !== 'spec that fails'
7+
}
8+
})
9+
10+
describe('spec', () => {
11+
it('that fails', () => {
12+
fail('This spec should not run!')
13+
})
14+
15+
it('that succeeds', () => {
16+
expect(1).toBe(1)
17+
})
18+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy