Skip to content

Commit

Permalink
feat(spec-filter): allow custom specFilter
Browse files Browse the repository at this point in the history
Allow users to define their own jasmine `specFilter`.

```js
jasmine.getEnv().configure({
  specFilter: function(spec) {
    // ...
  }
})
```
  • Loading branch information
nicojs authored and Jonathan Ginsburg committed Jun 16, 2022
1 parent 58d5d25 commit b73dbd6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
26 changes: 26 additions & 0 deletions README.md
Expand Up @@ -81,6 +81,32 @@ run a subset of the full set of specs. Complete sharding support needs to be
done in the process that calls karma, and would need to support test result
integration across shards.

## Custom spec filter

Providing a [custom spec filter](https://jasmine.github.io/api/edge/Configuration#specFilter) is also supported.

Example:

```js
// Users are able to set a custom specFilter themselves

jasmine.getEnv().configure({
specFilter: function (spec) {
return spec.getFullName() === 'spec that succeeds'
}
})

describe('spec', () => {
it('that fails', () => {
fail('This spec should not run!')
})

it('that succeeds', () => {
expect(1).toBe(1)
})
})
```

---

For more information on Karma see the [homepage](https://karma-runner.github.io/).
4 changes: 2 additions & 2 deletions src/adapter.js
Expand Up @@ -476,8 +476,9 @@ var KarmaSpecFilter = function (clientConfig, jasmineEnv) {
var createSpecFilter = function (config, jasmineEnv) {
var karmaSpecFilter = new KarmaSpecFilter(config, jasmineEnv)

var originalSpecFilter = jasmineEnv.configuration().specFilter
var specFilter = function (spec) {
return karmaSpecFilter.matches(spec)
return originalSpecFilter(spec) && karmaSpecFilter.matches(spec)
}

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

jasmineConfig.specFilter = createSpecFilter(clientConfig, jasmineEnv)

jasmineEnv.configure(jasmineConfig)

window.jasmine.DEFAULT_TIMEOUT_INTERVAL = jasmineConfig.timeoutInterval ||
Expand Down
18 changes: 16 additions & 2 deletions test/adapter.spec.js
Expand Up @@ -572,17 +572,21 @@ describe('jasmine adapter', function () {
name: 'test',
id: 1
}
var mockConfiguration = {
specFilter: jasmine.createSpy().and.returnValue(true)
}
mockJasmineEnv = {
topSuite: () => {
return {
children: [mockSpecTest, mockSpecBar]
}
}
},
configuration: () => mockConfiguration
}
specs = mockJasmineEnv.topSuite().children
})

describe(' getGrepSpecsToRun', function () {
describe('getGrepSpecsToRun', function () {
it('should not match without grep arg', function () {
var karmaConfMock = {
args: []
Expand Down Expand Up @@ -660,6 +664,16 @@ describe('jasmine adapter', function () {
expect(specFilter(mockSpecTest)).toEqual(true)
expect(specFilter(mockSpecBar)).toEqual(false)
})
it('should still allow a custom spec filter', function () {
var karmaConfMock = {}
mockJasmineEnv.configuration().specFilter.and.returnValue(false)
var specFilter = createSpecFilter(karmaConfMock, mockJasmineEnv)
expect(specFilter(mockSpecTest)).toEqual(false)
expect(specFilter(mockSpecBar)).toEqual(false)
expect(mockJasmineEnv.configuration().specFilter).toHaveBeenCalledTimes(2)
expect(mockJasmineEnv.configuration().specFilter).toHaveBeenCalledWith(mockSpecTest)
expect(mockJasmineEnv.configuration().specFilter).toHaveBeenCalledWith(mockSpecBar)
})
})
})
})
18 changes: 18 additions & 0 deletions test/fixtures/custom-filter/karma.conf.js
@@ -0,0 +1,18 @@
module.exports = function (config) {
config.set({
frameworks: ['jasmine'],
reporters: ['karma-jasmine'],

files: ['test/*.js'],

browsers: process.env.TRAVIS ? ['Firefox'] : ['Chrome'],

autoWatch: true,

plugins: [
'karma-firefox-launcher',
'karma-chrome-launcher',
require.resolve('../../../')
]
})
}
18 changes: 18 additions & 0 deletions test/fixtures/custom-filter/test/custom-filter.spec.js
@@ -0,0 +1,18 @@
// Users are able to set a custom specFilter themselves
// karma-jasmine will allow them to do so.

jasmine.getEnv().configure({
specFilter: function (spec) {
return spec.getFullName() !== 'spec that fails'
}
})

describe('spec', () => {
it('that fails', () => {
fail('This spec should not run!')
})

it('that succeeds', () => {
expect(1).toBe(1)
})
})

0 comments on commit b73dbd6

Please sign in to comment.