24
loading...
This website collects cookies to deliver better user experience
If you are looking to the easy way use jest schematic https://github.com/briebug/jest-schematic
"@types/jasmine": "~3.10.0",
"jasmine-core": "~3.10.0",
"karma": "~6.3.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.1.0",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "~1.7.0",
npm install
command from the terminal again to npm remove not used packages.rm karma.conf.js
rm src/test.ts
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": ["src/favicon.ico", "src/assets"],
"styles": ["src/styles.css"],
"scripts": []
}
npm install --save-dev jest jest-preset-angular @types/jest
import 'jest-preset-angular/setup-jest';
test: "ng test"
to test: "jest"
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "jest"
},
jest-preset-angular
and use the jest configuration file."jest": {
"preset": "jest-preset-angular",
"setupFilesAfterEnv": ["<rootDir>/setup-jest.ts"]
}
"esModuleInterop": true,
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": ["jest"]
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}
npm run test
> [email protected] test
> Jest
PASS src/app/app.component.spec.ts
AppComponent
✓ should create the app (169 ms)
✓ should have as title 'lab' (43 ms)
✓ should render title (47 ms)
Test Suites: 1 passed, one total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 1.635 s, estimated 4 s
Ran all test suites.
npm install --save-dev @testing-library/angular
render
and screen
from @testing-library/angular, render
help us to load the component, and screen
provide an extensive list o ways to find elements in the browser.We use the await keyword to wait for render and screen results.
import { render, screen } from '@testing-library/angular';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
it('should render Welcome', async () => {
await render(AppComponent);
await screen.getByText('Welcome');
});
});
npm run test
> [email protected] test
> Jest
PASS src/app/app.component.spec.ts
PASS src/app/app.ui.component.spec.ts
Test Suites: 2 passed, 2 total
Tests: 4 passed, 4 total
Snapshots: 0 total
Time: 4.631 s
Ran all test suites.