Debugging and Testing
Note for full Intellij debug compatabillity you must use Google Chrome. It's also sort of a hassle to configure linux for tests the first time; ensure CHROME_BIN
env variable is set to chromium, or add a firefox config to Karma.js
Debugging (In Intellij)
- You can create break points, but to run the you need to add a debug configuration.
Go to 'Edit Configurations...', then find JavaScript Debgger, there is a default Angular config
Testing
- The CLI generates tests ending in .spec.ts to allow for test-driven development
- Like JUnit, tests are run independently
- Run
ng test
in the console to run every test method
// Set up testing eviornment
fdescribe('DataService', () => {
let service: DataService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(DataService);
});
// tests begin with it
it('should be created', () => {
expect(service).toBeTruthy();
});
// focused test
fit('addBook increases size of books', () => {
const book = new Book();
const before = service.books.length;
service.addBook(book);
expect(service.books.length).toBeGreaterThan(before);
});
});
- To run 'focused' tests you add the letter f to the test class and method you want to be run.
- There are many features similar to JUnit and Mockito, such as service mocking and monitoring
- A fixture is sort metadata that sits above the component when its created in the testing environment
- It's the only way to reference a Service called by Dependecy Injection
Angular Environments
Angular environment variables are store in a folder under src/ called environments in files environment.ts and environment.prod.ts for testing and production. It contains key value pairs. We can import the environment for access to all these variables.
NEVER import environment.prod.ts. Import environment.ts
To run Angular with production enviornment:
ng serve --prod
which is a shortcut for ng serve -c production
(Although in actual deployment we would not use the Angular server)
Angular.json tells the app where to get production and regular environment variables, and can even replace files when certian environments are activated
It's a good idea to have at least 3 enviornments; One for Production, for development or localhost, and a local version that has data stored internally.
No Comments