2021-04-17 20:07:23 +05:30
|
|
|
/* eslint-disable no-inner-declarations, no-param-reassign */
|
2021-06-08 01:23:25 +05:30
|
|
|
const path = require('path');
|
2018-10-15 14:42:47 +05:30
|
|
|
const chalk = require('chalk');
|
|
|
|
const argumentsParser = require('commander');
|
2021-04-17 20:07:23 +05:30
|
|
|
const glob = require('glob');
|
|
|
|
const webpack = require('webpack');
|
2019-07-31 22:56:46 +05:30
|
|
|
const IS_EE = require('./helpers/is_ee_env');
|
2021-04-17 20:07:23 +05:30
|
|
|
const webpackConfig = require('./webpack.config.js');
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
const ROOT_PATH = path.resolve(__dirname, '..');
|
2021-04-17 20:07:23 +05:30
|
|
|
const SPECS_PATH = /^(?:\.[\\/])?(ee[\\/])?spec[\\/]javascripts[\\/]/;
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
function exitError(message) {
|
2018-10-15 14:42:47 +05:30
|
|
|
console.error(chalk.red(`\nError: ${message}\n`));
|
|
|
|
process.exit(1);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
function exitWarn(message) {
|
|
|
|
console.error(chalk.yellow(`\nWarn: ${message}\n`));
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function exit(message, isError = true) {
|
|
|
|
const fn = isError ? exitError : exitWarn;
|
|
|
|
|
|
|
|
fn(message);
|
|
|
|
}
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
// disable problematic options
|
|
|
|
webpackConfig.entry = undefined;
|
|
|
|
webpackConfig.mode = 'development';
|
2018-11-08 19:23:39 +05:30
|
|
|
webpackConfig.optimization.nodeEnv = false;
|
2018-10-15 14:42:47 +05:30
|
|
|
webpackConfig.optimization.runtimeChunk = false;
|
|
|
|
webpackConfig.optimization.splitChunks = false;
|
|
|
|
|
|
|
|
// use quicker sourcemap option
|
2017-09-10 17:25:29 +05:30
|
|
|
webpackConfig.devtool = 'cheap-inline-source-map';
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
// set BABEL_ENV to indicate when we're running code coverage
|
|
|
|
webpackConfig.plugins.push(
|
|
|
|
new webpack.DefinePlugin({
|
|
|
|
'process.env.BABEL_ENV': JSON.stringify(process.env.BABEL_ENV || process.env.NODE_ENV || null),
|
2019-07-07 11:18:12 +05:30
|
|
|
}),
|
2018-12-05 23:21:45 +05:30
|
|
|
);
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
const options = argumentsParser
|
|
|
|
.option('--no-fail-on-empty-test-suite')
|
2018-10-15 14:42:47 +05:30
|
|
|
.option(
|
|
|
|
'-f, --filter-spec [filter]',
|
|
|
|
'Filter run spec files by path. Multiple filters are like a logical OR.',
|
|
|
|
(filter, memo) => {
|
|
|
|
memo.push(filter, filter.replace(/\/?$/, '/**/*.js'));
|
|
|
|
return memo;
|
|
|
|
},
|
2019-07-07 11:18:12 +05:30
|
|
|
[],
|
2018-10-15 14:42:47 +05:30
|
|
|
)
|
2019-09-04 21:01:54 +05:30
|
|
|
.parse(process.argv);
|
|
|
|
|
|
|
|
const specFilters = options.filterSpec;
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
const createContext = (specFiles, regex, suffix) => {
|
|
|
|
const newContext = specFiles.reduce((context, file) => {
|
|
|
|
const relativePath = file.replace(SPECS_PATH, '');
|
|
|
|
context[file] = `./${relativePath}`;
|
|
|
|
return context;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
webpackConfig.plugins.push(
|
|
|
|
new webpack.ContextReplacementPlugin(regex, path.join(ROOT_PATH, suffix), newContext),
|
|
|
|
);
|
|
|
|
};
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
if (specFilters.length) {
|
2018-10-15 14:42:47 +05:30
|
|
|
// resolve filters
|
2021-03-08 18:12:59 +05:30
|
|
|
let filteredSpecFiles = specFilters.map((filter) =>
|
2018-10-15 14:42:47 +05:30
|
|
|
glob
|
|
|
|
.sync(filter, {
|
|
|
|
root: ROOT_PATH,
|
|
|
|
matchBase: true,
|
|
|
|
})
|
2021-04-17 20:07:23 +05:30
|
|
|
.filter((filePath) => filePath.endsWith('spec.js')),
|
2018-10-15 14:42:47 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
// flatten
|
|
|
|
filteredSpecFiles = Array.prototype.concat.apply([], filteredSpecFiles);
|
|
|
|
|
|
|
|
// remove duplicates
|
|
|
|
filteredSpecFiles = [...new Set(filteredSpecFiles)];
|
|
|
|
|
|
|
|
if (filteredSpecFiles.length < 1) {
|
2019-09-04 21:01:54 +05:30
|
|
|
const isError = options.failOnEmptyTestSuite;
|
|
|
|
|
|
|
|
exit('Your filter did not match any test files.', isError);
|
2018-10-15 14:42:47 +05:30
|
|
|
}
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
if (!filteredSpecFiles.every((file) => SPECS_PATH.test(file))) {
|
2019-09-04 21:01:54 +05:30
|
|
|
exitError('Test files must be located within /spec/javascripts.');
|
2018-10-15 14:42:47 +05:30
|
|
|
}
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const CE_FILES = filteredSpecFiles.filter((file) => !file.startsWith('ee'));
|
2021-04-17 20:07:23 +05:30
|
|
|
createContext(CE_FILES, /[^e]{2}[\\/]spec[\\/]javascripts$/, 'spec/javascripts');
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const EE_FILES = filteredSpecFiles.filter((file) => file.startsWith('ee'));
|
2021-04-17 20:07:23 +05:30
|
|
|
createContext(EE_FILES, /ee[\\/]spec[\\/]javascripts$/, 'ee/spec/javascripts');
|
2018-10-15 14:42:47 +05:30
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
// Karma configuration
|
2021-04-17 20:07:23 +05:30
|
|
|
module.exports = (config) => {
|
2018-03-17 18:26:18 +05:30
|
|
|
process.env.TZ = 'Etc/UTC';
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
const fixturesPath = `tmp/tests/frontend/fixtures${IS_EE ? '-ee' : ''}`;
|
|
|
|
const staticFixturesPath = 'spec/frontend/fixtures/static';
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
const karmaConfig = {
|
2017-08-17 22:00:37 +05:30
|
|
|
basePath: ROOT_PATH,
|
2018-05-09 12:01:36 +05:30
|
|
|
browsers: ['ChromeHeadlessCustom'],
|
2018-12-05 23:21:45 +05:30
|
|
|
client: {
|
|
|
|
color: !process.env.CI,
|
|
|
|
},
|
2017-09-10 17:25:29 +05:30
|
|
|
customLaunchers: {
|
|
|
|
ChromeHeadlessCustom: {
|
|
|
|
base: 'ChromeHeadless',
|
|
|
|
displayName: 'Chrome',
|
|
|
|
flags: [
|
|
|
|
// chrome cannot run in sandboxed mode inside a docker container unless it is run with
|
|
|
|
// escalated kernel privileges (e.g. docker run --cap-add=CAP_SYS_ADMIN)
|
|
|
|
'--no-sandbox',
|
2019-09-04 21:01:54 +05:30
|
|
|
// https://bugs.chromium.org/p/chromedriver/issues/detail?id=2870
|
|
|
|
'--enable-features=NetworkService,NetworkServiceInProcess',
|
2017-09-10 17:25:29 +05:30
|
|
|
],
|
2018-05-09 12:01:36 +05:30
|
|
|
},
|
2017-09-10 17:25:29 +05:30
|
|
|
},
|
2017-08-17 22:00:37 +05:30
|
|
|
frameworks: ['jasmine'],
|
|
|
|
files: [
|
|
|
|
{ pattern: 'spec/javascripts/test_bundle.js', watched: false },
|
2019-10-12 21:52:04 +05:30
|
|
|
{ pattern: `${fixturesPath}/**/*`, included: false },
|
|
|
|
{ pattern: `${staticFixturesPath}/**/*`, included: false },
|
2017-08-17 22:00:37 +05:30
|
|
|
],
|
2019-10-12 21:52:04 +05:30
|
|
|
proxies: {
|
|
|
|
'/fixtures/': `/base/${fixturesPath}/`,
|
|
|
|
'/fixtures/static/': `/base/${staticFixturesPath}/`,
|
|
|
|
},
|
2017-08-17 22:00:37 +05:30
|
|
|
preprocessors: {
|
|
|
|
'spec/javascripts/**/*.js': ['webpack', 'sourcemap'],
|
2019-07-07 11:18:12 +05:30
|
|
|
'ee/spec/javascripts/**/*.js': ['webpack', 'sourcemap'],
|
2017-08-17 22:00:37 +05:30
|
|
|
},
|
2019-07-07 11:18:12 +05:30
|
|
|
reporters: ['mocha'],
|
2017-08-17 22:00:37 +05:30
|
|
|
webpack: webpackConfig,
|
|
|
|
webpackMiddleware: { stats: 'errors-only' },
|
2019-07-07 11:18:12 +05:30
|
|
|
plugins: [
|
|
|
|
'karma-chrome-launcher',
|
|
|
|
'karma-coverage-istanbul-reporter',
|
|
|
|
'karma-jasmine',
|
|
|
|
'karma-junit-reporter',
|
|
|
|
'karma-mocha-reporter',
|
|
|
|
'karma-sourcemap-loader',
|
|
|
|
'karma-webpack',
|
|
|
|
],
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
if (process.env.CI) {
|
2019-07-07 11:18:12 +05:30
|
|
|
karmaConfig.reporters.push('junit');
|
2018-12-05 23:21:45 +05:30
|
|
|
karmaConfig.junitReporter = {
|
|
|
|
outputFile: 'junit_karma.xml',
|
|
|
|
useBrowserName: false,
|
|
|
|
};
|
2019-07-07 11:18:12 +05:30
|
|
|
} else {
|
|
|
|
// ignore 404s in local environment because we are not fixing them and they bloat the log
|
|
|
|
function ignore404() {
|
|
|
|
return (request, response /* next */) => {
|
|
|
|
response.writeHead(404);
|
|
|
|
return response.end('NOT FOUND');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
karmaConfig.middleware = ['ignore-404'];
|
|
|
|
karmaConfig.plugins.push({
|
|
|
|
'middleware:ignore-404': ['factory', ignore404],
|
|
|
|
});
|
2018-12-05 23:21:45 +05:30
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if (process.env.BABEL_ENV === 'coverage' || process.env.NODE_ENV === 'coverage') {
|
|
|
|
karmaConfig.reporters.push('coverage-istanbul');
|
|
|
|
karmaConfig.coverageIstanbulReporter = {
|
2020-07-28 23:09:34 +05:30
|
|
|
reports: ['html', 'text-summary', 'cobertura'],
|
2017-08-17 22:00:37 +05:30
|
|
|
dir: 'coverage-javascript/',
|
|
|
|
subdir: '.',
|
2018-05-09 12:01:36 +05:30
|
|
|
fixWebpackSourcePaths: true,
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
2017-09-10 17:25:29 +05:30
|
|
|
karmaConfig.browserNoActivityTimeout = 60000; // 60 seconds
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.DEBUG) {
|
|
|
|
karmaConfig.logLevel = config.LOG_DEBUG;
|
|
|
|
process.env.CHROME_LOG_FILE = process.env.CHROME_LOG_FILE || 'chrome_debug.log';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.CHROME_LOG_FILE) {
|
|
|
|
karmaConfig.customLaunchers.ChromeHeadlessCustom.flags.push('--enable-logging', '--v=1');
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
config.set(karmaConfig);
|
|
|
|
};
|