Skip to content

Commit

Permalink
feat (adapter): Use jasmine's new configure method (#224)
Browse files Browse the repository at this point in the history
Pass `client.jasmine` object directly to `jasmine.configure` method.
See: https://jasmine.github.io/api/3.3/Env.html

Closes: #221

BREAKING CHANGE:

`stopOnFailure`, which was previously documented in karma-jasmine's README, is
not configuration option for jasmine. Use `oneFailurePerSpec` instead.

Requires peerDependency Jasmine@^3.3.0
  • Loading branch information
Lalem001 authored and johnjbarton committed Nov 7, 2018
1 parent e604132 commit 6663e47
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 80 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module.exports = function(config) {
jasmine: {
random: true,
seed: '4321',
stopOnFailure: true,
oneFailurePerSpec: true,
failFast: true,
timeoutInterval: 1000
}
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
"grunt-karma": "2.x",
"grunt-npm": "0.0.2",
"karma": "2.x || ",
"jasmine-core": "~3.0.0",
"jasmine-core": "^3.3.0",
"karma-chrome-launcher": "1.x || ~0.2.2",
"karma-firefox-launcher": "1.x || ~0.1.7",
"load-grunt-tasks": "^3.4.1"
},
"peerDependencies": {
"jasmine-core": "*",
"jasmine-core": "^3.3.0",
"karma": "*"
},
"engines": {
Expand Down
24 changes: 11 additions & 13 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,26 @@ var KarmaSpecFilter = function (options) {
}

/**
* Configure jasmine specFilter
*
* This function is invoked from the wrapper.
* @see adapter.wrapper
*
* @param {Object} config The karma config
* @param {Object} jasmineEnv jasmine environment object
*/
var createSpecFilter = function (config, jasmineEnv) {
var specFilter = new KarmaSpecFilter({
var karmaSpecFilter = new KarmaSpecFilter({
filterString: function () {
return getGrepOption(config.args)
}
})

jasmineEnv.specFilter = function (spec) {
return specFilter.matches(spec.getFullName())
var specFilter = function (spec) {
return karmaSpecFilter.matches(spec.getFullName())
}

jasmineEnv.configure({ specFilter: specFilter })
}

/**
Expand All @@ -355,23 +362,14 @@ function createStartFn (karma, jasmineEnv) {

jasmineEnv = jasmineEnv || window.jasmine.getEnv()

setOption(jasmineConfig.stopOnFailure, jasmineEnv.throwOnExpectationFailure)
setOption(jasmineConfig.failFast, jasmineEnv.stopOnSpecFailure)
setOption(jasmineConfig.seed, jasmineEnv.seed)
setOption(jasmineConfig.random, jasmineEnv.randomizeTests)
jasmineEnv.configure(jasmineConfig)

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

jasmineEnv.addReporter(new KarmaReporter(karma, jasmineEnv))
jasmineEnv.execute()
}

function setOption (option, set) {
if (option != null && typeof set === 'function') {
set(option)
}
}
}

function indexOf (collection, find, i /* opt*/) {
Expand Down
84 changes: 23 additions & 61 deletions test/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,42 +366,14 @@ describe('jasmine adapter', function () {
jasmineEnv = new jasmine.Env()
})

it('should set random order', function () {
jasmineConfig.random = true
spyOn(jasmineEnv, 'randomizeTests')
it('should pass jasmineConfig directly to jasmine.configure', function () {
var configure = spyOn(jasmineEnv, 'configure')

createStartFn(tc, jasmineEnv)()

expect(jasmineEnv.randomizeTests).toHaveBeenCalledWith(true)
})

it('should set order seed', function () {
var seed = '4321'

jasmineConfig.seed = seed
spyOn(jasmineEnv, 'seed')
jasmineConfig.foo = 42

createStartFn(tc, jasmineEnv)()

expect(jasmineEnv.seed).toHaveBeenCalledWith(seed)
})

it('should set stopOnFailure', function () {
jasmineConfig.stopOnFailure = true
spyOn(jasmineEnv, 'throwOnExpectationFailure')

createStartFn(tc, jasmineEnv)()

expect(jasmineEnv.throwOnExpectationFailure).toHaveBeenCalledWith(true)
})

it('should set failFast', function () {
jasmineConfig.failFast = true
spyOn(jasmineEnv, 'stopOnSpecFailure')

createStartFn(tc, jasmineEnv)()

expect(jasmineEnv.stopOnSpecFailure).toHaveBeenCalledWith(true)
expect(configure).toHaveBeenCalledWith(jasmineConfig)
})

it('should change timeoutInterval', function () {
Expand All @@ -416,29 +388,6 @@ describe('jasmine adapter', function () {
jasmine.DEFAULT_TIMEOUT_INTERVAL = previousTimeoutInterval
})

it('should not set random order if client does not pass it', function () {
spyOn(jasmineEnv, 'randomizeTests')

createStartFn(tc, jasmineEnv)()

expect(jasmineEnv.randomizeTests).not.toHaveBeenCalled()
})

it('should not fail with failFast if the jasmineEnv does not support it', function () {
jasmineConfig.failFast = true
jasmineEnv.stopOnSpecFailure = null

expect(function () {
createStartFn(tc, jasmineEnv)()
}).not.toThrowError()
})

it('should not change timeoutInterval if client does not pass it', function () {
createStartFn(tc, jasmineEnv)()

expect(jasmine.DEFAULT_TIMEOUT_INTERVAL).toBe(jasmine.DEFAULT_TIMEOUT_INTERVAL)
})

it('should not fail if client does not set config', function () {
tc.config = null

Expand Down Expand Up @@ -601,18 +550,31 @@ describe('jasmine adapter', function () {
})

describe('createSpecFilter', function () {
var jasmineEnv

beforeEach(function () {
jasmineEnv = new jasmine.Env()
})

it('should create spec filter in jasmine', function () {
var jasmineEnvMock = {}
var karmaConfMock = {
args: ['--grep', 'test']
}
var specMock = {
getFullName: jasmine.createSpy('getFullName').and.returnValue('test')
}

createSpecFilter(karmaConfMock, jasmineEnvMock)
createSpecFilter(karmaConfMock, jasmineEnv)

var specFilter = jasmineEnv.configuration().specFilter

// Jasmine's default specFilter **always** returns true
// so test multiple possibilities

expect(specFilter({
getFullName: jasmine.createSpy('getFullName').and.returnValue('test')
})).toEqual(true)

expect(jasmineEnvMock.specFilter(specMock)).toEqual(true)
expect(specFilter({
getFullName: jasmine.createSpy('getFullName2').and.returnValue('foo')
})).toEqual(false)
})
})
})

0 comments on commit 6663e47

Please sign in to comment.