Skip to content

Commit

Permalink
Merge pull request #402 from pagopa/feat/OI-255-vitest-porting
Browse files Browse the repository at this point in the history
feat: [OI-255] porting to vitest
  • Loading branch information
sebbalex authored Oct 2, 2024
2 parents ee082a4 + 7ade0de commit ac600c9
Show file tree
Hide file tree
Showing 11 changed files with 661 additions and 157 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ gradle-app.setting
# Frontend stuff
src/oneid/oneid-ecs-core/src/main/webui/node_modules
src/oneid/oneid-ecs-core/src/main/webui/build
src/oneid/oneid-ecs-core/src/main/webui/coverage
src/oneid/oneid-ecs-core/.quinoa

# Test stuff
Expand Down
1 change: 0 additions & 1 deletion src/oneid/oneid-ecs-core/src/main/webui/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export default [
'locales/locales.ts',
'**/__tests__/**/*',
'eslint.config.mjs',
'jest.config.js',
'craco.config.js',
'definitions/*',
'src/react-app-env.d.ts',
Expand Down
6 changes: 0 additions & 6 deletions src/oneid/oneid-ecs-core/src/main/webui/jest.config.js

This file was deleted.

18 changes: 6 additions & 12 deletions src/oneid/oneid-ecs-core/src/main/webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test -i",
"test:coverage": "craco test -i --coverage",
"test": "vitest",
"test:coverage": "vitest --coverage",
"eject": "craco eject",
"lint-autofix": "eslint --fix",
"lint": "eslint",
Expand Down Expand Up @@ -69,26 +69,20 @@
"@testing-library/react": "^16.0.0",
"@testing-library/user-event": "^14.5.2",
"@types/history": "^5.0.0",
"@types/jest": "^29.5.12",
"@types/react-router": "^5.1.20",
"@typescript-eslint/eslint-plugin": "^7.14.1",
"@typescript-eslint/parser": "^7.14.1",
"@vitest/coverage-v8": "^2.1.1",
"dotenv": "^16.4.5",
"eslint": "9.5.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-functional": "^6.6.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-react": "^7.34.2",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-sonarjs": "^1.0.3",
"prettier": "^3.3.2"
},
"jest": {
"coveragePathIgnorePatterns": [
"src/index.tsx",
"src/reportWebVitals.ts",
"src/utils/constants.ts",
"src/utils/IDPS.ts"
]
"prettier": "^3.3.2",
"vitest": "^2.1.1"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import React from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import Login from '../Login';
import { ENV } from '../../../utils/env';
import './../../../locale';
import { MemoryRouter } from 'react-router-dom';
import { productId2ProductTitle } from '../../../utils/src/lib/utils/productId2ProductTitle';
import { vi } from 'vitest';

const oldWindowLocation = global.window.location;

beforeAll(() => {
// eslint-disable-next-line functional/immutable-data
Object.defineProperty(window, 'location', { value: { assign: jest.fn() } });
Object.defineProperty(window, 'location', { value: { assign: vi.fn() } });
});
afterAll(() => {
// eslint-disable-next-line functional/immutable-data
Object.defineProperty(window, 'location', { value: oldWindowLocation });
});

jest.spyOn(URLSearchParams.prototype, 'get');
vi.spyOn(URLSearchParams.prototype, 'get');

global.window.open = jest.fn();
global.window.open = vi.fn();

test('Test: Session not found while trying to access at onboarding flow product: "Onboarding" Login is displayed', async () => {
const productIds = [
Expand Down Expand Up @@ -59,7 +59,7 @@ test('Test: Session not found while trying to access at onboarding flow product:
test('Test: Trying to access the login with SPID', () => {
render(<Login />);
const buttonSpid = document.getElementById('spidButton');
fireEvent.click(buttonSpid);
fireEvent.click(buttonSpid as HTMLElement);
});

test('Test: Trying to access the login with CIE', () => {
Expand All @@ -69,7 +69,7 @@ test('Test: Trying to access the login with CIE', () => {
});
fireEvent.click(buttonCIE);
expect(global.window.location.assign).toHaveBeenCalledWith(
`${ENV.URL_API.AUTHORIZE}?idp=xx_servizicie_test`
`${ENV.URL_API.AUTHORIZE}?idp=${ENV.SPID_CIE_ENTITY_ID}`
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import SpidSelect from '../SpidSelect';
import { ENV } from '../../../utils/env';
import { vi } from 'vitest';

const oldWindowLocation = global.window.location;
beforeAll(() => {
// eslint-disable-next-line functional/immutable-data
Object.defineProperty(window, 'location', { value: { assign: jest.fn() } });
Object.defineProperty(window, 'location', { value: { assign: vi.fn() } });
});
afterAll(() => {
// eslint-disable-next-line functional/immutable-data
Expand All @@ -19,11 +19,11 @@ const idpList = {
test('go to the spid url', () => {
render(<SpidSelect onBack={() => {}} idpList={idpList} />);

idpList.identityProviders.forEach((element, i) => {
idpList.identityProviders.forEach((element) => {
const spidImg = screen.getByAltText(element.name);
const spidSpan = spidImg.parentNode;
const spidButton = spidSpan?.parentNode;
fireEvent.click(spidButton);
fireEvent.click(spidButton as Element);
const id = element.entityID;
expect(global.window.location.assign).toHaveBeenCalledWith(
ENV.URL_API.AUTHORIZE + '?idp=' + encodeURIComponent(id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import { render, waitFor, screen } from '@testing-library/react';
import LoginError from '../LoginError';
import { ROUTE_LOGIN_ERROR } from '../../../utils/constants';
import './../../../locale';
import { vi } from 'vitest';

const oldWindowLocation = global.window.location;

beforeAll(() => {
Object.defineProperty(window, 'location', { value: { assign: jest.fn() } });
Object.defineProperty(window, 'location', { value: { assign: vi.fn() } });
});
afterAll(() => {
Object.defineProperty(window, 'location', { value: oldWindowLocation });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import { render } from '@testing-library/react';
import Logout from '../Logout';
import { ROUTE_LOGIN } from '../../../utils/constants';
import { vi } from 'vitest';

const oldWindowLocation = global.window.location;

beforeAll(() => {
Object.defineProperty(window, 'location', { value: { assign: jest.fn() } });
Object.defineProperty(window, 'location', { value: { assign: vi.fn() } });
});
afterAll(() => {
Object.defineProperty(window, 'location', { value: oldWindowLocation });
Expand Down
2 changes: 1 addition & 1 deletion src/oneid/oneid-ecs-core/src/main/webui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true
},
"include": ["src/**/*"],
"include": ["src/**/*", "vitest.config.mts" ],
"exclude": ["__mocks__", "dist", "build", "node_modules", "Dangerfile.ts"]
}
39 changes: 39 additions & 0 deletions src/oneid/oneid-ecs-core/src/main/webui/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { configDefaults, defineConfig } from 'vitest/config';
import { config } from 'dotenv';

export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
clearMocks: true,
watch: false,
silent: true,
coverage: {
provider: 'v8',
reportOnFailure: true,
exclude: [
...configDefaults.exclude,
'**/*.test.ts?(x)',
'src/__tests__/',
'src/index.tsx',
'src/reportWebVitals',
'src/utils/constants.ts',
'src/global.d.ts',
],
include: ['src/**/*.ts?(x)'],
thresholds: {
lines: 80,
branches: 80,
},
},
env: {
...config({ path: './.env.test.local' }).parsed,
},
include: ['**/*.test.ts?(x)'],
},
resolve: {
alias: {
// Add all your absolute paths here
},
},
});
Loading

0 comments on commit ac600c9

Please sign in to comment.