Commit 0c1673bd authored by Eduardo Sanz García's avatar Eduardo Sanz García Committed by Eduardo

Added option browser launching for the test command

The purpose of this PR is to be able to easily run karma tests in other
browsers without manually editing the karma configuration or making
local copies of the karma configuration.

Additions:

- `yarn test --no-browser` to avoid launching the default browser.
  It is up to the developer to run the tests in whatever browser she/he
  chooses by navigating to http://localhost:9876/.

- `yarn test --browser <browser>` run the tests in a different
  browser/s instead of the default browser. Chrome launcher comes by
  default with karma. Firefox, Safari and others can be installed
  independently.

Modifications:

- `yarn gulp test-watch` has been substituted by `yarn test --watch` for consistency.
parent f99c7121
...@@ -23,18 +23,18 @@ dev: node_modules/.uptodate ...@@ -23,18 +23,18 @@ dev: node_modules/.uptodate
.PHONY: test .PHONY: test
test: node_modules/.uptodate test: node_modules/.uptodate
ifdef FILTER ifdef ARGS
yarn test --grep $(FILTER) yarn test $(ARGS)
else else
yarn test yarn test
endif endif
.PHONY: servetests .PHONY: servetests
servetests: node_modules/.uptodate servetests: node_modules/.uptodate
ifdef FILTER ifdef ARGS
node_modules/.bin/gulp test-watch --grep $(FILTER) node_modules/.bin/gulp --watch $(ARGS)
else else
node_modules/.bin/gulp test-watch node_modules/.bin/gulp --watch
endif endif
.PHONY: lint .PHONY: lint
......
...@@ -34,17 +34,34 @@ const IMAGES_DIR = 'build/images'; ...@@ -34,17 +34,34 @@ const IMAGES_DIR = 'build/images';
function parseCommandLine() { function parseCommandLine() {
commander commander
.option( .option(
'--grep [pattern]', '--grep <pattern>',
'Run only tests where filename matches a pattern' 'Run only tests where filename matches a regex pattern'
)
.option('--watch', 'Continuously run tests (default: false)', false)
.option('--browser <browser>', 'Run tests in browser of choice.')
.option(
'--no-browser',
"Don't launch default browser. Instead, navigate to http://localhost:9876/ to run the tests."
) )
.parse(process.argv); .parse(process.argv);
return { const { grep, watch, browser } = commander.opts();
grep: commander.opts().grep, const karmaOptions = {
grep: grep,
singleRun: !watch,
}; };
// browser option can be either false | undefined | string
if (browser === false) {
karmaOptions.browsers = null;
} else if (browser) {
karmaOptions.browsers = [browser];
}
return karmaOptions;
} }
const taskArgs = parseCommandLine(); const karmaOptions = parseCommandLine();
/** A list of all modules included in vendor bundles. */ /** A list of all modules included in vendor bundles. */
const vendorModules = Object.keys(vendorBundles.bundles).reduce(function ( const vendorModules = Object.keys(vendorBundles.bundles).reduce(function (
...@@ -361,13 +378,12 @@ gulp.task( ...@@ -361,13 +378,12 @@ gulp.task(
) )
); );
function runKarma({ singleRun }, done) { function runKarma(done) {
const karma = require('karma'); const karma = require('karma');
new karma.Server( new karma.Server(
{ {
configFile: path.resolve(__dirname, './src/karma.config.js'), configFile: path.resolve(__dirname, './src/karma.config.js'),
grep: taskArgs.grep, ...karmaOptions,
singleRun,
}, },
done done
).start(); ).start();
...@@ -377,9 +393,5 @@ function runKarma({ singleRun }, done) { ...@@ -377,9 +393,5 @@ function runKarma({ singleRun }, done) {
// Some (eg. a11y) tests rely on CSS bundles, so build these first. // Some (eg. a11y) tests rely on CSS bundles, so build these first.
gulp.task( gulp.task(
'test', 'test',
gulp.series('build-css', done => runKarma({ singleRun: true }, done)) gulp.series('build-css', done => runKarma(done))
);
gulp.task(
'test-watch',
gulp.series('build-css', done => runKarma({ singleRun: false }, done))
); );
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment