ng-test-utils
Advanced tools
Comparing version 0.0.8 to 0.0.9
var gulp = require('gulp'); | ||
//var git = require('nodegit'); | ||
//var clone = git.Clone.clone; | ||
var exec = require('child_process').exec; | ||
var clean = require('gulp-clean'); | ||
var gutils = require('gulp-util'); | ||
var gutil = require('gulp-util'); | ||
var istanbul = require('gulp-istanbul'); | ||
var mocha = require('gulp-mocha'); | ||
var jshint = require('gulp-jshint'); | ||
var jscs = require('gulp-jscs'); | ||
gulp.task('cover', function(cb){ | ||
gulp.src(['src/**/*.js']) | ||
.pipe(istanbul()) | ||
.pipe(istanbul.hookRequire()) | ||
.on('finish',function(){ | ||
gulp.src(['mocha-globals.js','test/**/*-test.js', 'test/*-test.js']) | ||
.pipe(mocha()) | ||
.pipe(istanbul.writeReports({dir: './coverage/' + (process.env.NG_UTILS_PARSER || 'recast')})) | ||
.on('end',cb); | ||
}); | ||
}); | ||
/*function cloneTask(url, dir){ | ||
var opts = { ignoreCertErrors: 1, depth:1 } | ||
return function(cb){ | ||
clone(url, dir, opts) | ||
.then(function(repo){ | ||
cb(); | ||
}) | ||
.catch(cb); | ||
} | ||
}*/ | ||
gulp.task('test', function() { | ||
return gulp.src(['mocha-globals.js','test/**/*-test.js', 'test/*-test.js']) | ||
.pipe(mocha({growl:true})) | ||
.on('error', gutil.log); | ||
}); | ||
gulp.task('watch', function() { | ||
gulp.watch(['test/**', 'src/**'],['test']); | ||
}); | ||
gulp.task('lint', function() { | ||
return gulp.src(['src/**', 'test/**']) | ||
.pipe(jshint()) | ||
.pipe(jshint.reporter('default')); | ||
}); | ||
gulp.task('check-style', function() { | ||
return gulp.src(['src/**', 'test/**']) | ||
.pipe(jscs()); | ||
}); | ||
function cloneTask(url, dir){ | ||
@@ -32,3 +55,2 @@ return function(cb){ | ||
function execTask(dir){ | ||
@@ -39,3 +61,3 @@ return function(cb){ | ||
if(err){ | ||
console.log(gutils.colors.red([ | ||
console.log(gutil.colors.red([ | ||
'************************************************************************************************', | ||
@@ -48,3 +70,3 @@ '***************************** BEGIN FAILURE: ' + capsDir + ' ***************************************' | ||
console.log(gutils.colors.red([ | ||
console.log(gutil.colors.red([ | ||
'***************************** END FAILURE: ' + capsDir + ' *****************************************', | ||
@@ -55,3 +77,3 @@ '************************************************************************************************' | ||
else { | ||
console.log(gutils.colors.blue('************** PLUGIN TESTS SUCCEEDED: ' + capsDir + '***************************')); | ||
console.log(gutil.colors.blue('************** PLUGIN TESTS SUCCEEDED: ' + capsDir + '***************************')); | ||
} | ||
@@ -90,2 +112,15 @@ cb(err); | ||
gulp.task('default', ['run-browserify','run-karma','run-gulp']); | ||
gulp.task('default', ['run-browserify','run-karma','run-gulp']); | ||
var deps; | ||
var pluginUnderTest = process.env.NGUTILS_PLUGIN; | ||
if(pluginUnderTest){ | ||
pluginUnderTest = pluginUnderTest.toLowerCase(); | ||
deps = ['run-' + pluginUnderTest]; | ||
} | ||
else { | ||
deps = ['default'] | ||
} | ||
gulp.task('test-plugin', deps); |
{ | ||
"name": "ng-test-utils", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "angular test utilities", | ||
@@ -33,2 +33,6 @@ "main": "index.js", | ||
"gulp-clean": "^0.3.1", | ||
"gulp-istanbul": "^0.6.0", | ||
"gulp-jscs": "^1.4.0", | ||
"gulp-jshint": "^1.9.2", | ||
"gulp-mocha": "^2.0.0", | ||
"gulp-util": "^3.0.4", | ||
@@ -44,16 +48,12 @@ "istanbul": "^0.3.6", | ||
"scripts": { | ||
"test": "npm run lint && npm run cover && npm run check-style && gulp", | ||
"test": "npm run lint && npm run cover && npm run check-style", | ||
"test-all": "npm test && gulp test-plugin", | ||
"cover": "rm -rf coverage && npm run cover-recast && npm run cover-esprima && npm run cover-acorn && node combine-coverage", | ||
"watch": "mocha -w --growl mocha-globals.js test/*-test.js test/**/*-test.js", | ||
"lint": "jshint src/** test/**", | ||
"check-style": "jscs test/** src/**", | ||
"test-run": "mocha mocha-globals.js test/*-test.js test/**/*-test.js", | ||
"cover-run": "istanbul cover --report none --root ./src --dir ./coverage/$NG_UTILS_PARSER _mocha -- mocha-globals.js test/*-test.js test/**/*-test.js", | ||
"test-recast": "NG_UTILS_PARSER=recast npm run test-run", | ||
"test-esprima": "NG_UTILS_PARSER=esprima npm run test-run", | ||
"test-acorn": "NG_UTILS_PARSER=acorn npm run test-run", | ||
"cover-recast": "NG_UTILS_PARSER=recast npm run cover-run", | ||
"cover-esprima": "NG_UTILS_PARSER=esprima npm run cover-run", | ||
"cover-acorn": "NG_UTILS_PARSER=acorn npm run cover-run" | ||
"lint": "gulp lint", | ||
"check-style": "gulp check-style", | ||
"cover-recast": "NG_UTILS_PARSER=recast gulp cover", | ||
"cover-esprima": "NG_UTILS_PARSER=esprima gulp cover", | ||
"cover-acorn": "NG_UTILS_PARSER=acorn gulp cover" | ||
} | ||
} |
212
Readme.md
@@ -5,2 +5,6 @@ ng-test-utils | ||
[](https://travis-ci.org/jamestalmage/angular-test-utils) | ||
[](https://coveralls.io/r/jamestalmage/angular-test-utils?branch=master) | ||
Angular is an amazing framework, but its dependency injection framework can lead to some pretty verbose tests: | ||
@@ -15,3 +19,3 @@ | ||
serviceA = sinon.spy(); | ||
$provide.value('serviceA', serviceA); | ||
$provide.value('serviceA serviceA); | ||
})); | ||
@@ -53,13 +57,6 @@ | ||
It includes a comprehensive tests suite (i.e. 100% coverage), and has a thorough complement | ||
of plugins and examples that will help you easily fit it in to your build process. | ||
It includes a comprehensive tests suite (with 100% coverage), and has a thorough complement | ||
of [plugins](#build-plugins) with examples that will help you fit it in to your build process. | ||
To get started, check out the [karma preprocessor](https://github.com/jamestalmage/karma-angular-test-utils) | ||
to start using it in your tests. | ||
Browserify users should check out the [browserify transform](https://github.com/jamestalmage/browserify-angular-test-utils). | ||
If neither of those work for your current build process, there is also a | ||
[gulp plugin](https://github.com/jamestalmage/gulp-angular-test-utils) that can be adapted to nearly any build chain. | ||
Each module contains its own set of examples that will help get you started. | ||
@ngInject | ||
@@ -139,10 +136,10 @@ --------- | ||
@ngProvide | ||
---------- | ||
Where `@ngInject` is useful for getting testable instances *out* of your angular module, `@ngProvide` gives | ||
you a way to put spies or mocks *in* to the dependency injection framework. Again, the variable name is used to infer | ||
the name for the item being injected. | ||
@ngValue & ~~@ngProvide~~ | ||
------------------------- | ||
Where `@ngInject` helps you get testable instances *out* of your angular module, `@ngValue` | ||
provides a way to place spies or mocks *in* to the dependency injection framework. Variable names | ||
are used to infer the name for the item being injected. | ||
```javascript | ||
// @ngProvide | ||
// @ngValue | ||
var myService = { | ||
@@ -162,9 +159,176 @@ doSomething: sinon.spy(), | ||
}; | ||
$provide.value('myService', myService); | ||
$provide.value('myService myService); | ||
})); | ||
``` | ||
`@ngProvide` works the same way and is synonymous with `@ngValue`, but I plan to repurpose it with slightly | ||
different semantics - so stick with `@ngvalue` for now. | ||
You can use both `@ngProvide` and `@ngInject` together in your tests, but you must make sure all of your | ||
`@ngProvide` declarations come before `@ngInject`. | ||
You can use both `@ngValue` and `@ngInject` together in your tests, but you must make sure all of your | ||
`@ngValue` declarations come before your first `@ngInject`. | ||
@ngConstant | ||
----------- | ||
Very similar to `@ngConstant`, but it provides a constant service. From the angular docs: | ||
> Unlike a value [a constant] can be injected into a module configuration function (see angular.Module) | ||
and it cannot be overridden by an Angular decorator | ||
```javascript | ||
// @ngConstant | ||
var siteUrl = 'https://angular.io'; | ||
// ---- becomes ---- | ||
var siteUrl; | ||
beforeEach(module(function($provide){ | ||
siteUrl = 'https://angular.io'; | ||
$provide.constant('siteUrl siteUrl); | ||
})); | ||
``` | ||
All your `@ngConstant` declarations must come before your first `@ngInject`. | ||
@ngFactory | ||
---------- | ||
Provides a way to inject mock services using Angulars factory style provider. | ||
A factory function takes a list of injectables as arguments and returns a service. | ||
When you place the `@ngFactory` annotation on a named function, it will be replaced | ||
by a variable with that same name that is injected with the result of the factory | ||
functions invocation. | ||
```javascript | ||
// @ngFactory | ||
function timeoutInSeconds($timeout) { | ||
return function(fn, delay, invokeApply) { | ||
return $timeout(fn, delay * 1000, invokeApply); | ||
}; | ||
} | ||
// ---- becomes ---- | ||
var timeoutInSeconds; | ||
beforeEach(function() { | ||
angular.mock.module(function($provide) { | ||
$provide.factory(function($timeout) { | ||
return timeoutInSeconds = function(fn, delay, invokeApply) { | ||
return $timeout(fn, delay * 1000, invokeApply); | ||
} | ||
}); | ||
}); | ||
}); | ||
``` | ||
The above example could also be achieved by combining `@ngValue` and `@ngInject`. | ||
```javascript | ||
@ngValue | ||
var timeoutInSeconds = function(fn, delay, invokeApply) { | ||
return $timeout(fn, delay, invokeApply); | ||
} | ||
@ngInject | ||
var $timeout; | ||
``` | ||
This will work in most cases, and has the added advantage of exposing $timeout to | ||
your tests as well. Problems would arise if `timeoutInSeconds` were to be called | ||
during module initialization before the `@ngInject` annotation has injected `$timeout` | ||
in to your test. In that case `@ngFactory` is an acceptable workaround. | ||
@ngService | ||
---------- | ||
Very similar to `@ngFactory`, but rather than assigning the return value, it | ||
uses the function as a constructor and injects the new instance. | ||
```javascript | ||
// @ngService | ||
function myService(injectedDependency) { | ||
this.foo = "bar"; | ||
} | ||
// ----- becomes ----- | ||
var myService; | ||
beforeEach(function() { | ||
angular.mock.module(function($provide) { | ||
$provide.service("myService" function(injectedDependency) { | ||
myService = this; | ||
this.foo = "bar"; | ||
}); | ||
}); | ||
}); | ||
``` | ||
Note - this currently does not work if you `return` a value from your constructor. | ||
If that is the case you probably should be using `@ngFactory`. | ||
@ngProvider | ||
----------- | ||
Provide mock providers using `$provide.provider()` | ||
```javascript | ||
// @ngProvider | ||
var myProvider = { | ||
name: "world", | ||
$get: function(a) { | ||
return "hello " + this.name + a; | ||
} | ||
} | ||
// ---- becomes ---- | ||
var myProvider; | ||
beforeEach(function() { | ||
angular.mock.module(function($provide) { | ||
myProvider = { | ||
name: "world", | ||
$get: function(a) { | ||
return "hello " + this.name + a; | ||
} | ||
}; | ||
$provide.provider("myProvider", myProvider); | ||
}); | ||
}); | ||
``` | ||
@ngDirective | ||
------------ | ||
Create a stub directive (experimental). | ||
```javascript | ||
// @ngDirective | ||
function myDirective($timeout, $log) { | ||
return { | ||
template: "<div></div>", | ||
link: function postLink(scope, iElement, iAttrs, controller) { | ||
// do stuff | ||
} | ||
}; | ||
} | ||
// ---- becomes ---- | ||
var myDirective; | ||
beforeEach(function() { | ||
angular.mock.module(function($compileProvider) { | ||
myDirective = function($timeout, $log) { | ||
return { | ||
template: "<div></div>", | ||
link: function postLink(scope, iElement, iAttrs, controller) { | ||
// do stuff | ||
} | ||
} | ||
}; | ||
$compileProvider.directive("myDirective", myDirective); | ||
}); | ||
}); | ||
``` | ||
source-maps | ||
@@ -177,8 +341,9 @@ ----------- | ||
`ng-test-utils` ships with full source-map support. Just make sure you enable source-maps | ||
in your browsers developer tools, and enable source-map support (see the individual plugins for more details | ||
on how to do so). | ||
in your browsers developer tools, and enable source-map support from whichever plugin you | ||
are using in your build. | ||
plugins | ||
------- | ||
build plugins | ||
------------- | ||
`ng-test-utils` has a number of companion plugins that help you insert it in your build process. | ||
Each one has its own set of examples that will help get you started. | ||
@@ -197,1 +362,2 @@ * The [karma preprocessor](https://github.com/jamestalmage/karma-angular-test-utils) provides | ||
@@ -5,5 +5,28 @@ 'use strict'; | ||
var ngProvide = require('./ngProvide')( | ||
'value', /^\s*@ngProvide\s*$/, require('./silent-logger') | ||
); | ||
var ngValue = require('./ngProvide')( | ||
'value', /^\s*@ngValue\s*$/, require('./silent-logger') | ||
); | ||
var ngConstant = require('./ngProvide')( | ||
'constant', /^\s*@ngConstant\s*$/, require('./silent-logger') | ||
); | ||
var ngProvider = require('./ngProvide')( | ||
'provider', /^\s*@ngProvider\s*$/, require('./silent-logger') | ||
); | ||
var ngDirective = require('./ngProvide')( | ||
'directive', /^\s*@ngDirective\s*$/, require('./silent-logger'), '$compileProvider' | ||
); | ||
var ngFactory = require('./ngFactory')(/^\s*@ngFactory\s*$/); | ||
var ngService = require('./ngService')(/^\s*@ngService\s*$/); | ||
var ngInject = require('./ngInject'); | ||
var recast = require('recast'); | ||
var ngProvide = require('./ngProvide/index'); | ||
var ngInject = require('./ngInject'); | ||
var convert = require('convert-source-map'); | ||
@@ -16,3 +39,9 @@ var merge = require('merge'); | ||
ngInject:true, | ||
ngProvide:true | ||
ngProvide:true, | ||
ngValue:true, | ||
ngConstant:true, | ||
ngFactory:true, | ||
ngService:true, | ||
ngProvider:true, | ||
ngDirective:true | ||
}, suppliedOptions); | ||
@@ -36,5 +65,23 @@ if (options.sourceMap) { | ||
} | ||
if (options.ngValue) { | ||
ngValue(ast); | ||
} | ||
if (options.ngInject) { | ||
ngInject(ast); | ||
} | ||
if (options.ngConstant) { | ||
ngConstant(ast); | ||
} | ||
if (options.ngFactory) { | ||
ngFactory(ast); | ||
} | ||
if (options.ngService) { | ||
ngService(ast); | ||
} | ||
if (options.ngProvider) { | ||
ngProvider(ast); | ||
} | ||
if (options.ngDirective) { | ||
ngDirective(ast); | ||
} | ||
var result = recast.print(ast, options); | ||
@@ -41,0 +88,0 @@ var transformedCode = result.code; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
33713
692
357
19
15
2
2