English
Linting, formatting and tests
The backend has analyzers that fail the build and dotnet format that keeps the code style (topic 2). The client gets the same three safety nets: a linter, a formatter and a test runner. All commands of this chapter run in Source/Chatex.Web.
Step 1. ESLint
Why. The TypeScript compiler checks types; ESLint checks everything else that is legal but suspicious — an unused variable, an empty function, any where a type was possible — and, through angular-eslint, Angular-specific rules for components and templates, including accessibility (a button without a label, a click handler without a keyboard equivalent).
Do. ng add angular-eslint has already written the configuration. Check that yours matches:
js
// @ts-check
const eslint = require('@eslint/js');
const { defineConfig } = require('eslint/config');
const tseslint = require('typescript-eslint');
const angular = require('angular-eslint');
module.exports = defineConfig([
{
files: ['**/*.ts'],
extends: [
eslint.configs.recommended,
tseslint.configs.recommended,
tseslint.configs.stylistic,
angular.configs.tsRecommended,
],
processor: angular.processInlineTemplates,
rules: {
'@angular-eslint/directive-selector': [
'error',
{
type: 'attribute',
prefix: 'app',
style: 'camelCase',
},
],
'@angular-eslint/component-selector': [
'error',
{
type: 'element',
prefix: 'app',
style: 'kebab-case',
},
],
},
},
{
files: ['**/*.html'],
extends: [angular.configs.templateRecommended, angular.configs.templateAccessibility],
rules: {},
},
]);- The file is the flat config format of ESLint 9 and later: an array of objects, each applying rules to the files that match
files. - TypeScript files get the recommended rules of ESLint, typescript-eslint (plus its stylistic set) and angular-eslint.
processInlineTemplatesextracts the inline templates of components so that the template rules check them too. - Selectors of components must be kebab-case elements starting with
app-(app-shell), selectors of directives camelCase attributes starting withapp. The prefix keeps our elements apart from real HTML elements and from library components. - HTML templates get the recommended template rules and the accessibility rules.
Check
powershell
npm run lintLinting "chatex"...
All files pass linting.Step 2. Prettier
Why. A formatter ends all discussions about style: it rewrites the code into one canonical layout. Prettier formats TypeScript, HTML, CSS and JSON, and it understands Angular templates — also the inline ones inside @Component. The Tailwind plugin additionally sorts the utility classes of every class attribute into Tailwind's recommended order, so the same classes always appear in the same order and diffs stay small.
Do. Add the plugin and the path to the stylesheet that defines our custom utilities (the next chapter) to .prettierrc:
json
{
"printWidth": 100,
"singleQuote": true,
"plugins": ["prettier-plugin-tailwindcss"],
"tailwindStylesheet": "./src/styles.css",
"overrides": [
{
"files": "*.html",
"options": {
"parser": "angular"
}
}
]
}printWidth: 100— lines up to 100 characters, the same limit as the C# code.singleQuote: true—'text'instead of"text"in TypeScript.overrides—.htmlfiles are parsed as Angular templates, so that control flow blocks such as@ifare formatted correctly.
Now format the generated files once. This also turns their CRLF line endings into LF:
powershell
npm run formatsrc/app/app.config.ts 77ms
src/app/app.css 1ms (unchanged)
src/app/app.html 66ms
src/app/app.routes.ts 2ms
src/app/app.spec.ts 8ms
src/app/app.ts 4ms
src/index.html 3ms
src/main.ts 2ms
src/styles.css 1msCheck
powershell
npm run format:checkChecking formatting...
All matched files use Prettier code style!Before npm run format, the same command listed eight files and ended with Code style issues found in 8 files. Run Prettier with --write to fix. — the message CI shows when you forget to format.
Step 3. Vitest
Why. Unit tests of the client run with Vitest, a fast test runner with a Jest-compatible API. The Angular CLI integrates it: the test target of angular.json uses the builder @angular/build:unit-test, which compiles the application and the *.spec.ts files with the same compiler as ng build and runs them with Vitest in jsdom, a browser simulation in Node.js. There is no vitest.config.ts to maintain.
The test files are compiled with tsconfig.spec.json. Its only addition to the common settings is the type package vitest/globals: the functions describe, it, expect, beforeEach and vi are available in every spec without imports, as in xUnit you do not import [Fact] into each file.
Do. Replace the generated file with the same content in LF line endings:
json
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": [
"vitest/globals"
]
},
"include": [
"src/**/*.d.ts",
"src/**/*.spec.ts"
]
}Do the same with the common settings of Table 15.5:
json
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
{
"compileOnSave": false,
"compilerOptions": {
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"isolatedModules": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "ES2022",
"module": "preserve"
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true
},
"files": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}Run the tests once, without watching for changes:
powershell
npm test -- --watch=falseThe -- passes the option through npm to ng test. Without it, Vitest keeps running and reruns the affected tests on every save — convenient while you write code.
Check
The generated app.spec.ts has two tests, and both pass:
Test Files 1 passed (1)
Tests 2 passed (2)They test the welcome page, which the next chapters replace; app.spec.ts will go with it.
Vitest for an xUnit user
A spec file is a test class: describe('ThemeService', () => { ... }) groups tests, it('does something', () => { ... }) is a [Fact], beforeEach is the constructor that xUnit runs before every test, and expect(x).toBe(y) is x.ShouldBe(y). vi.fn() creates a mock function that records its calls, like a fake in topic 9. Angular's TestBed is a small DI container for one test: you register providers, and TestBed.inject(T) resolves a service.
Step 4. The editor and the README
Why. VS Code with the Angular Language Service extension type-checks templates as you type and completes component inputs. The generated .vscode folder recommends the extension, starts the dev server for debugging and defines the npm tasks. The debug configuration for ng test opens a Karma page that Vitest does not have, so it goes.
Do. Replace the three files (only launch.json changes; the other two get LF endings):
json
{
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "ng serve",
"type": "chrome",
"request": "launch",
"preLaunchTask": "npm: start",
"url": "http://localhost:4200/"
}
]
}json
{
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=827846
"recommendations": ["angular.ng-template"]
}json
{
// For more information, visit: https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "start",
"isBackground": true,
"problemMatcher": {
"owner": "typescript",
"pattern": "$tsc",
"background": {
"activeOnStart": true,
"beginsPattern": {
"regexp": "Changes detected"
},
"endsPattern": {
"regexp": "bundle generation (complete|failed)"
}
}
}
},
{
"type": "npm",
"script": "test",
"isBackground": true,
"problemMatcher": {
"owner": "typescript",
"pattern": "$tsc",
"background": {
"activeOnStart": true,
"beginsPattern": {
"regexp": "Changes detected"
},
"endsPattern": {
"regexp": "bundle generation (complete|failed)"
}
}
}
}
]
}The repository root's .gitignore ignores .vscode/* but keeps exactly these files (!.vscode/launch.json and so on), so they are shared with the team.
Replace the generated README with a short one that points to the documentation:
md
# Chatex web app
Angular 22 + Tailwind CSS v4 frontend for Chatex. See the [Frontend docs](../../Docs/Frontend.md) for the architecture and how it talks to the API.
```bash
npm install
```
```bash
npm start
```
The dev server runs at http://localhost:4200 and proxies `/api` and `/hubs` to the API at http://localhost:5214, so start the API first.
```bash
npm run lint
```
```bash
npm test -- --watch=false
```
```bash
npm run build
```