Skip to content

Commit 3ad74a3

Browse files
authored
Set up Jest for unit tests and Cypress for e2e tests (#401)
* Add support for Jest * Set up cypress e2e tests #SkipVersionBump
1 parent 898f2b7 commit 3ad74a3

21 files changed

+10541
-5864
lines changed

.eslintrc.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ module.exports = {
1111
plugins: ['@typescript-eslint', 'cypress', 'prettier'],
1212
rules: {
1313
'prettier/prettier': ['error'],
14-
"@typescript-eslint/no-explicit-any": "warn",
15-
"@typescript-eslint/no-unused-vars": "warn"
14+
'@typescript-eslint/no-explicit-any': 'warn',
15+
'@typescript-eslint/no-unused-vars': 'warn'
1616
},
1717
globals: {
1818
Cypress: 'readonly'
19+
},
20+
env: {
21+
jest: true
1922
}
2023
};

.github/workflows/cps-shared-ui-checkers.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ jobs:
3333
- name: Build app
3434
run: npm run build
3535

36+
- name: Run unit tests
37+
run: npm run test
38+
3639
- name: Lint app
3740
run: npm run lint
38-
39-
- name: Run Cypress component tests
40-
run: npx cypress run --component

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ Make sure `ng build cps-ui-kit --watch` is running, so the library will be rebui
8080

8181
Execute `npm run generate-json-api` to generate documentation for any changes in the components' API.
8282

83-
#### Run cypress tests
83+
#### Run Jest unit tests
8484

8585
`npm run test`
86+
87+
88+
#### Run Cypress e2e tests
89+
90+
`npm run cypress:run` (headless run) or you can open Cypress tests using `npm run cypress:open`

angular.json

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
"schematics": {
99
"@schematics/angular:component": {
1010
"style": "scss"
11-
},
12-
"@cypress/schematic:component": {
13-
"style": "scss"
1411
}
1512
},
1613
"root": "projects/cps-ui-kit",
@@ -33,45 +30,7 @@
3330
"defaultConfiguration": "production"
3431
},
3532
"test": {
36-
"builder": "@angular-devkit/build-angular:karma",
37-
"options": {
38-
"tsConfig": "projects/cps-ui-kit/tsconfig.spec.json",
39-
"polyfills": ["zone.js", "zone.js/testing"]
40-
}
41-
},
42-
"cypress-run": {
43-
"builder": "@cypress/schematic:cypress",
44-
"options": {
45-
"devServerTarget": "cps-ui-kit:serve",
46-
"configFile": "cypress.config.ts"
47-
},
48-
"configurations": {
49-
"production": {
50-
"devServerTarget": "cps-ui-kit:serve:production"
51-
}
52-
}
53-
},
54-
"cypress-open": {
55-
"builder": "@cypress/schematic:cypress",
56-
"options": {
57-
"watch": true,
58-
"headless": false,
59-
"configFile": "cypress.config.ts"
60-
}
61-
},
62-
"ct": {
63-
"builder": "@cypress/schematic:cypress",
64-
"options": {
65-
"devServerTarget": "cps-ui-kit:serve",
66-
"watch": true,
67-
"headless": false,
68-
"testingType": "component"
69-
},
70-
"configurations": {
71-
"development": {
72-
"devServerTarget": "cps-ui-kit:serve:development"
73-
}
74-
}
33+
"builder": "@angular-builders/jest:run"
7534
}
7635
}
7736
},
@@ -80,9 +39,6 @@
8039
"schematics": {
8140
"@schematics/angular:component": {
8241
"style": "scss"
83-
},
84-
"@cypress/schematic:component": {
85-
"style": "scss"
8642
}
8743
},
8844
"root": "projects/composition",
@@ -96,9 +52,7 @@
9652
"base": "dist/composition"
9753
},
9854
"index": "projects/composition/src/index.html",
99-
"polyfills": [
100-
"zone.js"
101-
],
55+
"polyfills": ["zone.js"],
10256
"tsConfig": "projects/composition/tsconfig.app.json",
10357
"inlineStyleLanguage": "scss",
10458
"assets": [
@@ -109,9 +63,7 @@
10963
"output": "assets"
11064
}
11165
],
112-
"styles": [
113-
"projects/composition/src/styles.scss"
114-
],
66+
"styles": ["projects/composition/src/styles.scss"],
11567
"scripts": [],
11668
"browser": "projects/composition/src/main.ts"
11769
},
@@ -164,21 +116,13 @@
164116
}
165117
},
166118
"test": {
167-
"builder": "@angular-devkit/build-angular:karma",
168-
"options": {
169-
"polyfills": ["zone.js", "zone.js/testing"],
170-
"tsConfig": "projects/composition/tsconfig.spec.json",
171-
"inlineStyleLanguage": "scss",
172-
"assets": ["projects/composition/src/assets"],
173-
"styles": ["projects/composition/src/styles.scss"],
174-
"scripts": []
175-
}
119+
"builder": "@angular-builders/jest:run"
176120
}
177121
}
178122
}
179123
},
180124
"cli": {
181-
"schematicCollections": ["@cypress/schematic", "@schematics/angular"],
125+
"schematicCollections": ["@schematics/angular"],
182126
"analytics": false
183127
}
184128
}

cypress.config.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
import { defineConfig } from 'cypress';
22

33
export default defineConfig({
4-
component: {
5-
devServer: {
6-
framework: 'angular',
7-
bundler: 'webpack',
8-
options: {
9-
projectConfig: {
10-
root: './',
11-
sourceRoot: 'src',
12-
buildOptions: {
13-
outputPath: 'dist/composition'
14-
}
15-
}
16-
}
17-
},
18-
specPattern: 'projects/cps-ui-kit/**/*.cy.ts',
19-
video: false
4+
e2e: {
5+
baseUrl: 'http://localhost:4200'
206
}
217
});

cypress/e2e/cps-autocomplete.cy.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
describe('cps-autocomplete page', () => {
2+
describe('required single autocomplete with a tooltip', () => {
3+
beforeEach(() => {
4+
cy.visit('/autocomplete');
5+
});
6+
7+
it('should select items properly', () => {
8+
cy.get("[data-cy='required-autocomplete']").click();
9+
cy.contains('Rome').click();
10+
cy.get("[data-cy='required-autocomplete'] .single-item-selection").should(
11+
'have.text',
12+
'Rome'
13+
);
14+
cy.get("[data-cy='required-autocomplete']").click();
15+
cy.contains('Prague').click();
16+
cy.get("[data-cy='required-autocomplete'] .single-item-selection").should(
17+
'have.text',
18+
'Prague'
19+
);
20+
});
21+
22+
it('should clear items', () => {
23+
cy.get("[data-cy='required-autocomplete'] .single-item-selection").should(
24+
'have.text',
25+
'Prague'
26+
);
27+
cy.get(
28+
"[data-cy='required-autocomplete'] .cps-autocomplete-box-clear-icon"
29+
).click();
30+
cy.get('body').click(0, 0);
31+
cy.contains('Field is required');
32+
cy.get("[data-cy='required-autocomplete'] .single-item-selection").should(
33+
'not.exist'
34+
);
35+
});
36+
});
37+
});

cypress/support/component-index.html

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ***********************************************************
2-
// This example support/component.ts is processed and
2+
// This example support/e2e.ts is processed and
33
// loaded automatically before your test files.
44
//
55
// This is a great place to put global configuration and
@@ -16,20 +16,5 @@
1616
// Import commands.js using ES2015 syntax:
1717
import './commands';
1818

19-
import { mount } from 'cypress/angular';
20-
21-
// Augment the Cypress.Chainable interface to include type definitions for
22-
// your custom command.
23-
declare global {
24-
// eslint-disable-next-line @typescript-eslint/no-namespace
25-
namespace Cypress {
26-
interface Chainable {
27-
mount: typeof mount;
28-
}
29-
}
30-
}
31-
32-
Cypress.Commands.add('mount', mount);
33-
34-
// Example use:
35-
// cy.mount(MyComponent)
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

cypress/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["es5", "dom"],
5+
"types": ["cypress", "node"],
6+
"resolveJsonModule": true
7+
},
8+
"include": ["**/*.ts"]
9+
}

jest-setup.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'jest-preset-angular/setup-jest';
2+
3+
window.ResizeObserver = class ResizeObserver {
4+
observe() {}
5+
unobserve() {}
6+
disconnect() {}
7+
};

0 commit comments

Comments
 (0)