Skip to content

Commit

Permalink
Build system: improve build for development workflow (#7778)
Browse files Browse the repository at this point in the history
* Build system: improve build for development workflow

This adds a "test-only" gulp task that runs only tests (without clean/lint) and makes the single-spec test environment consistent with the whole-suite case.

* Build system: remove tests from 'serve-fast' task; add 'serve-and-test' task
  • Loading branch information
dgirardi authored Dec 3, 2021
1 parent 613bb08 commit f50b7ec
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 78 deletions.
161 changes: 87 additions & 74 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,6 @@ function viewReview(done) {

viewReview.displayName = 'view-review';

// Watch Task with Live Reload
function watch(done) {
var mainWatcher = gulp.watch([
'src/**/*.js',
'modules/**/*.js',
'test/spec/**/*.js',
'!test/spec/loaders/**/*.js'
]);
var loaderWatcher = gulp.watch([
'loaders/**/*.js',
'test/spec/loaders/**/*.js'
]);

connect.server({
https: argv.https,
port: port,
host: FAKE_SERVER_HOST,
root: './',
livereload: true
});

mainWatcher.on('all', gulp.series(clean, gulp.parallel(lint, 'build-bundle-dev', test)));
loaderWatcher.on('all', gulp.series(lint));
done();
};

function makeDevpackPkg() {
var cloned = _.cloneDeep(webpackConfig);
cloned.devtool = 'source-map';
Expand Down Expand Up @@ -247,60 +221,68 @@ function bundle(dev, moduleArr) {
// If --browsers is given, browsers can be chosen explicitly. e.g. --browsers=chrome,firefox,ie9
// If --notest is given, it will immediately skip the test task (useful for developing changes with `gulp serve --notest`)

function test(done) {
if (argv.notest) {
done();
} else if (argv.e2e) {
let wdioCmd = path.join(__dirname, 'node_modules/.bin/wdio');
let wdioConf = path.join(__dirname, 'wdio.conf.js');
let wdioOpts;

if (argv.file) {
wdioOpts = [
wdioConf,
`--spec`,
`${argv.file}`
]
} else {
wdioOpts = [
wdioConf
];
}
function testTaskMaker(options = {}) {
['watch', 'e2e', 'file', 'browserstack', 'notest'].forEach(opt => {
options[opt] = options[opt] || argv[opt];
})

// run fake-server
const fakeServer = spawn('node', ['./test/fake-server/index.js', `--port=${FAKE_SERVER_PORT}`]);
fakeServer.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
fakeServer.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
return function test(done) {
if (options.notest) {
done();
} else if (options.e2e) {
let wdioCmd = path.join(__dirname, 'node_modules/.bin/wdio');
let wdioConf = path.join(__dirname, 'wdio.conf.js');
let wdioOpts;

if (options.file) {
wdioOpts = [
wdioConf,
`--spec`,
`${options.file}`
]
} else {
wdioOpts = [
wdioConf
];
}

execa(wdioCmd, wdioOpts, { stdio: 'inherit' })
.then(stdout => {
// kill fake server
fakeServer.kill('SIGINT');
done();
process.exit(0);
})
.catch(err => {
// kill fake server
fakeServer.kill('SIGINT');
done(new Error(`Tests failed with error: ${err}`));
process.exit(1);
// run fake-server
const fakeServer = spawn('node', ['./test/fake-server/index.js', `--port=${FAKE_SERVER_PORT}`]);
fakeServer.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
fakeServer.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
} else {
var karmaConf = karmaConfMaker(false, argv.browserstack, argv.watch, argv.file);

var browserOverride = helpers.parseBrowserArgs(argv);
if (browserOverride.length > 0) {
karmaConf.browsers = browserOverride;
}
execa(wdioCmd, wdioOpts, { stdio: 'inherit' })
.then(stdout => {
// kill fake server
fakeServer.kill('SIGINT');
done();
process.exit(0);
})
.catch(err => {
// kill fake server
fakeServer.kill('SIGINT');
done(new Error(`Tests failed with error: ${err}`));
process.exit(1);
});
} else {
var karmaConf = karmaConfMaker(false, options.browserstack, options.watch, options.file);

new KarmaServer(karmaConf, newKarmaCallback(done)).start();
var browserOverride = helpers.parseBrowserArgs(argv);
if (browserOverride.length > 0) {
karmaConf.browsers = browserOverride;
}

new KarmaServer(karmaConf, newKarmaCallback(done)).start();
}
}
}

const test = testTaskMaker();

function newKarmaCallback(done) {
return function (exitCode) {
if (exitCode) {
Expand Down Expand Up @@ -377,6 +359,35 @@ function startFakeServer() {
});
}

// Watch Task with Live Reload
function watchTaskMaker(options = {}) {
if (options.livereload == null) {
options.livereload = true;
}
options.alsoWatch = options.alsoWatch || [];

return function watch(done) {
var mainWatcher = gulp.watch([
'src/**/*.js',
'modules/**/*.js',
].concat(options.alsoWatch));

connect.server({
https: argv.https,
port: port,
host: FAKE_SERVER_HOST,
root: './',
livereload: options.livereload
});

mainWatcher.on('all', options.task());
done();
}
}

const watch = watchTaskMaker({alsoWatch: ['test/**/*.js'], task: () => gulp.series(clean, gulp.parallel(lint, 'build-bundle-dev', test))});
const watchFast = watchTaskMaker({livereload: false, task: () => gulp.series('build-bundle-dev')});

// support tasks
gulp.task(lint);
gulp.task(watch);
Expand All @@ -389,7 +400,8 @@ gulp.task('build-bundle-dev', gulp.series(makeDevpackPkg, gulpBundle.bind(null,
gulp.task('build-bundle-prod', gulp.series(makeWebpackPkg, gulpBundle.bind(null, false)));

// public tasks (dependencies are needed for each task since they can be ran on their own)
gulp.task('test', gulp.series(clean, lint, test));
gulp.task('test-only', test);
gulp.task('test', gulp.series(clean, lint, 'test-only'));

gulp.task('test-coverage', gulp.series(clean, testCoverage));
gulp.task(viewCoverage);
Expand All @@ -400,7 +412,8 @@ gulp.task('build', gulp.series(clean, 'build-bundle-prod'));
gulp.task('build-postbid', gulp.series(escapePostbidConfig, buildPostbid));

gulp.task('serve', gulp.series(clean, lint, gulp.parallel('build-bundle-dev', watch, test)));
gulp.task('serve-fast', gulp.series(clean, gulp.parallel('build-bundle-dev', watch)));
gulp.task('serve-fast', gulp.series(clean, gulp.parallel('build-bundle-dev', watchFast)));
gulp.task('serve-and-test', gulp.series(clean, gulp.parallel('build-bundle-dev', watchFast, testTaskMaker({watch: true}))));
gulp.task('serve-fake', gulp.series(clean, gulp.parallel('build-bundle-dev', watch), injectFakeServerEndpointDev, test, startFakeServer));

gulp.task('default', gulp.series(clean, makeWebpackPkg));
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ module.exports = function(codeCoverage, browserstack, watchMode, file) {
var webpackConfig = newWebpackConfig(codeCoverage);
var plugins = newPluginsArray(browserstack);

var files = file ? ['test/helpers/prebidGlobal.js', file] : ['test/test_index.js'];
var files = file ? ['test/test_deps.js', file] : ['test/test_index.js'];
// This file opens the /debug.html tab automatically.
// It has no real value unless you're running --watch, and intend to do some debugging in the browser.
if (watchMode) {
Expand Down
1 change: 1 addition & 0 deletions test/helpers/prebidGlobal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
window.$$PREBID_GLOBAL$$ = (window.$$PREBID_GLOBAL$$ || {});
window.$$PREBID_GLOBAL$$.installedModules = (window.$$PREBID_GLOBAL$$.installedModules || []);
window.$$PREBID_GLOBAL$$.cmd = window.$$PREBID_GLOBAL$$.cmd || [];
window.$$PREBID_GLOBAL$$.que = window.$$PREBID_GLOBAL$$.que || [];
3 changes: 3 additions & 0 deletions test/test_deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('test/helpers/prebidGlobal.js');
require('test/mocks/adloaderStub.js');
require('test/mocks/xhr.js');
4 changes: 1 addition & 3 deletions test/test_index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require('test/helpers/prebidGlobal.js');
require('test/mocks/adloaderStub.js');
require('test/mocks/xhr.js');
require('./test_deps.js');

var testsContext = require.context('.', true, /_spec$/);
testsContext.keys().forEach(testsContext);
Expand Down

0 comments on commit f50b7ec

Please sign in to comment.