Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Add support for providing mochaOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ctdio committed Sep 9, 2017
1 parent 4cec98a commit 3868b61
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 34 deletions.
37 changes: 13 additions & 24 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"koa": "^2.3.0",
"koa-bodyparser": "^4.2.0",
"koa-mount": "^3.0.0",
"koa-path-router": "^0.1.1",
"koa-path-router": "^1.0.0",
"koa-static": "^4.0.1",
"lasso": "^2.11.16",
"lasso-babel-transform": "^1.0.1",
Expand Down
16 changes: 15 additions & 1 deletion src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const Server = require('./Server')

const _prepareTestPageInput = require('./utils/prepareTestPageInput')

const DEFAULT_MOCHA_OPTIONS = {
ui: 'bdd',
reporter: 'spec',
useColors: true
}

class TestRunner extends EventEmitter {
constructor (options) {
super()
Expand All @@ -36,12 +42,15 @@ class TestRunner extends EventEmitter {
const {
testFiles,
lassoConfig,
mochaOptions,

// test options
_instrumentCode,
_randomizeOutputDir
} = options

this._mochaOptions = mochaOptions

assert(Array.isArray(testFiles), 'testFiles must be provided as an array')

// use output dir from lasso config if present
Expand Down Expand Up @@ -108,6 +117,10 @@ class TestRunner extends EventEmitter {
const server = this._server
await server.listen()

// config mocha options
const mochaOptions = Object.assign({},
DEFAULT_MOCHA_OPTIONS, this._mochaOptions)

const browser = this._browser = await puppeteer.launch()
const page = await browser.newPage()

Expand All @@ -120,7 +133,8 @@ class TestRunner extends EventEmitter {
// for cleaner output
page.setViewport({ width: columns, height: columns })

await page.goto(`http://localhost:${server.getPort()}`)
await page.goto(`http://localhost:${server.getPort()}` +
`#${JSON.stringify({ mochaOptions })}`)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/test-page/run-tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Mocha, WebSocket, superagent, location } = window
const { mocha, Mocha, WebSocket, superagent, location } = window
const { hostname, port } = location

// end test if not started within one second
Expand Down Expand Up @@ -28,7 +28,7 @@ socket.addEventListener('open', () => {
oldConsoleLog(...args)
}

const runner = window.mocha.run()
const runner = mocha.run()

let testsPassed = true

Expand Down
10 changes: 5 additions & 5 deletions src/pages/test-page/setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO: Make these options configurable
const { mocha, location } = window

const { mocha } = window
// parse query to get mocha options
const hashData = location.hash.substring(1)
const { mochaOptions } = JSON.parse(hashData)

mocha.setup('bdd')
mocha.reporter('spec')
mocha.useColors(true)
mocha.setup(mochaOptions)
52 changes: 51 additions & 1 deletion test/unit/TestRunner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const EventEmitter = require('events')

const uuid = require('uuid')

const DEFAULT_MOCHA_OPTIONS = {
ui: 'bdd',
reporter: 'spec',
useColors: true
}

class MockPage extends EventEmitter {
async goto (url) {
await superagent.get(url).send()
Expand Down Expand Up @@ -102,7 +108,51 @@ test('should direct puppeteer page to "goto" the root path of the ' +
const server = testRunner._server
const port = server.getPort()

sandbox.assert.calledWith(gotoSpy, `http://localhost:${port}`)
sandbox.assert.calledWith(gotoSpy, sandbox.match((url) =>
url.startsWith(`http://localhost:${port}#`)))
})

async function _testMochaOptions (context, inputOptions, expectedOptions) {
const {
sandbox,
mockPage,
TestRunner
} = context

const gotoSpy = sandbox.spy(mockPage, 'goto')

const testRunner = new TestRunner({
testFiles: [],
mochaOptions: inputOptions
})

await testRunner.start()

const server = testRunner._server
const port = server.getPort()

sandbox.assert.calledWith(gotoSpy, `http://localhost:${port}` +
`#${JSON.stringify(expectedOptions)}`)
}

test('should apply default mochaOptions as part of query in url passed to "goto" ' +
'the root path of the server that is launched', async (t) => {
t.plan(0)

await _testMochaOptions(t.context, null, { mochaOptions: DEFAULT_MOCHA_OPTIONS })
})

test('should apply mochaOptions as part of query in url passed to "goto" ' +
'the root path of the server that is launched', async (t) => {
t.plan(0)

const mochaOptions = {
ui: 'tdd',
reporter: 'nyan',
useColors: false
}

await _testMochaOptions(t.context, mochaOptions, { mochaOptions })
})

test('should fail to start if unable to start puppeteer', async (t) => {
Expand Down

0 comments on commit 3868b61

Please sign in to comment.