Skip to content

Commit c6fc4c2

Browse files
Migrate project to use Jest (#371)
* Migrate project to use Jest * Remove karma and jasmine dependencies * Exclude playground from coverage
1 parent 7a5d2d5 commit c6fc4c2

20 files changed

+26109
-21360
lines changed

jest.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
projects: [
3+
'<rootDir>/projects/auth0-angular',
4+
'<rootDir>/projects/playground',
5+
],
6+
};

package-lock.json

Lines changed: 25576 additions & 21085 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"build:schematics": "npm run build --prefix projects/auth0-angular",
1414
"prepublishOnly": "node scripts/prepublish-stopper.js",
1515
"postbuild": "npm run docs",
16-
"test": "ng test",
17-
"test:ci": "ng test --no-watch --no-progress --browsers=ChromeHeadlessCI --code-coverage",
16+
"test": "jest",
17+
"test:ci": "jest --coverage --silent",
1818
"e2e": "ng e2e",
1919
"e2e:headless": "ng e2e --no-watch --headless",
2020
"e2e:ci": "concurrently --raw --kill-others --success first npm:start:local:oidc npm:e2e:headless",
@@ -53,6 +53,7 @@
5353
"@briebug/cypress-schematic": "^4.4.0",
5454
"@types/jasmine": "^3.6.2",
5555
"@types/jasminewd2": "~2.0.3",
56+
"@types/jest": "^29.2.2",
5657
"@types/node": "^12.19.8",
5758
"@typescript-eslint/eslint-plugin": "5.27.1",
5859
"@typescript-eslint/parser": "5.27.1",
@@ -66,15 +67,9 @@
6667
"eslint-plugin-prefer-arrow": "latest",
6768
"express-jwt": "^8.3.0",
6869
"husky": "^4.3.8",
69-
"jasmine-core": "^3.6.0",
70-
"jasmine-spec-reporter": "~5.0.0",
70+
"jest": "^28.1.3",
71+
"jest-preset-angular": "^12.2.2",
7172
"jwks-rsa": "^3.0.0",
72-
"karma": "~6.3.16",
73-
"karma-chrome-launcher": "~3.1.0",
74-
"karma-coverage": "~2.2.0",
75-
"karma-jasmine": "^4.0.1",
76-
"karma-jasmine-html-reporter": "^1.5.0",
77-
"karma-junit-reporter": "^2.0.1",
7873
"moment": "^2.29.4",
7974
"ng-packagr": "^13.3.1",
8075
"oidc-provider": "^7.6.0",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: 'auth0-angular',
4+
preset: 'jest-preset-angular',
5+
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
6+
globals: {
7+
'ts-jest': {
8+
tsconfig: '<rootDir>/tsconfig.spec.json',
9+
stringifyContentPathRegex: '\\.(html|svg)$',
10+
},
11+
},
12+
coverageDirectory: '../../coverage/auth0-angular',
13+
transform: {
14+
'^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
15+
},
16+
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
17+
snapshotSerializers: [
18+
'jest-preset-angular/build/serializers/no-ng-attributes',
19+
'jest-preset-angular/build/serializers/ng-snapshot',
20+
'jest-preset-angular/build/serializers/html-comment',
21+
],
22+
};

projects/auth0-angular/src/lib/abstract-navigator.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('RouteNavigator', () => {
1919
navigator = TestBed.inject(AbstractNavigator);
2020

2121
const location = TestBed.inject(Location);
22-
replaceStateSpy = spyOn(location, 'replaceState');
22+
replaceStateSpy = jest.spyOn(location, 'replaceState');
2323
});
2424

2525
it('should be created', () => {
@@ -49,7 +49,7 @@ describe('RouteNavigator', () => {
4949
navigator = TestBed.inject(AbstractNavigator);
5050

5151
const location = TestBed.inject(Location);
52-
replaceStateSpy = spyOn(location, 'replaceState');
52+
replaceStateSpy = jest.spyOn(location, 'replaceState');
5353
});
5454

5555
it('should use the router if available', fakeAsync(() => {

projects/auth0-angular/src/lib/auth.client.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { AuthConfig, AuthClientConfig } from './auth.config';
22
import { Auth0ClientFactory } from './auth.client';
33

4+
const mockWindow = global as any;
5+
6+
mockWindow.crypto = {
7+
subtle: {
8+
digest: () => 'foo',
9+
},
10+
getRandomValues() {
11+
return '123';
12+
},
13+
};
14+
415
describe('Auth0ClientFactory', () => {
516
describe('createClient', () => {
617
it('creates a new instance of Auth0Client', () => {

projects/auth0-angular/src/lib/auth.guard.spec.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { of } from 'rxjs';
22
import { AuthGuard } from './auth.guard';
3+
import { expect } from '@jest/globals';
34

45
describe('AuthGuard', () => {
56
let guard: AuthGuard;
@@ -10,10 +11,10 @@ describe('AuthGuard', () => {
1011
it('should return true for a logged in user', () => {
1112
const authServiceMock: any = {
1213
isAuthenticated$: of(true),
13-
loginWithRedirect: jasmine.createSpy('loginWithRedirect'),
14+
loginWithRedirect: jest.fn(),
1415
};
1516
guard = new AuthGuard(authServiceMock);
16-
const listener = jasmine.createSpy();
17+
const listener = jest.fn();
1718
guard.canActivate(routeMock, routeStateMock).subscribe(listener);
1819
expect(authServiceMock.loginWithRedirect).not.toHaveBeenCalled();
1920
expect(listener).toHaveBeenCalledWith(true);
@@ -22,7 +23,7 @@ describe('AuthGuard', () => {
2223
it('should redirect a logged out user', () => {
2324
const authServiceMock: any = {
2425
isAuthenticated$: of(false),
25-
loginWithRedirect: jasmine.createSpy('loginWithRedirect'),
26+
loginWithRedirect: jest.fn(),
2627
};
2728
guard = new AuthGuard(authServiceMock);
2829
guard.canActivate(routeMock, routeStateMock).subscribe();
@@ -36,10 +37,10 @@ describe('AuthGuard', () => {
3637
it('should return true for a logged in user', () => {
3738
const authServiceMock: any = {
3839
isAuthenticated$: of(true),
39-
loginWithRedirect: jasmine.createSpy('loginWithRedirect'),
40+
loginWithRedirect: jest.fn(),
4041
};
4142
guard = new AuthGuard(authServiceMock);
42-
const listener = jasmine.createSpy();
43+
const listener = jest.fn();
4344
guard.canActivateChild(routeMock, routeStateMock).subscribe(listener);
4445
expect(authServiceMock.loginWithRedirect).not.toHaveBeenCalled();
4546
expect(listener).toHaveBeenCalledWith(true);
@@ -48,7 +49,7 @@ describe('AuthGuard', () => {
4849
it('should redirect a logged out user', () => {
4950
const authServiceMock: any = {
5051
isAuthenticated$: of(false),
51-
loginWithRedirect: jasmine.createSpy('loginWithRedirect'),
52+
loginWithRedirect: jest.fn(),
5253
};
5354
guard = new AuthGuard(authServiceMock);
5455
guard.canActivateChild(routeMock, routeStateMock).subscribe();
@@ -64,7 +65,7 @@ describe('AuthGuard', () => {
6465
isAuthenticated$: of(true),
6566
};
6667
guard = new AuthGuard(authServiceMock);
67-
const listener = jasmine.createSpy();
68+
const listener = jest.fn();
6869
guard.canLoad(routeMock, []).subscribe(listener);
6970
expect(listener).toHaveBeenCalledWith(true);
7071
});
@@ -74,7 +75,7 @@ describe('AuthGuard', () => {
7475
isAuthenticated$: of(false),
7576
};
7677
guard = new AuthGuard(authServiceMock);
77-
const listener = jasmine.createSpy();
78+
const listener = jest.fn();
7879
guard.canLoad(routeMock, []).subscribe(listener);
7980
expect(listener).toHaveBeenCalledWith(false);
8081
});

0 commit comments

Comments
 (0)