Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Commit

Permalink
Merge pull request #379 from saguijs/use-chrome-headless-to-run-unit-…
Browse files Browse the repository at this point in the history
…tests-by-default

Use Chrome Headless by default to run unit tests 😑
  • Loading branch information
pirelenito authored Jul 19, 2017
2 parents 5947831 + 5042f7d commit 434fe28
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 21 deletions.
53 changes: 39 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
language: node_js
node_js:
- 6.11.1
- 8.1.4
sudo: false

before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash # until yarn is
- export PATH=$HOME/.yarn/bin:$PATH # pre-installed
- curl -o- -L https://yarnpkg.com/install.sh | bash # until yarn is
- export PATH=$HOME/.yarn/bin:$PATH # pre-installed

script:
- node bin/ci.js
env:
matrix:
- TEST_TYPE=lint_and_test_unit
- TEST_TYPE=integration_test
- TEST_TYPE=test_create_project_npm
- TEST_TYPE=test_create_project_yarn
- node bin/ci.js

matrix:
include:
- sudo: false
env: TEST_TYPE=lint_and_test_unit
node_js: 6.11.1

- sudo: false
env: TEST_TYPE=test_create_project_npm
node_js: 6.11.1

- sudo: false
env: TEST_TYPE=test_create_project_yarn
node_js: 6.11.1

# Test with Chrome available
- dist: trusty
addons:
chrome: stable
sudo: false
env:
- TEST_TYPE=integration_test
- BROWSER=Chrome
node_js: 6.11.1

# Test with PhantomJS available
- sudo: false
env:
- TEST_TYPE=integration_test
- BROWSER=PhantomJS
node_js: 6.11.1

cache:
directories:
- node_modules
- node_modules

deploy:
provider: npm
email: [email protected]
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ Sagui will automatically run every test file that follows this convention.

Under the hood it uses [Karma test runner](http://karma-runner.github.io/) to allow running the tests in the most diverse browsers and even through [Selenium](http://docs.seleniumhq.org/) (not natively).

By default Sagui uses [PhantomJS](http://phantomjs.org/) to run the tests headlessly. To **speed up installing the dependencies** (`npm install`) it is advisable to have PhantomJS [installed globally](https://github.com/Medium/phantomjs#using-phantomjs-from-disk) in the machine.
To run the tests Sagui uses [Chrome Headless](https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md), but it fallbacks to [PhantomJS](http://phantomjs.org/) if Chrome is not installed on the machine.

Make sure either of these browsers is installed to be able to run the tests:

- [Chrome](https://www.google.com/chrome/browser/desktop/index.html)
- [PhantomJS](https://github.com/Medium/phantomjs#using-phantomjs-from-disk)

To open the tests in a browser (or in multiple browsers!), simply follow the link Karma outputs when you start running the script `test:unit:watch`. Running them in a browser allows you to set breakpoints and debug your code properly. Note watch mode is necessary, else tests will stop running when finished.

Expand Down
4 changes: 2 additions & 2 deletions bin/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if (process.env.TEST_TYPE === 'integration_test') {
if (process.env.TEST_TYPE === 'test_create_project_npm') {
// # builds Sagui before installing
exec('npm run build', saguiPath)
exec('npm pack')
exec('npm pack --ignore-scripts')

var npmProjectPath = createTempFolder()

Expand All @@ -47,7 +47,7 @@ if (process.env.TEST_TYPE === 'test_create_project_npm') {
if (process.env.TEST_TYPE === 'test_create_project_yarn') {
// # builds Sagui before installing
exec('npm run build', saguiPath)
exec('npm pack')
exec('npm pack --ignore-scripts')

var yarnProjectPath = createTempFolder()

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@
"html-webpack-plugin": "^2.28.0",
"jasmine-core": "^2.5.2",
"karma": "^1.6.0",
"karma-chrome-launcher": "^2.0.0",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.1",
"karma-detect-browsers": "^2.2.5",
"karma-firefox-launcher": "^1.0.1",
"karma-jasmine": "^1.1.0",
"karma-mocha-reporter": "^2.2.3",
Expand All @@ -110,7 +111,6 @@
"node-sass": "^4.5.2",
"null-loader": "^0.1.1",
"parent-module": "^0.1.0",
"phantomjs-prebuilt": "^2.1.14",
"postcss-loader": "^1.3.3",
"prettier": "^1.3.1",
"raw-loader": "^0.5.1",
Expand Down
21 changes: 19 additions & 2 deletions src/configure-karma/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default (saguiConfig = {}, webpackConfig) => {
}

const buildStandardKarmaConfig = (saguiConfig, webpackConfig) => {
const { projectPath, watch, coverage } = saguiConfig
const { projectPath, watch, coverage, additionalKarmaConfig = {} } = saguiConfig

return {
// Webpack itself supports an array of configurations
Expand All @@ -26,7 +26,24 @@ const buildStandardKarmaConfig = (saguiConfig, webpackConfig) => {
...(coverage ? ['coverage'] : [])
],

frameworks: ['jasmine'],
frameworks: ['jasmine', 'detectBrowsers'],

detectBrowsers: {
enabled: additionalKarmaConfig.browsers && additionalKarmaConfig.browsers > 0,
usePhantomJS: true,

postDetection: function (availableBrowser) {
const isTravis = process.env['TRAVIS'] === 'true'
const isChromeAvailable = availableBrowser.indexOf('Chrome') > -1
const isTravisChromeAvailable = (process.env['TRAVIS_STACK_FEATURES'] || '').indexOf('google-chrome') !== -1

if (isChromeAvailable && (!isTravis || (isTravis && isTravisChromeAvailable))) {
return ['ChromeHeadless']
}

return ['PhantomJS']
}
},

// first run will have the full output and
// the next runs just output the summary and
Expand Down

0 comments on commit 434fe28

Please sign in to comment.