I have a basic AngularJS app and want to have all my terminal command run with gulp tasks eg $ gulp dev for the development server and $ gulp unitTest for testing etc.
I have installed Gulp as per the Docs using $ npm install --save-dev gulp with my gulpfile.js in the root of the project file. I have also done the same for karma's install and config file.
It is worth stating now that I want all the npm installs tagged with --save for easily move the project around the office and servers.
When it comes to adding the task to Gulp I have to us a relative (to the karma module) path for the configFile option to find the config but then It does not find the tests.
the following gulpfile.js produces the error ERROR [config]: File karma.conf.js does not exist!
var gulp = require('gulp'),
// ....
karma = require('karma').Server;
gulp.task('test', function(done) {
var karmaServerOptions = {
configFile: 'karma.conf.js', // works if relative path from ./node_modules/karma/lib/config.js
singleRun: true
};
karma.start(
karmaServerOptions,
function(exitStatus) {
done(exitStatus ? 'There are failing tests' : undefined);
}
);
});
karma.conf.js:
// Karma configuration
// Generated on Thu Aug 06 2015 13:38:12 GMT+0100 (BST)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
// '**/*js',
'node_modules/angular/angular.js',
'app/**/*.js',
// 'unitTests/**/*Spec.js',
// 'unitTests/**/*spec.js'
'unitTests/**/*.js'
],
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
})
}
note: The files array is a bit of a mess as it still has some, but not all, of my experiments in it.
See gulp task can't find karma.conf.js for an explantation about __dirname.
Or use:
var Server = require('karma').Server
gulp.task('test', function (done) {
new Server({
configFile: require('path').resolve('karma.conf.js'),
singleRun: true
}, done).start();
});
Related
I have installed the npm package karma-junit-reporter at version 2.0.1.
my karma.conf.js file is :
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '#angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-junit-reporter'),
require('karma-coverage-istanbul-reporter'),
require('#angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false, // leave Jasmine Spec Runner output visible in browser
captureConsole: true
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, './coverage/ClientApp'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml', 'junit'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true,
// the default configuration
junitReporter: {
outputDir: 'test', // results will be saved as $outputDir/$browserName.xml
outputFile: 'junit.xml', // if included, results will be saved as $outputDir/$browserName/$outputFile
//suite: '', // suite will become the package name attribute in xml testsuite element
//useBrowserName: false, // add browser name to report and classes names
//nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
//classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
//properties: {}, // key value pair of properties to add to the <properties> section of the report
//xmlVersion: null // use '1' if reporting to be per SonarQube 6.2 XML format
}
});
};
When I run:
ng test --reporters junit
I receive the following error:
can not load reporter "junit", it is not registered! Perhaps you are missing some plugin?
[karma]: No captured browser, open http://localhost:9876/
Ok. I found the answer by chance. Really weird, but this worked. I needed to explicitly mention the config file :
ng test --karma-config=karma.conf.js --reporters junit
it now works even though I was running the ng test command from the same directory as my karma.conf.js file
How do you get composer to install dependencies correctly whenusing a gulp build?
My build process set up that outputs to a set location, either ../sites/www/public_html or ../sites/dev/public_html dependent on if an environment argument is passed to a gulp task. These locations essentially mirror my remote host.
I'm wanting to automate composer installs, updates and optimisation to output the correct vendor files in either ../sites/www/vendor or ../sites/dev/vendor whenever the build is initially run or to just optimise based on any watched php files being changed.
My build folder has the following structure:
source/
bower.json
composer.json
composer.lock
gulpfile.json
package.json
My example composer.json has the following:
{
"name": "mycomposer/mycomposer",
"version": "1.0.0",
"autoload": {
"psr-4" : {
"mycomposer\\": "public_html/app/mycomposer"
}
},
"require": {
"rollbar/rollbar": "^1.3",
"vlucas/phpdotenv": "^2.4.0"
}
}
I have tried a gulp-compose and task to install the composer libraries and to run dumpautoload for local first party libraries
gulp.task('composer', function() {
var dest = argv.live ? 'www' : 'devsite',
env = '../sites/' + dest + '/public_html';
$.composer('config vendor-dir ' + env.replace('public_html', 'vendor') );
$.composer({
"no-ansi": true,
"no-dev": true,
"no-interaction": true,
"no-progress": true,
"no-scripts": true,
"optimize-autoloader": true
});
$.composer('dumpautoload ', {
optimize: true
});
});
When the task is complete I'm finding is that the $baseDir variable references the build directory
Expected
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
Output
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname(dirname(dirname($vendorDir))).'/mybuild';
Is this something I can achieve, or should I really be running composer separately from my build process?
Thanks
I had a similar problem.
I use gulp-composer package.
gulpfile.js
const composer = require('gulp-composer');
gulp.task('composer-deployed', async function() {
let opts = {
"working-dir": 'my-path-to-composer.json',
"self-install": true, // false for my case
optimize: true,
"classmap-authoritative": true
};
composer("dumpautoload", opts);
});
In my case, I use composer installed globaly but you can choose the bin path with bin: 'path-to-composer.phar' in opts{}.
Hi i'm trying to run some gulp task on netlify for building Hugo web.
I wonder how to run serial gulp task on netlify,
by the way this is my gulpfile.js
var gulp = require('gulp');
var removeEmptyLines = require('gulp-remove-empty-lines');
var prettify = require('gulp-html-prettify');
var rm = require( 'gulp-rm' );
var minifyInline = require('gulp-minify-inline');
gulp.task('tojson', function () {
gulp.src('public/**/*.html')
.pipe(removeEmptyLines())
.pipe(gulp.dest('public/./'));
});
gulp.task('htmlClean', function () {
gulp.src('public/**/*.html')
.pipe(removeEmptyLines({
removeComments: true
}))
.pipe(gulp.dest('public/./'));
});
gulp.task('templates', function() {
gulp.src('public/**/*.html')
.pipe(prettify({indent_char: ' ', indent_size: 2}))
.pipe(gulp.dest('public/./'))
});
gulp.task('minify-inline', function() {
gulp.src('public/**/*.html')
.pipe(minifyInline())
.pipe(gulp.dest('public/./'))
});
where should i put the command to run all my gulps task in Netlify?
There are two places to setup your build commands in Netlify.
Admin Option
Put your commands in the online admin under the Settings section of your site and go to Build & Deploy (Deploy settings) and change the Build command:
Netlify Config file (netlify.toml) Option
Edit/add a netlify.toml file to the root of your repository and put your build commands into the context you want to target.
netlify.toml
# global context
[build]
publish = "public"
command = "gulp build"
# build a preview (optional)
[context.deploy-preview]
command = "gulp build-preview"
# build a branch with debug (optional)
[context.branch-deploy]
command = "gulp build-debug"
NOTE:
The commands can be any valid command string. Serializing gulp commands would work fine if you do not want to create a gulp sequence to run them. In example, gulp htmlClean && hugo && gulp tojson would be a valid command.
Commands in the netlify.toml will overwrite the site admin command.
You can string your tasks together like this:
add another plugin with NPM:
https://www.npmjs.com/package/run-sequence
var runSequence = require('run-sequence');
gulp.task('default', function (callback) {
runSequence(['tojson', 'htmlClean', 'templates', 'minify-inline'],
callback
)
})
Then run $ gulp
There's a section on run-sequence on this page that will help:
https://css-tricks.com/gulp-for-beginners/
I am trying to use minimist with gulp like this...
var minimist = require('minimist'),
...
knownOptions = {
string: ['env'],
boolean: ['watch'],
default: {
env: process.env.NODE_ENV || '',
watch: false
}
},
options = minimist(process.argv.slice(2), knownOptions),
install = require("gulp-install");
console.log("The options are "+process.argv.slice(2));
console.log("The watch is "+options.watch);
but when I run gulp clean -watch true I still see...
The options are clean,-watch,true
The watch is false
Why is it still false?
UPDATE
this works gulp clean --watch true but can't I do it with 1 slash instead of 2?
I believe (and tested) that since you are invoking the gulp cli, the flags are parsed from gulp itself which stops the execution and prompts you to the "Usage" cli manual.
Just for the record, all of the following will evaluate watch=true:
gulp clean --watch true
gulp clean --watch
gulp clean --watch=true
My karma test runner isn't automatically quitting after tests have finished, even though my config has singleRun set to true and I'm not auto-watching files, which should make the test runner run once, then quit according to the docs.
module.exports = function(config) {
config.set({
basePath: '',
browsers: ['PhantomJS'],
frameworks: ['browserify', 'jasmine'],
files: [
{ pattern: 'test/*.js', watched: false }
],
preprocessors: {
'static/js/src/*.js': ['browserify'],
'test/*.js': ['browserify']
},
browserify: {
debug: true,
transform: [["babelify", { "presets": ["es2015"] }]]
},
colors: true,
reporters: ['progress'],
singleRun: true,
autoWatch: false
});
};
When run via my gulp test command:
gulp.task('test', function(done) {
new Karma({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, done).start();
});
the tests complete:
[09:18:38] Using gulpfile ~/static-projects/tic-tac-toe-es6/gulpfile.js
[09:18:38] Starting 'test'...
04 02 2016 09:18:40.502:INFO [framework.browserify]: bundle built
04 02 2016 09:18:40.509:INFO [karma]: Karma v0.13.19 server started at http://localhost:9876/
04 02 2016 09:18:40.523:INFO [launcher]: Starting browser PhantomJS
04 02 2016 09:18:41.157:INFO [PhantomJS 1.9.8 (Linux 0.0.0)]: Connected on socket /#xIZCPzrCyB2xljZ7AAAA with id 64233425
PhantomJS 1.9.8 (Linux 0.0.0): Executed 9 of 9 SUCCESS (0.042 secs / 0.003 secs)
[09:18:41] Finished 'test' after 3.1 s
However, I have to manually quit the test runner via Ctrl + c. What am I doing wrong?
I'm having this same problem. There are various threads that elude to this problem with various fixes claimed for this:
https://github.com/karma-runner/gulp-karma/issues/3
https://github.com/karma-runner/karma/issues/1035
However, as far as I can tell this is still an issue (or maybe it's resurfaced?). The only way I've found to successfully execute karma from grunt is by spawning a child process to start karma.
var spawn = require('child_process').spawn;
gulp.task('test', function(done) {
spawn('karma', ['start', __dirname + '/karma.conf.js'], { stdio : 'inherit' });
});