Does Karma have an initialization hook? - hook

Does Karma (0.12) have a hook which I can use to perform some initialization before running tests?

With karma 0.10 I've done the following:
created an helper.js file which has been added to the JS list in the karma configuration
module.exports = function(config) {
config.set({
...
files: [
// Helping Libraries
'vendor/jquery.js',
'helpers/helper.js',
...
],
...
the content of the helper is:
window.__karma__.loaded = function() {};
$(function () {
// do my stuff in here
// at the end of my stuff start karma:
window.__karma__.start();
});

Related

Gulp watch for specific file and generate other file

I have this code in my gulpfile.js
// Sass configuration
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('sass', function() {
gulp.src('*.scss')
.pipe(sass())
.pipe(gulp.dest(function(f) {
return f.base;
}))
});
gulp.task('default', ['sass'], function() {
gulp.watch('*.scss', ['sass']);
})
This basically watches all scss file.
My theme.scss contains:
#import "default"
#import "buttons"
I want to watch for a specific file i.e default.scss and buttons.scss and if those are modified I want to src in my theme.scss and regenerate theme.css
How do I modify the above code to achieve this?
Any help is appreciated.
If I understand your question, I think this might be a solution
gulp.task('default', function(){
gulp.watch(['default.scss', 'buttons.scss', ..., 'the-files-you-want-to-watch'], ['sass']);
});
If you want to just generate theme.scss when modifying the said files, you could add something like this, and add 'sass_theme' besides 'sass' in the gulp.watch above.
gulp.task('sass_theme', function() {
gulp.src('theme.scss')
.pipe(sass())
.pipe(gulp.dest(function(f) {
return f.base;
}))
});

Gulp and Browserify always giving "dest.write is not a function" error

I'm trying to utilize Browserify in my Gulp file, but it seems no matter how I set things up, it always throws an error that reads "dest.write is not a function". I originally started with this task using gulp-concat:
gulp.task('scripts', function()
{
return gulp.src(['src/shared/js/one.js', 'src/shared/js/two.js', 'src/shared/js/*.js'])
.pipe(browserify()
.pipe(concat('main.js'))
.pipe(gulp.dest('dist/js'))
.pipe(browserSync.stream());
});
When I commented out the browserify() line, everything worked fine. While Googling around for a solution to the error message, I found this page when someone linked to it, saying "the gulp docs rave about this solution" (here):
var gulp = require('gulp');
var browserify = require('browserify');
var transform = require('vinyl-transform');
var uglify = require('gulp-uglify');
gulp.task('browserify', function () {
var browserified = transform(function(filename) {
var b = browserify(filename);
return b.bundle();
});
return gulp.src(['./src/*.js'])
.pipe(browserified)
.pipe(uglify())
.pipe(gulp.dest('./dist'));
});
I dug further, and read that vinyl-transform doesn't work with Browserify in this way because there is no write stream, and the preferred method was to use vinyl-source-stream instead. I'm now currently trying to use this proposed solution, but still getting the error:
gulp.task('scripts', function()
{
return gulp.src(['src/shared/js/one.js', 'src/shared/js/two.js', 'src/shared/js/*.js'])
.pipe(browserify('src/shared/js/*.js', {
debug: true,
extensions: ['.js']
}))
.bundle()
.pipe(source('main.js'))
.pipe(buffer())
.pipe(gulp.dest('dist/js'))
.pipe(browserSync.stream());
});
Tweaking the browserify() reference in various ways has not changed anything. Can anyone tell me what I'm doing wrong?
Here's the solution that worked for me in exactly same situation.
I spent some time trying to make vinyl-transform work with browserify, without success, so had to revert to vinyl-source-stream and vinyl-buffer combination.
I used the following gist as a base reference for my code snippet:
https://gist.github.com/stoikerty/cfa49330a9609f6f8d2d
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var uglify = require('gulp-uglify');
gulp.task('browserify', function () {
var b = browserify({
entries: './js/test.js', // Only need initial file, browserify finds the deps
debug: true // Enable sourcemaps
});
return b.bundle()
.pipe(source('./js/test.js')) // destination file for browserify, relative to gulp.dest
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest('./dist'));
});

How to run protractor as a script and not as a child process or using a task runner?

I am wondering how I can run a protractor test as a script and not as a child process or from a task runner such as grunt and gulp. I am wanting to run the test suits in order when my sauce queuing application notifies the test runner I am building. This way, my tests do not conflict with my co-workers tests.
I am using node, so is there something like this?
var protractor = require('protractor');
protractor.run('path/to/conf', suites, callback);
protractor.on('message', callback)
protractor.on('error', callback)
protractor.end(callback);
It will not be possible. I tried to do that but by reading the protractor source code there is no way to perform this.
https://github.com/angular/protractor/blob/master/lib/launcher.js#L107
This function is called with your config as a json object, but as you can see it calls a bunch of process.exit, according to this it will not be possible to run this without at least forking your process.
My solution for programmatically call protractor is the following:
var npm = require('npm');
var childProcess = require('child_process');
var address = ...some address object
var args = ['--baseUrl', url.format(address)];
npm.load({}, function() {
var child = childProcess
.fork(path.join(npm.root, 'protractor/lib/cli'), args)
.on('close', function(errorCode) {
console.log('error code: ', errorCode);
});
process.on('SIGINT', child.kill);
});
const Launcher = require("protractor/built/launcher");
Launcher.init('path/to/conf');
const protractorFlake = require('protractor-flake'),
baseUrl = process.argv[2],
maxAttempts = process.argv[3];
if (process.argv.length > 2) {
console.info('Launching protractor with baseUrl: %s, maxAttempts: %d', baseUrl, maxAttempts);
protractorFlake({
maxAttempts: maxAttempts,
parser: 'multi',
protractorArgs: [
'./protractor.conf.js',
'--baseUrl',
baseUrl
]
}, function (status, output) {
process.exit(status);
});
} else {
console.error(`
Usage: protractor-wrapper <baseUrl>
`);
}

Gulp dest not working in Windows7

The setup
I've followed this tutorial for installing Gulp and configuring a gulpfile.js to concatenate and minify JavaScript files. I am using a Windows7 machine. When I run the gulp command, I get this output:
There are no errors, but gulp.dest() doesn't create the expected "dist/js/MATRIX.min.js".
Here is my gulpfile:
/* based on http://sixrevisions.com/web-performance/improve-website-speed/ */
var gulp = require('gulp');
var minifycss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var concatcss = require('gulp-concat-css');
var concat = require('gulp-concat');
var jshint = require('gulp-jshint');
var imagemin = require('gulp-imagemin'),
svgmin = require('gulp-svgmin');
//gulp.task('default', ['css', 'js', 'img', 'svg', 'html'], function () {});
gulp.task('default', ['js'], function () {});
// CSS concatenation + minification task
gulp.task('css', function() {
return gulp.src('sourcecode/css/*.css')
.pipe(concatcss('semeano.min.css'))
.pipe(minifycss())
.pipe(gulp.dest('dist/css'));
});
// JS linting + minification + concatenation task
gulp.task('js', function() {
return gulp.src([
'sourcecode/js/agregate/modernizr.custom.08680.min.js',
'sourcecode/js/agregate/jquery.js',
'sourcecode/js/agregate/jquery-ui.js',
'sourcecode/js/agregate/bootstrap.js',
'sourcecode/js/agregate/jquery.dataTables.js',
'sourcecode/js/agregate/dataTables.tableTools.js',
'sourcecode/js/agregate/ZeroClipboard.js',
'sourcecode/js/agregate/DT_bootstrap.js',
'sourcecode/js/agregate/MATRIX_main.js'
])
.pipe(jshint())
.pipe(jshint.reporter("default"))
.pipe(uglify())
.pipe(concat('MATRIX.min.js'))
.pipe(gulp.dest('dist/js'));
});
// Image optimization task
gulp.task('img', function () {
return gulp.src('sourcecode/img/*.*')
.pipe(imagemin())
.pipe(gulp.dest('dist/img'));
});
// SVG optimization task
gulp.task('svg', function () {
return gulp.src('sourcecode/svg/*.svg')
.pipe(svgmin())
.pipe(gulp.dest('dist/svg'));
});
What I've tried
Figuring "I know how to do things", I did some research and came across this "Gulp suddenly not working anymore after npm update" issue, so I tried this suggestion:
Run: "npm install glob#4.2.2" //no problem
Run: "npm install" //now I get a bunch of errors:
What do I need to do at this point to get this working in my Windows environment?
From looking at the first screenshot and your code snippet, it looks like gulp.src() isn't finding the files you want to process. Check to see if you've got any typos like aggregate?
As for that github issue, it refers to an old version of gulp. Be sure you're using the latest version of gulp 3.9.0.

Sails js running tests

I am trying to run my sails unit tests (using mocha and istanbul)
when running
grunt test
I get the errors
1) "before all" hook
2) "after all" hook
0 passing (5s)
2 failing
1) "before all" hook:
Error: timeout of 2000ms exceeded
at null.<anonymous> (/vagrant/node_modules/mocha/lib/runnable.js:157:19)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
2) "after all" hook:
ReferenceError: sails is not defined
the setup does not seem to find my Sails...but doing
which sails
I get
/usr/local/node/node-default/bin/sails
and running sails lift works fine
Here is the mocha tests file in my project
//boostrap.test.js
var Sails = require('sails');
before(function(done) {
Sails.lift({
// configuration for testing purposes
}, function(err, sails) {
if (err) return done(err);
// here you can load fixtures, etc.
done(err, sails);
});
});
after(function(done) {
// here you can clear fixtures, etc.
sails.lower(done);
});
First, you have a typo issue, you have :
var Sails = require('sails');
But you try to use
sails.lower(done);
Instead of
Sails.lower(done);
Then, you need to define a project-wide "migrate" setting configured (http://sailsjs.org/#/documentation/concepts/ORM/model-settings.html?q=migrate)
You just have to edit the config/models.js file.
You can simply uncomment the following line :
//migrate: 'alter'
Enjoy :)
I haven't seen the actual code of your test case, so I just list a couple of possibilities here.
If you run your test asynchronously, I think you need to set the timeout attribute in mocha to some number bigger. I wrote three very simple test cases, but I always get the same problem as you described. I have tried 10000ms, 20000ms and 30000ms. When I increase it to 90000ms, all the test cases are passed. So I think it's because sails really need some time to lift before the test begins.
Another thought is that have you set the 'migrate' attribute in your environment configuration file? If not, sails lift command will wait for your input to select "drop","alter" or "safe" before proceed, which will also cause the timeout problem in test.
Its not like that I know your problem but it might help you find out yours if I write down some snipets of my working tests in sails. I am including the package.json file as well so that you know my version of each module.
here is the relevant modules in package.json:
rootproject/test/mocha.opts , this probably what u need
--timeout 5000
rootproject/package.json
{
...
"dependencies": {
...
"sails": "0.9.7",
"sails-disk": "~0.9.0",
"sails-memory": "^0.9.1",
"sails-mongo": "^0.9.7",
...
},
"devDependencies": {
"mocha": "^1.20.1",
"barrels": "^0.0.3",
"supervisor": "^0.6.0"
},
"scripts": {
"start": "node app.js",
"debug": "node debug app.js",
"test": "PORT=9999 NODE_ENV=test mocha -R spec -b --recursive"
},
"main": "app.js",
...
}
I have also added another model adapter to be used by the tests at rootproject/config/adapters.js
test: {
module : 'sails-memory'
},
rootproject/test/index.js
var assert = require('assert');
var Sails = require('sails');
var barrels = require('barrels');
var fixtures;
var userTest = require('./controllers/User.js');
//... other test controllers ...
//in case you need different simulations per controller you could add a custom Response in your test controller and use them instead
var defaultCustomRequest = function(urlParams, bodyParams/*whatever else u need*/) {
//simulates the sails request
//create an object here, based on how u use the req object in your sails controllers
//.eg
return {
params: urlParams,
body: bodyParams
};
}
//in case you need different simulations per controller or per method you could add multiple custom Responses in your test controller and use them instead
var defaultCustomResponse = function(expectedCode, dataExpecting/*whatever else u need*/) {
//simulates the sails res which I was using like this: res.status(httpCode).json({somedata})
//use the assert function here to validate
//.eg
status: function (responseCode){
assert(expectedCode === responseCode, 'Expected status is ' + expectedCode + ' but ' + responseCode + ' was returned.');
return this;
},
json: function (responseData){
//assert your responseData with your expectedData
return this;
}
return this;
},
}
before(function (done) {
// Lift Sails with test database
Sails.lift({
log: {
level: 'error'
},
adapters: {
default: 'test'
}
}, function(err, sails) {
if (err)
return done(err);
// Load fixtures
barrels.populate(function(err) {
done(err, sails);
});
// Save original objects in `fixtures` variable
fixtures = barrels.objects;
});
});
// Global after hook
after(function (done) {
//console.log('fixtures loaded: ' + JSON.stringify(fixtures));
sails.lower(done);
});
describe('User', function() { userTest.run(fixtures, customRequest, customRespose); });
//or describe('User', function() { userTest.run(fixtures); });
//... other test controllers ...
rootproject/test/controllers/User.js
var assert = require('assert');
var UserController = require('../../api/controllers/UserController.js');
module.exports = {
run: function(fixtures, customReq, customRes) {
describe('create', function(customReq, customRes) {
it ('should create a few user entries', function() {
if (!customReq) customReq = {/*custom request for user create*/};
if (!customRes) customRes = {/*custom response for user create*/};
//call your controllers for testing here
//.eg
UserController.create(
new req({},
{email: 'someemail#gmail.com', password: 'password'})
,new res(201,
{email: 'someemail#gmail.com', password: null});
UserController.create(
new req({},
{email: 'someemail#gmail.com', password: 'password'})
,new res(400,
{error: true});
);
});
});
//... more guns
}
};
As you can see on the package.json I used sails 0.9.7, therefore there might be needed additional changes for 0.10.x versions. .eg instead of config/adapters.js there are the config/models.js and config/connections.js that are used.
The accepted answer is correct for one of the errors.
For the timeout error simply add
this.timeout(5000);
on the line after
before(function(done) {

Resources