How to exeHicute angular unit test cases using Nodejs and Karma - node.js

I have created a unit test in angular js,However I have no idea on how to setup node.js and Karma
So I downloaded the node.js from nodejs.org and installed it.
Open the command prompt and installed karma by executing "npm install karma"
It installed karma.But when I execute my angular js unit test case,it reports some errors
angular is undefined.
I followed the instructions as mentioned in this
http://bardevblog.wordpress.com/2013/07/28/setting-up-angularjs-angular-seed-node-js-and-karma/
Is there any where how to setup Karma and node js to execute angular unit test cases.
Please provide me the information

You can use the Yeoman generator for an Angular.js app to generate a new project and see how it is done there. Please follow the steps under "Usage" on the GitHub page to set up the new project. (For all questions yo is asking just hit enter.) Finally, type grunt test:client. This executes Karma configured with the karma.conf.js.

In your karma config file you need to define all the js files that needs to be loaded and the order in which you specify then is important! Example:
files : [
'source/lib/angular/angular.js', // angular first
'source/lib/angular/*.js', // then any other angular files (angular-mocks.js, angular-resource.js, angular-route.js ...
'source/lib/x2js/xml2json.min.js', // some libraries I used
'source/lib/jquery/jquery.js',
'source/lib/ui-bootstrap/ui-bootstrap.js',
'source/js/*.js', // my own js code
'source/tests/unit/*.js' // unit tests
]

Related

How to debug Angular universal?

I am using Angular Universal Starter repo. In angular 4 universal for ssr we could debug browser and node part of application in dev mode(see console), but now I do not see way to debug node part. I tried to execute ts-node server.ts with some changes( paths to files, etc), but angular seems needs aot compiled app and
throw Error: You must pass in a NgModule or NgModuleFactory to be
bootstrapped.
from docs:
Development (Client-side only rendering)
- run npm run start which will start ng serve
Production (also for testing SSR/Pre-rendering locally)
- npm run build:ssr && npm run serve:ssr
At first glance debug on Node.js in development do not work. At least from the box. May be someone resolve this issue.
You can't debug node part of your Angular 4 app in browser. Node work on server part so you can't see this in browser (client side).
Only way to debug this part when you start it from ts-node server.ts is to use external tools like WebStorm etc. If you start your App in TS mode from Debug mode you can use all features of this tools.
I think this small piece of code can help you
create the project
ng n debuggable-universal-server --interactive=false
cd debuggable-universal-server
add universal
ng add #nguniversal/express-engine --clientProject debuggable-universal-server
To create the server-side app module, app.server.module.ts, run the following CLI command.
ng add #nguniversal/express-engine
To start rendering your app with Universal on your local system, use the following command.
npm run dev:ssr

Test nodejs server code without running server

New to nodejs testing.
I have a nodejs server that runs some complex server side logic, and I'm looking at building a unit test runner for that code. I do not want to run the server and tests sending it requests though, as that doesn't expose all the modules and functions I want to test on the server side. That will be more like integration testing. I just want to import those server side files, which are written as AMD modules, and call their functions one by one in unit tests.
What's the best way to go about doing this?
You will want to start by installing a unit-test and assertion framework to your current project. Then you will also want to add requirejs (r.js).
We are currently using mochajs for unit-testing with should as the assertion library. Both have great adoption and feature support for testing Node.js.
npm install mochajs shouldjs requirejs --save-dev
This will add three packages to your local node_modules as well as save them inside your package.json's devDependencies.
Go ahead and setup a unit test directory in your project and create a new test file, [your_module_name]_test.js:
const should = require('should'),
foo = require('foo');
describe('foo', () => {
it('returns the letter a', () => {
foo().should.eql('a');
});
});
In the test file you will want to require the module under test and then unit-test as usual.
You can then run the test through r.js
./node_modules/requirejs/bin/r.js [your_test_dir]/[your_module_name]_test.js
You can also install mochajs globally and then simply run the mocha command instead of using the bin inside of your local node_modules.
Best of luck, and hope this helps.

Enabling Intellisense for Mocha

I'm running VS Code 1.0. I've already successfully added intellisense for a number of frameworks, i.e., Express, Restify, and even Gulp.
Now I'm trying to enable it for Mocha.
The minimum setup I'm doing is:
Create a folder.
npm init
npm install mocha
typings install mocha --ambient
Open Code in said folder.
Create a new file and try to type "describe".
Also, this guy got it working.
You just have to ad jsconfig.json file to the root of your project. It can even be empty.
Check this docs.

Angular2 throws error while use the node modules reference

I am getting error while i use the angular2 from node_modules.
How do I fix? What I missed in my html file?
Please give me a git repo for angular2 for offline development.
Edit: I missed that the console was throwing other errors, too. I use Karma to run unit tests for an angular2 app and borrowed much of my test setup from the angular2 project itself.
Karma loads the files and since I'm using them for test, I load out of node_modules. The first four must be loaded first and in this order before bringing in angular2 and other libs:
'node_modules/angular2/node_modules/traceur/bin/traceur-runtime.js',
'node_modules/es6-module-loader/dist/es6-module-loader-sans-promises.js',
'node_modules/systemjs/dist/system.src.js',
'node_modules/reflect-metadata/Reflect.js',
'node_modules/angular2/**/*.js
Then a test helper configures System:
System.config({
...
paths: {
'angular2/*': 'node_modules/angular2/*.js',
'rx': 'node_modules/angular2/node_modules/rx/dist/rx.js'
}
...
});
Hope that helps. You may want to pull the files you need into a lib directory if you're serving them with your code.

Trouble working with Karma

I am trying to use Karma with Mocha over a NodeJS project, and I get an error:
"Module name "TestStub" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded"
Not sure where am I supposed to add the test stub file.
In addition, when I configured Karma using "Karma init" I asked for being able to use requireJS, but don't where are the config files to be added to Karma. There's a config file for the tests and another one for the code and I'll need to include both.
Karma is for browser testing. If you want to test your server side nodejs code with karma, you will fail... Maybe you can do integration tests which can check the response of your nodejs application by hand-made http requests, but that's all...
Almost everything about how to use karma with requirejs is well documented here.

Resources