-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christoffer Åström
committed
Nov 2, 2017
1 parent
a51f70e
commit 003d5b4
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* eslint global-require: 0, no-console: 0, import/no-unresolved: 0, import/no-extraneous-dependencies: 0, import/no-dynamic-require: 0, max-len: 0 */ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const globby = require('globby'); | ||
const findUp = require('find-up'); | ||
const options = require('./options'); | ||
const Mocha = require('mocha'); | ||
|
||
process.on('unhandledRejection', (err) => { | ||
console.error(`Promise Rejection:${err}`); | ||
}); | ||
|
||
function runTests(files, mochaOpts) { | ||
return (resolve) => { | ||
const mocha = new Mocha(mochaOpts); | ||
files.forEach(f => mocha.addFile(f)); | ||
mocha.run((failures) => { | ||
resolve(failures); | ||
}); | ||
}; | ||
} | ||
function run(files, mochaOpts) { | ||
return new Promise(runTests(files, mochaOpts)); | ||
} | ||
|
||
const puppet = { | ||
command: ['puppeteer [options]', 'puppet'], | ||
desc: 'Run tests with puppeteer', | ||
builder(yargs) { | ||
return yargs | ||
.usage('puppeteer [options]') | ||
.options(options) | ||
.config('config', (configPath) => { | ||
let foundConfigPath = configPath; | ||
if (!fs.existsSync(configPath)) { | ||
foundConfigPath = findUp.sync(options.config.default); | ||
} | ||
let config = {}; | ||
try { | ||
config = require(foundConfigPath); | ||
} catch (_) { } //eslint-disable-line | ||
return config; | ||
}); | ||
}, | ||
handler(argv) { | ||
let puppeteer; | ||
try { | ||
puppeteer = require('puppeteer'); | ||
} catch (_) { | ||
console.log('Could not load puppeteer'); | ||
const p = `${path.resolve(process.cwd())}/node_modules/puppeteer`; | ||
console.log(`Trying: ${p}`); | ||
try { | ||
puppeteer = require(p); | ||
} catch (__) { | ||
console.log('Puppeteer could not be found by after-work.js! Please verify that it has been added as a devDependencies in your package.json'); | ||
process.exit(1); | ||
} | ||
} | ||
const files = globby.sync(argv.glob); | ||
if (!files.length) { | ||
console.log('No test files found for:', argv.glob); | ||
process.exit(1); | ||
} | ||
(async function launchAndRun() { | ||
const browser = await puppeteer.launch(argv.chrome); | ||
global.browser = browser; | ||
global.page = await browser.newPage(); | ||
const failures = await run(files, argv.mocha); | ||
process.exit(failures); | ||
}()); | ||
}, | ||
}; | ||
|
||
module.exports = puppet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = { | ||
config: { | ||
description: 'Path to config file', | ||
type: 'string', | ||
default: 'aw.config.js', | ||
}, | ||
glob: { | ||
description: 'Glob pattern', | ||
default: ['test/**/*.spec.js'], | ||
type: 'array', | ||
}, | ||
'mocha.enableTimeouts': { | ||
description: 'Enable timeouts', | ||
default: false, | ||
type: 'boolean', | ||
}, | ||
'chrome.headless': { | ||
description: 'Run chrome headless', | ||
default: false, | ||
type: 'boolean', | ||
}, | ||
}; |