# Developing AngularJS * [Development Setup](#setup) * [Running Tests](#tests) * [Coding Rules](#rules) * [Commit Message Guidelines](#commits) * [Writing Documentation](#documentation) ## Development Setup This document describes how to set up your development environment to build and test AngularJS, and explains the basic mechanics of using `git`, `node`, `yarn` and `grunt`. ### Installing Dependencies Before you can build AngularJS, you must install and configure the following dependencies on your machine: * [Git](http://git-scm.com/): The [Github Guide to Installing Git][git-setup] is a good source of information. * [Node.js v8.x (LTS)](http://nodejs.org): We use Node to generate the documentation, run a development web server, run tests, and generate distributable files. Depending on your system, you can install Node either from source or as a pre-packaged bundle. We recommend using [nvm](https://github.com/creationix/nvm) (or [nvm-windows](https://github.com/coreybutler/nvm-windows)) to manage and install Node.js, which makes it easy to change the version of Node.js per project. * [Yarn](https://yarnpkg.com): We use Yarn to install our Node.js module dependencies (rather than using npm). See the detailed [installation instructions][yarn-install]. * [Java](http://www.java.com): We minify JavaScript using [Closure Tools](https://developers.google.com/closure/), which require Java (version 7 or higher) to be installed and included in your [PATH](http://docs.oracle.com/javase/tutorial/essential/environment/paths.html) variable. * [Grunt](http://gruntjs.com): We use Grunt as our build system. We're using it as a local dependency, but you can also add the grunt command-line tool globally (with `yarn global add grunt-cli`), which allows you to leave out the `yarn` prefix for all our grunt commands. ### Forking AngularJS on Github To contribute code to AngularJS, you must have a GitHub account so you can push code to your own fork of AngularJS and open Pull Requests in the [GitHub Repository][github]. To create a Github account, follow the instructions [here](https://github.com/signup/free). Afterwards, go ahead and [fork](http://help.github.com/forking) the [main AngularJS repository][github]. ### Building AngularJS To build AngularJS, you clone the source code repository and use Grunt to generate the non-minified and minified AngularJS files: ```shell # Clone your Github repository: git clone https://github.com//angular.js.git # Go to the AngularJS directory: cd angular.js # Add the main AngularJS repository as an upstream remote to your repository: git remote add upstream "https://github.com/angular/angular.js.git" # Install JavaScript dependencies: yarn install # Build AngularJS: yarn grunt package ``` **Note:** If you're using Windows, you must use an elevated command prompt (right click, run as Administrator). This is because `yarn grunt package` creates some symbolic links. The build output is in the `build` directory. It consists of the following files and directories: * `angular-.zip` — The complete zip file, containing all of the release build artifacts. * `angular.js` / `angular.min.js` — The regular and minified core AngularJS script file. * `angular-*.js` / `angular-*.min.js` — All other AngularJS module script files. * `docs/` — A directory that contains a standalone version of the docs (same as served in `docs.angularjs.org`). ### Running a Local Development Web Server To debug code, run end-to-end tests, and serve the docs, it is often useful to have a local HTTP server. For this purpose, we have made available a local web server based on Node.js. 1. To start the web server, run: ```shell yarn grunt webserver ``` 2. To access the local server, enter the following URL into your web browser: ```text http://localhost:8000/ ``` By default, it serves the contents of the AngularJS project directory. 3. To access the locally served docs, visit this URL: ```text http://localhost:8000/build/docs/ ``` ## Running Tests ### Running the Unit Test Suite We write unit and integration tests with Jasmine and execute them with Karma. To run all of the tests once on Chrome run: ```shell yarn grunt test:unit ``` To run the tests on other browsers use the command line flag: ```shell yarn grunt test:unit --browsers=Chrome,Firefox ``` **Note:** there should be _no spaces between browsers_. `Chrome, Firefox` is INVALID. If you have a Saucelabs or Browserstack account, you can also run the unit tests on these services via our pre-defined customLaunchers. See the [karma config file](/karma-shared.conf.js) for all pre-configured browsers. For example, to run the whole unit test suite on selected browsers: ```shell # Browserstack yarn grunt test:unit --browsers=BS_Chrome,BS_Firefox,BS_Safari,BS_IE_9,BS_IE_10,BS_IE_11,BS_EDGE,BS_iOS_10 # Saucelabs yarn grunt test:unit --browsers=SL_Chrome,SL_Firefox,SL_Safari,SL_IE_9,SL_IE_10,SL_IE_11,SL_EDGE,SL_iOS_10 ``` Running these commands requires you to set up [Karma Browserstack][karma-browserstack] or [Karma-Saucelabs][karma-saucelabs], respectively. During development, however, it's more productive to continuously run unit tests every time the source or test files change. To execute tests in this mode run: 1. To start the Karma server, capture Chrome browser and run unit tests, run: ```shell yarn grunt autotest ``` 2. To capture more browsers, open this URL in the desired browser (URL might be different if you have multiple instance of Karma running, read Karma's console output for the correct URL): ```text http://localhost:9876/ ``` 3. To re-run tests just change any source or test file. To learn more about all of the preconfigured Grunt tasks run: ```shell yarn grunt --help ``` ### Running the End-to-end Test Suite AngularJS's end to end tests are run with Protractor. Simply run: ```shell yarn grunt test:e2e ``` This will start the webserver and run the tests on Chrome. ## Coding Rules To ensure consistency throughout the source code, keep these rules in mind as you are working: * All features or bug fixes **must be tested** by one or more [specs][unit-testing]. * All public API methods **must be documented** with ngdoc, an extended version of jsdoc (we added support for markdown and templating via @ngdoc tag). To see how we document our APIs, please check out the existing source code and see the section about [writing documentation](#documentation) * With the exceptions listed below, we follow the rules contained in [Google's JavaScript Style Guide][js-style-guide]: * **Do not use namespaces**: Instead, wrap the entire AngularJS code base in an anonymous closure and export our API explicitly rather than implicitly. * Wrap all code at **100 characters**. * Instead of complex inheritance hierarchies, we **prefer simple objects**. We use prototypal inheritance only when absolutely necessary. * We **love functions and closures** and, whenever possible, prefer them over objects. * To write concise code that can be better minified, we **use aliases internally** that map to the external API. See our existing code to see what we mean. * We **don't go crazy with type annotations** for private internal APIs unless it's an internal API that is used throughout AngularJS. The best guidance is to do what makes the most sense. ### Specific topics #### Provider configuration When adding configuration (options) to [providers][docs.provider], we follow a special pattern. - for each option, add a `method` that ... - works as a getter and returns the current value when called without argument - works as a setter and returns itself for chaining when called with argument - for boolean options, uses the naming scheme `