Skip to content

Commit

Permalink
Merge pull request #146 from maksimr/patch-145
Browse files Browse the repository at this point in the history
feat: Support --random and --seed flags
  • Loading branch information
dignifiedquire authored Dec 9, 2016
2 parents 5418bba + 0b59d69 commit 3a5051d
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 9 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ module.exports = function(config) {
}
```

If you want to pass configuration options directly to jasmine you can do this in the following way

```js
module.exports = function(config) {
config.set({
client: {
jasmine: {
random: true,
seed: '4321',
stopOnFailure: true
}
}
})
}
```

----

For more information on Karma see the [homepage].
Expand Down
17 changes: 16 additions & 1 deletion src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ function KarmaReporter (tc, jasmineEnv) {
})
}

this.jasmineDone = function () {
this.jasmineDone = function (result) {
result = result || {}
tc.complete({
order: result.order,
coverage: window.__coverage__
})
}
Expand Down Expand Up @@ -319,11 +321,24 @@ var createSpecFilter = function (config, jasmineEnv) {
function createStartFn (karma, jasmineEnv) {
// This function will be assigned to `window.__karma__.start`:
return function () {
var clientConfig = karma.config || {}
var jasmineConfig = clientConfig.jasmine || {}

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

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

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

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

function indexOf (collection, find, i /* opt*/) {
Expand Down
96 changes: 88 additions & 8 deletions test/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ describe('jasmine adapter', function () {
id: 'spec0',
description: 'contains spec with an expectation',
queueableFn: {
fn: function () {}
fn: function () {
}
},
getSpecName: function () {
return 'A suite contains spec with an expectation'
Expand All @@ -69,12 +70,14 @@ describe('jasmine adapter', function () {

env.describe('one', function () {
env.describe('nested', function () {
env.it('should do something', function () {})
env.it('should do something', function () {
})
})
})

env.describe('two', function () {
env.it('should not do anything', function () {})
env.it('should not do anything', function () {
})
})

reporter.jasmineStarted({totalSpecsDefined: 2})
Expand All @@ -84,7 +87,7 @@ describe('jasmine adapter', function () {
karma.result.and.callFake(function (result) {
expect(result.id).toBe(spec.id)
expect(result.description).toBe('contains spec with an expectation')
expect(result.suite).toEqual([ 'Parent Suite', 'Child Suite' ])
expect(result.suite).toEqual(['Parent Suite', 'Child Suite'])
expect(result.success).toBe(true)
expect(result.skipped).toBe(false)
})
Expand Down Expand Up @@ -215,15 +218,15 @@ describe('jasmine adapter', function () {

it('should remove special top level suite from result', function () {
karma.result.and.callFake(function (result) {
expect(result.suite).toEqual([ 'Child Suite' ])
expect(result.suite).toEqual(['Child Suite'])
})

reporter.suiteStarted({
id: 'suite0',
description: 'Jasmine_TopLevel_Suite'
})
reporter.suiteStarted(suite.result)
spec.result.failedExpectations.push({ stack: 'stack' })
spec.result.failedExpectations.push({stack: 'stack'})

reporter.specDone(spec.result)

Expand All @@ -248,6 +251,35 @@ describe('jasmine adapter', function () {
expect(karma.result).toHaveBeenCalled()
})

it('should report order on complete', function () {
var result = {
order: {
random: true,
seed: '4321'
}
}

spyOn(karma, 'complete')

reporter.jasmineDone(result)

expect(karma.complete).toHaveBeenCalledWith({
order: result.order,
coverage: undefined
})
})

it('should not fail if result is undefined', function () {
spyOn(karma, 'complete')

reporter.jasmineDone()

expect(karma.complete).toHaveBeenCalledWith({
order: undefined,
coverage: undefined
})
})

describe('time', function () {
afterEach(function () {
jasmine.clock().uninstall()
Expand Down Expand Up @@ -306,16 +338,64 @@ describe('jasmine adapter', function () {
describe('startFn', function () {
var tc
var jasmineEnv
var jasmineConfig

beforeEach(function () {
tc = new Karma(new MockSocket(), {})
jasmineConfig = {}

tc = new Karma(new MockSocket(), {}, null, null, {search: ''})
tc.config = {jasmine: jasmineConfig}

spyOn(tc, 'info')
spyOn(tc, 'complete')
spyOn(tc, 'result')

jasmineEnv = new jasmine.Env()
createStartFn(tc, jasmineEnv)
})

it('should set random order', function () {
jasmineConfig.random = true
spyOn(jasmineEnv, 'randomizeTests')

createStartFn(tc, jasmineEnv)()

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

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

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

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 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 if client does not set config', function () {
tc.config = null

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

Expand Down

0 comments on commit 3a5051d

Please sign in to comment.