From cb6f8062749189e979ff4ae6f631657515d137a1 Mon Sep 17 00:00:00 2001 From: Giuseppe Leo Date: Tue, 12 Dec 2023 11:35:38 +0100 Subject: [PATCH] Correct issue with TS after tsconfig split with unit tests (#9686) * Correct file test naming * Correct test files import and names * Restructure TS configuration with shared path * Copied TS configuration for shell due current architecture state --- babel.config.js | 1 + cypress/globals.d.ts | 96 ++++++++++--------- cypress/tsconfig.json | 16 +++- jest.config.js | 58 +++++------ shell/babel.config.js | 8 +- ...scription.ts => NameNsDescription.test.ts} | 0 .../__tests__/PolicyRuleTarget.spec.ts | 2 +- .../utils/{selectors.ts => selectors.test.ts} | 0 ...Command.tests.ts => CustomCommand.test.ts} | 0 .../{actions.spec.ts => actions.test.ts} | 0 .../{getters.spec.ts => getters.test.ts} | 0 .../{mutations.spec.ts => mutations.test.ts} | 2 +- shell/tsconfig.default.json | 46 --------- shell/tsconfig.json | 44 +++++++-- shell/tsconfig.paths.json | 18 ++++ storybook/tsconfig.json | 18 +--- tsconfig.json | 40 +++++++- tsconfig.paths.json | 21 ++++ tsconfig.test.json | 16 ---- 19 files changed, 218 insertions(+), 168 deletions(-) rename shell/components/form/__tests__/{NameNsDescription.ts => NameNsDescription.test.ts} (100%) rename shell/edit/networking.k8s.io.networkpolicy/__tests__/utils/{selectors.ts => selectors.test.ts} (100%) rename shell/edit/provisioning.cattle.io.cluster/__tests__/{CustomCommand.tests.ts => CustomCommand.test.ts} (100%) rename shell/plugins/dashboard-store/__tests__/{actions.spec.ts => actions.test.ts} (100%) rename shell/plugins/dashboard-store/__tests__/{getters.spec.ts => getters.test.ts} (100%) rename shell/plugins/dashboard-store/__tests__/{mutations.spec.ts => mutations.test.ts} (99%) delete mode 100644 shell/tsconfig.default.json create mode 100644 shell/tsconfig.paths.json create mode 100644 tsconfig.paths.json delete mode 100644 tsconfig.test.json diff --git a/babel.config.js b/babel.config.js index 3d78f1703f..a4b2df9bb3 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1 +1,2 @@ +// NOTE: We import the babel config from the shell folder due to "Validate Plugin build system" setup set in this way module.exports = require('./shell/babel.config.js'); diff --git a/cypress/globals.d.ts b/cypress/globals.d.ts index 1a53aa1baa..e16edb15a9 100644 --- a/cypress/globals.d.ts +++ b/cypress/globals.d.ts @@ -19,63 +19,65 @@ export type CreateUserParams = { } } -// eslint-disable-next-line no-unused-vars -declare namespace Cypress { - interface Chainable { +declare global { + // eslint-disable-next-line no-unused-vars + namespace Cypress { + interface Chainable { - state(state: any): any; + state(state: any): any; - login(username?: string, password?: string, cacheSession?: boolean): Chainable; - logout(): Chainable; - byLabel(label: string): Chainable; + login(username?: string, password?: string, cacheSession?: boolean): Chainable; + logout(): Chainable; + byLabel(label: string): Chainable; - createUser(params: CreateUserParams): Chainable; - setGlobalRoleBinding(userId: string, role: string): Chainable; - setClusterRoleBinding(clusterId: string, userPrincipalId: string, role: string): Chainable; - setProjectRoleBinding(clusterId: string, userPrincipalId: string, projectName: string, role: string): Chainable; - getProjectByName(clusterId: string, projectName: string): Chainable; - createProject(projName: string, clusterId: string, userId: string): Chainable; - createNamespace(nsName: string, projId: string): Chainable; + createUser(params: CreateUserParams): Chainable; + setGlobalRoleBinding(userId: string, role: string): Chainable; + setClusterRoleBinding(clusterId: string, userPrincipalId: string, role: string): Chainable; + setProjectRoleBinding(clusterId: string, userPrincipalId: string, projectName: string, role: string): Chainable; + getProjectByName(clusterId: string, projectName: string): Chainable; + createProject(projName: string, clusterId: string, userId: string): Chainable; + createNamespace(nsName: string, projId: string): Chainable; - getRancherResource(prefix: 'v3' | 'v1', resourceType: string, resourceId?: string, expectedStatusCode: string): Chainable; - setRancherResource(prefix: 'v3' | 'v1', resourceType: string, resourceId: string, body: string): Chainable; - deleteRancherResource(prefix: 'v3' | 'v1', resourceType: string, resourceId: string): Chainable; + getRancherResource(prefix: 'v3' | 'v1', resourceType: string, resourceId?: string, expectedStatusCode: string): Chainable; + setRancherResource(prefix: 'v3' | 'v1', resourceType: string, resourceId: string, body: string): Chainable; + deleteRancherResource(prefix: 'v3' | 'v1', resourceType: string, resourceId: string): Chainable; - /** - * Wrapper for cy.get() to simply define the data-testid value that allows you to pass a matcher to find the element. - * @param id Value used for the data-testid attribute of the element. - * @param matcher Matching character used for attribute value: - * - `$`: Suffixed with this value - * - `^`: Prefixed with this value - * - `~`: Contains this value as whitespace separated words - * - `*`: Contains this value - */ - getId(id: string, matcher?: Matcher): Chainable; + /** + * Wrapper for cy.get() to simply define the data-testid value that allows you to pass a matcher to find the element. + * @param id Value used for the data-testid attribute of the element. + * @param matcher Matching character used for attribute value: + * - `$`: Suffixed with this value + * - `^`: Prefixed with this value + * - `~`: Contains this value as whitespace separated words + * - `*`: Contains this value + */ + getId(id: string, matcher?: Matcher): Chainable; - /** - * Wrapper for cy.find() to simply define the data-testid value that allows you to pass a matcher to find the element. - * @param id Value used for the data-testid attribute of the element. - * @param matcher Matching character used for attribute value: - * - `$`: Suffixed with this value - * - `^`: Prefixed with this value - * - `~`: Contains this value as whitespace separated words - * - `*`: Contains this value - */ - findId(id: string, matcher?: Matcher): Chainable; + /** + * Wrapper for cy.find() to simply define the data-testid value that allows you to pass a matcher to find the element. + * @param id Value used for the data-testid attribute of the element. + * @param matcher Matching character used for attribute value: + * - `$`: Suffixed with this value + * - `^`: Prefixed with this value + * - `~`: Contains this value as whitespace separated words + * - `*`: Contains this value + */ + findId(id: string, matcher?: Matcher): Chainable; - /** - * Override user preferences to default values, allowing to pass custom preferences for a deterministic scenario - * Leave empty for reset to default values - */ - // eslint-disable-next-line no-undef - userPreferences(preferences?: Partial): Chainable; + /** + * Override user preferences to default values, allowing to pass custom preferences for a deterministic scenario + * Leave empty for reset to default values + */ + // eslint-disable-next-line no-undef + userPreferences(preferences?: Partial): Chainable; - requestBase64Image(url: string): Chainable; + requestBase64Image(url: string): Chainable; - keyboardControls(triggerKeys: Partial, count: number): Chainable; + keyboardControls(triggerKeys: Partial, count: number): Chainable; - interceptAllRequests(verbs: Verbs, urls: string[], timeout: number): Chainable; + interceptAllRequests(verbs: Verbs, urls: string[], timeout: number): Chainable; - iFrame(): Chainable; + iFrame(): Chainable; + } } } diff --git a/cypress/tsconfig.json b/cypress/tsconfig.json index 6a7306a6c8..19ca6aeb9c 100644 --- a/cypress/tsconfig.json +++ b/cypress/tsconfig.json @@ -1,6 +1,20 @@ { - "extends": "../shell/tsconfig.default.json", + "extends": "../tsconfig.paths.json", + "target": "ES2018", + "module": "ES2020", + "moduleResolution": "Node", + "lib": [ + "ES2020", + "ES2020.AsyncIterable", + "DOM" + ], + "allowJs": true, + "sourceMap": true, + "strict": true, + "noEmit": false, "compilerOptions": { + "esModuleInterop": true, + "baseUrl": "..", "noEmit": true, "types": [ "cypress", diff --git a/jest.config.js b/jest.config.js index fa0fed2d08..55ca6fe439 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,13 +1,17 @@ module.exports = { + preset: 'ts-jest', testEnvironment: 'jsdom', setupFilesAfterEnv: ['./jest.setup.js'], - modulePaths: [ - '' - ], + watchman: false, + // tell Jest to handle `*.vue` files moduleFileExtensions: ['js', 'json', 'vue', 'ts'], - watchman: false, - moduleNameMapper: { + + // Paths + // NOTE: Docs configuration does not work for our environment + // https://kulshekhar.github.io/ts-jest/docs/27.1/getting-started/paths-mapping#jest-config-with-helper + modulePaths: [''], + moduleNameMapper: { '^~/(.*)$': '/$1', '^~~/(.*)$': '/$1', '^@/(.*)$': '/$1', @@ -15,21 +19,6 @@ module.exports = { '@pkg/(.*)': '/pkg/$1', '@components/(.*)': '/pkg/rancher-components/src/components/$1', }, - transform: { - '^.+\\.js$': '/node_modules/babel-jest', // process js with `babel-jest` - '.*\\.(vue)$': '/node_modules/@vue/vue2-jest', // process `*.vue` files with `vue-jest` - '^.+\\.tsx?$': 'ts-jest', // process `*.ts` files with `ts-jest` - '^.+\\.svg$': '/svgTransform.js' // to mock `*.svg` files - }, - snapshotSerializers: ['/node_modules/jest-serializer-vue'], - collectCoverage: false, - collectCoverageFrom: [ - '/shell/**/*.{vue,ts,js}', - '/pkg/rancher-components/src/components/**/*.{vue,ts,js}', - '!/shell/scripts/', - '!/.nuxt', - '!/.nuxt-prod', - ], modulePathIgnorePatterns: [ '/cypress/', '/scripts/', @@ -44,13 +33,26 @@ module.exports = { '/node_modules/', '(/.*)*/__tests__/utils/', ], - coverageDirectory: '/coverage/unit', - coverageReporters: ['json', 'text-summary'], - globals: { - 'ts-jest': { - isolatedModules: true, - tsconfig: 'tsconfig.test.json' - } + + // Babel + transform: { + '^.+\\.js$': '/node_modules/babel-jest', // process js with `babel-jest` + '.*\\.(vue)$': '/node_modules/@vue/vue2-jest', // process `*.vue` files with `vue-jest` + '^.+\\.tsx?$': 'ts-jest', // process `*.ts` files with `ts-jest` + '^.+\\.svg$': '/svgTransform.js' // to mock `*.svg` files }, - preset: 'ts-jest' + snapshotSerializers: ['/node_modules/jest-serializer-vue'], + + // Coverage + coverageDirectory: '/coverage/unit', + coverageReporters: ['json', 'text-summary'], + collectCoverage: false, + collectCoverageFrom: [ + '/shell/**/*.{vue,ts,js}', + '/pkg/rancher-components/src/components/**/*.{vue,ts,js}', + '!/shell/scripts/', + ], + + // Globals + globals: { 'ts-jest': { isolatedModules: true } }, }; diff --git a/shell/babel.config.js b/shell/babel.config.js index 806bea6a70..cb35349877 100644 --- a/shell/babel.config.js +++ b/shell/babel.config.js @@ -10,7 +10,13 @@ module.exports = function(api) { { targets: { node: 'current' } } ] ]; - const env = { test: { presets: [['@babel/env', { targets: { node: 'current' } }]] } }; + const env = { + test: { + presets: [[ + '@babel/env', { targets: { node: 'current' } } + ]] + } + }; const plugins = []; diff --git a/shell/components/form/__tests__/NameNsDescription.ts b/shell/components/form/__tests__/NameNsDescription.test.ts similarity index 100% rename from shell/components/form/__tests__/NameNsDescription.ts rename to shell/components/form/__tests__/NameNsDescription.test.ts diff --git a/shell/edit/networking.k8s.io.networkpolicy/__tests__/PolicyRuleTarget.spec.ts b/shell/edit/networking.k8s.io.networkpolicy/__tests__/PolicyRuleTarget.spec.ts index 7439165cc6..c51300826f 100644 --- a/shell/edit/networking.k8s.io.networkpolicy/__tests__/PolicyRuleTarget.spec.ts +++ b/shell/edit/networking.k8s.io.networkpolicy/__tests__/PolicyRuleTarget.spec.ts @@ -2,7 +2,7 @@ import { mount } from '@vue/test-utils'; import PolicyRuleTarget from '@shell/edit/networking.k8s.io.networkpolicy/PolicyRuleTarget'; import { cleanHtmlDirective } from '@shell/plugins/clean-html-directive'; import mock from '@shell/edit/networking.k8s.io.networkpolicy/__tests__/utils/mock.json'; -import { PolicyRuleTargetSelectors } from '@shell/edit/networking.k8s.io.networkpolicy/__tests__/utils/selectors'; +import { PolicyRuleTargetSelectors } from '@shell/edit/networking.k8s.io.networkpolicy/__tests__/utils/selectors.test.ts'; describe.each([ 'view', diff --git a/shell/edit/networking.k8s.io.networkpolicy/__tests__/utils/selectors.ts b/shell/edit/networking.k8s.io.networkpolicy/__tests__/utils/selectors.test.ts similarity index 100% rename from shell/edit/networking.k8s.io.networkpolicy/__tests__/utils/selectors.ts rename to shell/edit/networking.k8s.io.networkpolicy/__tests__/utils/selectors.test.ts diff --git a/shell/edit/provisioning.cattle.io.cluster/__tests__/CustomCommand.tests.ts b/shell/edit/provisioning.cattle.io.cluster/__tests__/CustomCommand.test.ts similarity index 100% rename from shell/edit/provisioning.cattle.io.cluster/__tests__/CustomCommand.tests.ts rename to shell/edit/provisioning.cattle.io.cluster/__tests__/CustomCommand.test.ts diff --git a/shell/plugins/dashboard-store/__tests__/actions.spec.ts b/shell/plugins/dashboard-store/__tests__/actions.test.ts similarity index 100% rename from shell/plugins/dashboard-store/__tests__/actions.spec.ts rename to shell/plugins/dashboard-store/__tests__/actions.test.ts diff --git a/shell/plugins/dashboard-store/__tests__/getters.spec.ts b/shell/plugins/dashboard-store/__tests__/getters.test.ts similarity index 100% rename from shell/plugins/dashboard-store/__tests__/getters.spec.ts rename to shell/plugins/dashboard-store/__tests__/getters.test.ts diff --git a/shell/plugins/dashboard-store/__tests__/mutations.spec.ts b/shell/plugins/dashboard-store/__tests__/mutations.test.ts similarity index 99% rename from shell/plugins/dashboard-store/__tests__/mutations.spec.ts rename to shell/plugins/dashboard-store/__tests__/mutations.test.ts index 7bdc8d6edd..e07f7cc3cb 100644 --- a/shell/plugins/dashboard-store/__tests__/mutations.spec.ts +++ b/shell/plugins/dashboard-store/__tests__/mutations.test.ts @@ -1,4 +1,4 @@ -import { batchChanges } from '@shell/plugins/dashboard-store/mutations'; +import { batchChanges } from '@shell/plugins/dashboard-store/mutations.js'; import { POD, WORKLOAD_TYPES } from '@shell/config/types'; import Resource from '@shell/plugins/dashboard-store/resource-class'; diff --git a/shell/tsconfig.default.json b/shell/tsconfig.default.json deleted file mode 100644 index dd2b807812..0000000000 --- a/shell/tsconfig.default.json +++ /dev/null @@ -1,46 +0,0 @@ -{ - "compilerOptions": { - "target": "ES2018", - "module": "ESNext", - "moduleResolution": "Node", - "lib": [ - "ESNext", - "ESNext.AsyncIterable", - "DOM" - ], - "esModuleInterop": true, - "allowJs": true, - "sourceMap": true, - "strict": true, - "noEmit": false, - "baseUrl": ".", - "rootDir": ".", - "paths": { - "~/*": [ - "../*" - ], - "@/*": [ - "../*" - ], - "@shell/*": [ - "../shell/*" - ], - "@pkg/*": [ - "../shell/pkg/*" - ] - }, - "typeRoots": [ - "./node_modules/@types" - ], - }, - "exclude": [ - "node_modules", - ".nuxt", - "dist", - "dist-pkg", - "shell/creators", - "shell/scripts", - "script/standalone", - "docusaurus" - ] -} \ No newline at end of file diff --git a/shell/tsconfig.json b/shell/tsconfig.json index 5f68742ec3..2ae14756b4 100644 --- a/shell/tsconfig.json +++ b/shell/tsconfig.json @@ -1,16 +1,42 @@ { - "extends": "./tsconfig.default.json", + "extends": "./tsconfig.paths.json", "compilerOptions": { + "target": "ES2018", + "module": "ESNext", + "moduleResolution": "Node", + "lib": [ + "ESNext", + "ESNext.AsyncIterable", + "DOM" + ], + "allowJs": true, + "sourceMap": true, + "strict": true, + "noEmit": false, + "esModuleInterop": true, + "baseUrl": ".", + "rootDir": ".", "types": [ - "@types/node", - "@types/jest", - "@nuxt/types", - ] + "node", + "jest", + ], + "typeRoots": [ + "./node_modules/@types", + "../node_modules/@types" + ], }, "exclude": [ - "cypress", - "./cypress.config.ts", - "**/*.test.ts", - "**/*.spec.ts" + "node_modules", + ".nuxt", + "dist", + "dist-pkg", + "../dist", + "../dist-pkg", + "../creators", + "../scripts", + "../cypress", + "../cypress.config.ts", + "../docusaurus", + "../**/*.spec.ts" ] } \ No newline at end of file diff --git a/shell/tsconfig.paths.json b/shell/tsconfig.paths.json new file mode 100644 index 0000000000..759c1c28f5 --- /dev/null +++ b/shell/tsconfig.paths.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "paths": { + "~/*": [ + "../*" + ], + "@/*": [ + "../*" + ], + "@shell/*": [ + "../shell/*" + ], + "@pkg/*": [ + "../shell/pkg/*" + ] + }, + }, +} \ No newline at end of file diff --git a/storybook/tsconfig.json b/storybook/tsconfig.json index ee655253f7..3c43903cfd 100644 --- a/storybook/tsconfig.json +++ b/storybook/tsconfig.json @@ -1,19 +1,3 @@ { - "extends": "../tsconfig.json", - "compilerOptions": { - "types": ["@types/node", "@types/jest"] - }, - "exclude": [ - "node_modules", - ".nuxt", - "dist", - "dist-pkg", - "cypress", - "shell/creators", - "shell/scripts", - "cypress", - "./cypress.config.ts", - "docusaurus", - "script/standalone" - ] + "extends": "../tsconfig.json" } diff --git a/tsconfig.json b/tsconfig.json index da021ae9b1..8b4116935f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,3 +1,41 @@ { - "extends": "./shell/tsconfig.default.json", + "extends": "./tsconfig.paths.json", + "compilerOptions": { + "target": "ES2018", + "module": "ESNext", + "moduleResolution": "Node", + "lib": [ + "ESNext", + "ESNext.AsyncIterable", + "DOM" + ], + "allowJs": true, + "sourceMap": true, + "strict": true, + "noEmit": false, + "esModuleInterop": true, + "baseUrl": ".", + "rootDir": ".", + "types": [ + "node", + "jest", + ], + "typeRoots": [ + "./node_modules/@types" + ], + }, + "exclude": [ + "node_modules", + ".nuxt", + "dist", + "dist-pkg", + "cypress", + "shell/creators", + "shell/scripts", + "cypress", + "./cypress.config.ts", + "docusaurus", + "scripts/standalone", + "**/*.spec.ts" + ] } \ No newline at end of file diff --git a/tsconfig.paths.json b/tsconfig.paths.json new file mode 100644 index 0000000000..a994127b95 --- /dev/null +++ b/tsconfig.paths.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "paths": { + "~/*": [ + "*" + ], + "@/*": [ + "*" + ], + "@shell/*": [ + "shell/*" + ], + "@pkg/*": [ + "shell/pkg/*" + ], + "@components/*": [ + "pkg/rancher-components/src/components/*" + ] + }, + }, +} \ No newline at end of file diff --git a/tsconfig.test.json b/tsconfig.test.json deleted file mode 100644 index a197d9a1af..0000000000 --- a/tsconfig.test.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "extends": "./shell/tsconfig.default.json", - "compilerOptions": { - "types": [ - "jest" - ] - }, - "include": [ - "./**/*.test.ts" - ], - "exclude": [ - "cypress", - "./cypress.config.ts", - "**/*.spec.ts" - ] -} \ No newline at end of file