Skip to content
This repository has been archived by the owner on Mar 14, 2022. It is now read-only.

Commit

Permalink
Disable silent mode when building sass for tests. (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
notlee authored May 8, 2018
1 parent 8375418 commit c0b524d
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 167 deletions.
34 changes: 18 additions & 16 deletions config/karma.config.browserstack.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const karmaBaseConfig = require('./karma.config');
const { getBaseKarmaConfig } = require('./karma.config');
const constants = require('karma').constants;

const customLaunchers = {
// If browser_version is not set, uses latest stable version
Expand Down Expand Up @@ -65,20 +66,21 @@ const customLaunchers = {

const browsers = Object.keys(customLaunchers);

module.exports = function (config) {
module.exports.getBrowserStackKarmaConfig = function () {
return getBaseKarmaConfig().then(karmaBaseConfig => {
const karmaConfig = Object.assign(
{},
karmaBaseConfig,
{
browsers,
browserStack: {
startTunnel: true // let BrowserStack connect to our local server
},
customLaunchers,
logLevel: constants.LOG_DISABLE
}
);

const karmaConfig = Object.assign(
{},
karmaBaseConfig,
{
browsers,
browserStack: {
startTunnel: true // let BrowserStack connect to our local server
},
customLaunchers,
logLevel: config.LOG_DISABLE
}
);

config.set(karmaConfig);
return karmaConfig;
});
};
26 changes: 14 additions & 12 deletions config/karma.config.chrome.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const karmaBaseConfig = require('./karma.config');
const { getBaseKarmaConfig } = require('./karma.config');
const constants = require('karma').constants;

module.exports = function (config) {
module.exports.getChromeKarmaConfig = function () {
return getBaseKarmaConfig().then(karmaBaseConfig => {
const karmaConfig = Object.assign(
{},
karmaBaseConfig,
{
browsers: ['ChromeHeadless'],
logLevel: constants.LOG_DISABLE
}
);

const karmaConfig = Object.assign(
{},
karmaBaseConfig,
{
browsers: ['ChromeHeadless'],
logLevel: config.LOG_DISABLE
}
);

config.set(karmaConfig);
return karmaConfig;
});
};
163 changes: 85 additions & 78 deletions config/karma.config.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,92 @@
const process = require('process');
const path = require('path');
const webpackConfig = require('./webpack.config.dev');
const fileHelpers = require('../lib/helpers/files');

// https://github.com/webpack/webpack/issues/3324#issuecomment-289720345
delete webpackConfig.bail;
module.exports = {

// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: process.cwd(),

browserDisconnectTimeout: 60 * 1000, // default 2000
browserDisconnectTolerance: 3, // default 0
browserNoActivityTimeout: 60 * 1000, // default 10000

// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadless'],

client: {
// Capture all console output and pipe it to the terminal.
captureConsole: false
},

captureTimeout: 60 * 2000, // default 60000,

// enable / disable colors in the output (reporters and logs)
colors: true,

concurrency: 1, // default Infinity,

// list of files to exclude
exclude: [],

// list of files / patterns to load in the browser
files: [
'test/*.js',
'test/**/*.js',
'main.scss'
],

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'sinon'],

plugins: [
'karma-mocha',
'karma-sinon',
'karma-webpack',
'karma-browserstack-launcher',
'karma-chrome-launcher',
'karma-sourcemap-loader',
'karma-scss-preprocessor'
],

// web server port
port: 9876,

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/*.js': ['webpack', 'sourcemap'],
'test/**/*.js': ['webpack', 'sourcemap'],
'main.scss': ['scss']
},
scssPreprocessor: {
options: {
includePaths: [process.cwd(), path.join(process.cwd(), 'bower_components')]
}
},

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,

webpack: webpackConfig,

// Hide webpack output logging
webpackMiddleware: {
noInfo: true
}

module.exports.getBaseKarmaConfig = function() {
return Promise.all([fileHelpers.getModuleName(), fileHelpers.readIfExists(path.resolve('main.scss'))]).then(values => {
const moduleName = values[0];
const mainScssContent = values[1];
return {
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: process.cwd(),

browserDisconnectTimeout: 60 * 1000, // default 2000
browserDisconnectTolerance: 3, // default 0
browserNoActivityTimeout: 60 * 1000, // default 10000

// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadless'],

client: {
// Capture all console output and pipe it to the terminal.
captureConsole: false
},

captureTimeout: 60 * 2000, // default 60000,

// enable / disable colors in the output (reporters and logs)
colors: true,

concurrency: 1, // default Infinity,

// list of files to exclude
exclude: [],

// list of files / patterns to load in the browser
files: [
'test/*.js',
'test/**/*.js',
'main.scss'
],

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'sinon'],

plugins: [
'karma-mocha',
'karma-sinon',
'karma-webpack',
'karma-browserstack-launcher',
'karma-chrome-launcher',
'karma-sourcemap-loader',
'karma-scss-preprocessor'
],

// web server port
port: 9876,

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/*.js': ['webpack', 'sourcemap'],
'test/**/*.js': ['webpack', 'sourcemap'],
'main.scss': ['scss']
},
scssPreprocessor: {
options: {
file: '',
data: `$${moduleName}-is-silent: false; ${mainScssContent}`,
includePaths: [process.cwd(), path.join(process.cwd(), 'bower_components')]
}
},

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,

webpack: webpackConfig,

// Hide webpack output logging
webpackMiddleware: {
noInfo: true
}
};
});
};
13 changes: 9 additions & 4 deletions lib/helpers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ function getBuildFolderPath(cwd) {
}

function requireIfExists(filePath) {
return readIfExists(filePath)
.then(file => {
return file ? JSON.parse(file) : undefined;
});
}

function readIfExists(filePath) {
return fileExists(filePath)
.then(exists => {
if (exists) {
return readFile(filePath, 'utf-8')
.then(file => {
return JSON.parse(file);
});
return readFile(filePath, 'utf-8');
} else {
return undefined;
}
Expand Down Expand Up @@ -171,6 +175,7 @@ function sassSupportsSilent(files, cwd) {
});
}

module.exports.readIfExists = readIfExists;
module.exports.getBuildFolderPath = getBuildFolderPath;
module.exports.getMainSassPath = getMainSassPath;
module.exports.getMainJsPath = getMainJsPath;
Expand Down
99 changes: 50 additions & 49 deletions lib/tasks/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,72 @@ const constructPolyfillUrl = require('../helpers/construct-polyfill-url');
const Listr = require('listr');
const denodeify = require('denodeify');
const deglob = denodeify(require('deglob'));
const { getBrowserStackKarmaConfig } = require('../../config/karma.config.browserstack.js');
const { getChromeKarmaConfig } = require('../../config/karma.config.chrome.js');

const karmaTest = function (config, task) {
return constructPolyfillUrl()
.then(polyfillUrl => {
return new Promise((resolve, reject) => {
const getCustomKarmaConfig = config.browserstack ? getBrowserStackKarmaConfig : getChromeKarmaConfig;
return Promise.all([constructPolyfillUrl(), getCustomKarmaConfig()]).then(values => {
const polyfillUrl = values[0];

const cfg = require('karma').config;
const path = require('path');
// Add default Karma config to custom Karma config.
const customKarmaConfig = values[1];
const cfg = require('karma').config;
Object.assign(customKarmaConfig, { basePath: config.cwd });
const karmaConfig = cfg.parseConfig(null, customKarmaConfig);

const karmaConfig = config.browserstack ?
cfg.parseConfig(path.join(__dirname, '../../config/karma.config.browserstack.js'), {basePath: config.cwd})
: cfg.parseConfig(path.join(__dirname, '../../config/karma.config.chrome.js'), { basePath: config.cwd });
return new Promise((resolve, reject) => {
const debug = Boolean(config.debug);

const debug = Boolean(config.debug);

if (debug) {
karmaConfig.singleRun = false;
}

const { reporter, errors } = require('../plugins/listr-karma')();
if (debug) {
karmaConfig.singleRun = false;
}

karmaConfig.files.unshift(polyfillUrl);
karmaConfig.plugins.unshift(reporter);
karmaConfig.reporters = ['listr'];
const { reporter, errors } = require('../plugins/listr-karma')();

const Server = require('karma').Server;
const server = new Server(karmaConfig, exitCode => {
if (exitCode !== 0) {
reject(new Error(`Failed Karma tests: ${'\n\n' + errors.join('\n\n')}`));
} else {
resolve();
}
});
karmaConfig.files.unshift(polyfillUrl);
karmaConfig.plugins.unshift(reporter);
karmaConfig.reporters = ['listr'];

server.on('browser_start', function (browser) {
task.title = `Starting tests on ${browser.name}`;
});
const Server = require('karma').Server;
const server = new Server(karmaConfig, exitCode => {
if (exitCode !== 0) {
reject(new Error(`Failed Karma tests: ${'\n\n' + errors.join('\n\n')}`));
} else {
resolve();
}
});

server.on('browser_register', function (browser) {
task.title = `Running tests on ${browser.name}`;
});
server.on('browser_start', function (browser) {
task.title = `Starting tests on ${browser.name}`;
});

server.on('browser_complete', function (browser) {
task.title = `Completed tests on ${browser.name}`;
});
server.on('browser_register', function (browser) {
task.title = `Running tests on ${browser.name}`;
});

server.on('browser_error', function (browser, error) {
task.title = `Error with ${browser.name}`;
reject(new Error(`Error connecting to ${browser.name}: ${error.toString()}`));
});
server.on('browser_complete', function (browser) {
task.title = `Completed tests on ${browser.name}`;
});

server.on('browser_register', function (browser) {
if (debug) {
task.title = 'Running tests, visit http://localhost:9876/ to debug';
} else {
task.title = `Opening ${browser.name}`;
}
});
server.on('browser_error', function (browser, error) {
task.title = `Error with ${browser.name}`;
reject(new Error(`Error connecting to ${browser.name}: ${error.toString()}`));
});

server.on('browser_register', function (browser) {
if (debug) {
task.title = 'Running tests, visit http://localhost:9876/ to debug';
} else {
task.title = `Opening ${browser.name}`;
}
});

server.start(karmaConfig);
server.start(karmaConfig);

task.title = 'Starting Karma server';
});
task.title = 'Starting Karma server';
});
});
};

module.exports = (cfg) => {
Expand Down
Loading

0 comments on commit c0b524d

Please sign in to comment.