Skip to content

Instantly share code, notes, and snippets.

@craigmulligan
Created May 2, 2017 17:06
Show Gist options
  • Select an option

  • Save craigmulligan/510383d826ed59bfe0aef06ff710dc85 to your computer and use it in GitHub Desktop.

Select an option

Save craigmulligan/510383d826ed59bfe0aef06ff710dc85 to your computer and use it in GitHub Desktop.
script to start mock server before tests
var mockserver = require('mockserver-grunt'),
Mocha = require('mocha'),
fs = require('fs'),
path = require('path'),
Karma = require('karma').Server,
cfg = require('karma').config,
testType = process.argv[2]
var mockserverConfig = {
serverPort: process.env.RESIN_SUPERVISOR_PORT || 1080,
proxyPort: 1090,
verbose: true,
}
function isTestBrowser() {
return testType === 'browser'
}
function isTestUndefined() {
return testType === undefined
}
function killMock() {
mockserver.stop_mockserver(mockserverConfig)
}
process.on('exit', function() {
killMock()
})
process.on('uncaughtException', function() {
killMock()
})
// Run the tests.
mockserver.start_mockserver(mockserverConfig).then(function() {
console.log('mock Server started!')
if (!isTestBrowser() || isTestUndefined()) {
// Instantiate a Mocha instance.
var mocha = new Mocha()
var testDir = './test'
// Add each .js file to the mocha instance
fs.readdirSync(testDir).filter(function(file){
// Only keep the .js files
return file.substr(-8) === '.spec.js'
}).forEach(function(file){
mocha.addFile(
path.join(testDir, file)
)
})
var runner = mocha.run()
runner.on('end', function() {
if (!isTestBrowser()) {
// we're just testing node.
killMock()
}
})
}
if (isTestBrowser() || isTestUndefined()) {
var karmaConfig = cfg.parseConfig(path.resolve(__dirname, '../karma.conf.js'), { port: 1337 })
var karmaServer = new Karma(karmaConfig, function(exitCode) {
process.exit(exitCode)
})
karmaServer.start()
karmaServer.on('run_complete', function() {
karmaServer.stop()
})
}
}).catch(function(e) {
console.log('Mock Server Error: ', e)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment