From 1cb5ea23c747acf53857b18e9201c40b82d5f15b Mon Sep 17 00:00:00 2001 From: D048415 Date: Mon, 15 Jul 2024 10:51:56 +0200 Subject: [PATCH 01/10] add unit test TS template --- generators/dependencies.js | 1 + generators/qunit/index.js | 26 +++++++++++++++++++------- generators/qunit/templates/Test.ts | 6 ++++++ 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 generators/qunit/templates/Test.ts diff --git a/generators/dependencies.js b/generators/dependencies.js index eda2a56..668f804 100644 --- a/generators/dependencies.js +++ b/generators/dependencies.js @@ -4,6 +4,7 @@ export default { "@sap-ux/eslint-plugin-fiori-tools": "^0.2", "@sap/approuter": "latest", "@ui5/linter": "latest", + "@types/qunit": "2.5.4", "bestzip": "latest", "mbt": "^1", "npm-run-all": "^4", diff --git a/generators/qunit/index.js b/generators/qunit/index.js index ccac77b..fed8be2 100644 --- a/generators/qunit/index.js +++ b/generators/qunit/index.js @@ -1,6 +1,7 @@ import chalk from "chalk" import fs from "fs" import Generator from "yeoman-generator" +import dependencies from "../dependencies.js" import prompts from "./prompts.js" import { lookForParentUI5ProjectAndPrompt, @@ -33,15 +34,26 @@ export default class extends Generator { addPreviewMiddlewareTestConfig.call(this, "Qunit") - this.fs.copyTpl( - // for some reason this.templatePath() doesn't work here - path.join(__dirname, "templates/Test.js"), - this.destinationPath(`webapp/test/unit/${this.options.config.testName}Test.js`), - { testName: this.options.config.testName } - ) - const uimodulePackageJson = JSON.parse(fs.readFileSync(this.destinationPath("package.json"))) uimodulePackageJson.scripts["qunit"] = "fiori run --open test/unitTests.qunit.html" + + if (this.options.config.enableTypescript) { + this.fs.copyTpl( + // for some reason this.templatePath() doesn't work here + path.join(__dirname, "templates/Test.ts"), + this.destinationPath(`webapp/test/unit/${this.options.config.testName}Test.ts`), + {testName: this.options.config.testName} + ) + uimodulePackageJson["devDependencies"]["@types/qunit"] = dependencies["@types/qunit"] + } else { + this.fs.copyTpl( + // for some reason this.templatePath() doesn't work here + path.join(__dirname, "templates/Test.js"), + this.destinationPath(`webapp/test/unit/${this.options.config.testName}Test.js`), + {testName: this.options.config.testName} + ) + } + fs.writeFileSync(this.destinationPath("package.json"), JSON.stringify(uimodulePackageJson, null, 4)) } diff --git a/generators/qunit/templates/Test.ts b/generators/qunit/templates/Test.ts new file mode 100644 index 0000000..9bcde10 --- /dev/null +++ b/generators/qunit/templates/Test.ts @@ -0,0 +1,6 @@ +QUnit.module("<%= testName %> Test", {}); + +QUnit.test("It is just true", assert => { + // Assert + assert.strictEqual(true, true); +}); \ No newline at end of file From 74f4874e4746db8665f0bd0975ab6167e7f97934 Mon Sep 17 00:00:00 2001 From: D048415 Date: Mon, 15 Jul 2024 13:44:51 +0200 Subject: [PATCH 02/10] refactoring --- generators/qunit/index.js | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/generators/qunit/index.js b/generators/qunit/index.js index fed8be2..beeeec0 100644 --- a/generators/qunit/index.js +++ b/generators/qunit/index.js @@ -34,26 +34,18 @@ export default class extends Generator { addPreviewMiddlewareTestConfig.call(this, "Qunit") + this.fs.copyTpl( + // for some reason this.templatePath() doesn't work here + path.join(__dirname, `templates/Test.${this.options.config.enableTypescript ? "ts": "js"}`), + this.destinationPath(`webapp/test/unit/${this.options.config.testName}Test.${this.options.config.enableTypescript ? "ts": "js"}`), + {testName: this.options.config.testName} + ) + const uimodulePackageJson = JSON.parse(fs.readFileSync(this.destinationPath("package.json"))) uimodulePackageJson.scripts["qunit"] = "fiori run --open test/unitTests.qunit.html" - if (this.options.config.enableTypescript) { - this.fs.copyTpl( - // for some reason this.templatePath() doesn't work here - path.join(__dirname, "templates/Test.ts"), - this.destinationPath(`webapp/test/unit/${this.options.config.testName}Test.ts`), - {testName: this.options.config.testName} - ) uimodulePackageJson["devDependencies"]["@types/qunit"] = dependencies["@types/qunit"] - } else { - this.fs.copyTpl( - // for some reason this.templatePath() doesn't work here - path.join(__dirname, "templates/Test.js"), - this.destinationPath(`webapp/test/unit/${this.options.config.testName}Test.js`), - {testName: this.options.config.testName} - ) } - fs.writeFileSync(this.destinationPath("package.json"), JSON.stringify(uimodulePackageJson, null, 4)) } From 424d97e318a552d78993f1ae0dd1696c9bb430f9 Mon Sep 17 00:00:00 2001 From: D048415 Date: Mon, 15 Jul 2024 14:14:17 +0200 Subject: [PATCH 03/10] add opa5 test TS template --- generators/opa5/index.js | 8 +++---- generators/opa5/templates/Journey.ts | 29 +++++++++++++++++++++++++ generators/opa5/templates/pages/View.ts | 25 +++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 generators/opa5/templates/Journey.ts create mode 100644 generators/opa5/templates/pages/View.ts diff --git a/generators/opa5/index.js b/generators/opa5/index.js index 367b2df..0cc0e77 100644 --- a/generators/opa5/index.js +++ b/generators/opa5/index.js @@ -38,8 +38,8 @@ export default class extends Generator { this.fs.copyTpl( // for some reason this.templatePath() doesn't work here - path.join(__dirname, "templates/pages/View.js"), - this.destinationPath(`webapp/test/integration/pages/${this.options.config.viewName}.js`), + path.join(__dirname, `templates/pages/View.${this.options.config.enableTypescript ? "ts": "js"}`), + this.destinationPath(`webapp/test/integration/pages/${this.options.config.viewName}.${this.options.config.enableTypescript ? "ts": "js"}`), { viewName: this.options.config.viewName, uimoduleName: this.options.config.uimoduleName, @@ -48,8 +48,8 @@ export default class extends Generator { ) this.fs.copyTpl( // for some reason this.templatePath() doesn't work here - path.join(__dirname, "templates/Journey.js"), - this.destinationPath(`webapp/test/integration/${this.options.config.testName}Journey.js`), + path.join(__dirname, `templates/Journey.${this.options.config.enableTypescript ? "ts": "js"}`), + this.destinationPath(`webapp/test/integration/${this.options.config.testName}Journey.${this.options.config.enableTypescript ? "ts": "js"}`), { viewName: this.options.config.viewName, uimoduleName: this.options.config.uimoduleName, diff --git a/generators/opa5/templates/Journey.ts b/generators/opa5/templates/Journey.ts new file mode 100644 index 0000000..a042146 --- /dev/null +++ b/generators/opa5/templates/Journey.ts @@ -0,0 +1,29 @@ +import Opa5 from "sap/ui/test/Opa5"; +import opaTest from "sap/ui/test/opaQunit"; +import <%= viewName %>Page from "./pages/<%= viewName %>"; + +const onThe<%= viewName %>Page = new <%= viewName %>Page(); + +Opa5.extendConfig({ + viewNamespace: "<%= uimoduleName %>.view", + autoWait: true +}); + +QUnit.module("<%= viewName %>"); + +opaTest("Should have correct title", function() { + // Arrangements + onThe<%= viewName %>Page.iStartMyUIComponent({ + componentConfig: { + name: "<%= uimoduleName %>", + async: true + }, + hash: "<%= route %>" + }); + + // Assertions + onThe<%= viewName %>Page.theTitleShouldBeCorrect(); + + // Cleanup + onThe<%= viewName %>Page.iTeardownMyApp(); +}); diff --git a/generators/opa5/templates/pages/View.ts b/generators/opa5/templates/pages/View.ts new file mode 100644 index 0000000..c41f77d --- /dev/null +++ b/generators/opa5/templates/pages/View.ts @@ -0,0 +1,25 @@ +import Opa5 from "sap/ui/test/Opa5"; +import I18NText from "sap/ui/test/matchers/I18NText"; + +const viewName = "<%= viewName %>"; + +export default class <%= viewName %>Page extends Opa5 { + //Actions + + //Assertions + theTitleShouldBeCorrect(this: Opa5) { + this.waitFor({ + id: "page", + viewName, + matchers: new I18NText({ + key: '<%- route === "" ? "title" : route %>', + propertyName: 'title', + parameters: '<%- route === "" ? uimoduleName : viewName %>' + }), + success: function() { + Opa5.assert.ok(true, "The page has the correct title"); + }, + errorMessage: "The page does not have the correct title" + }); + } +} From 01882ac8823ba93458115128ae6570d8907eaff1 Mon Sep 17 00:00:00 2001 From: D048415 Date: Tue, 16 Jul 2024 10:35:50 +0200 Subject: [PATCH 04/10] delete hash --- generators/opa5/templates/Journey.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/generators/opa5/templates/Journey.ts b/generators/opa5/templates/Journey.ts index a042146..f600aed 100644 --- a/generators/opa5/templates/Journey.ts +++ b/generators/opa5/templates/Journey.ts @@ -17,8 +17,7 @@ opaTest("Should have correct title", function() { componentConfig: { name: "<%= uimoduleName %>", async: true - }, - hash: "<%= route %>" + } }); // Assertions From 574c1ca18f2bf789b335ec571b61e636a581a1ee Mon Sep 17 00:00:00 2001 From: D048415 Date: Thu, 18 Jul 2024 08:26:38 +0200 Subject: [PATCH 05/10] add qunit type to package.json and tsconfig --- generators/opa5/index.js | 7 +++++++ generators/qunit/index.js | 3 +++ 2 files changed, 10 insertions(+) diff --git a/generators/opa5/index.js b/generators/opa5/index.js index 0cc0e77..69f7a7c 100644 --- a/generators/opa5/index.js +++ b/generators/opa5/index.js @@ -9,6 +9,7 @@ import { } from "../helpers.js" import path, { dirname } from "path" import { fileURLToPath } from "url" +import dependencies from "../dependencies.js"; const __dirname = dirname(fileURLToPath(import.meta.url)) export default class extends Generator { @@ -59,6 +60,12 @@ export default class extends Generator { const uimodulePackageJson = JSON.parse(fs.readFileSync(this.destinationPath("package.json"))) uimodulePackageJson.scripts["opa5"] = "fiori run --open test/opaTests.qunit.html" + if (this.options.config.enableTypescript) { + uimodulePackageJson["devDependencies"]["@types/qunit"] = dependencies["@types/qunit"] + const tsconfigJson = JSON.parse(fs.readFileSync(this.destinationPath("tsconfig.json"))) + tsconfigJson.compilerOptions.types.includes("qunit") || tsconfigJson.compilerOptions.types.push( "qunit" ) + fs.writeFileSync(this.destinationPath("tsconfig.json"), JSON.stringify(tsconfigJson, null, 4)) + } fs.writeFileSync(this.destinationPath("package.json"), JSON.stringify(uimodulePackageJson, null, 4)) } diff --git a/generators/qunit/index.js b/generators/qunit/index.js index beeeec0..a2f3b47 100644 --- a/generators/qunit/index.js +++ b/generators/qunit/index.js @@ -45,6 +45,9 @@ export default class extends Generator { uimodulePackageJson.scripts["qunit"] = "fiori run --open test/unitTests.qunit.html" if (this.options.config.enableTypescript) { uimodulePackageJson["devDependencies"]["@types/qunit"] = dependencies["@types/qunit"] + const tsconfigJson = JSON.parse(fs.readFileSync(this.destinationPath("tsconfig.json"))) + tsconfigJson.compilerOptions.types.includes("qunit") || tsconfigJson.compilerOptions.types.push( "qunit" ) + fs.writeFileSync(this.destinationPath("tsconfig.json"), JSON.stringify(tsconfigJson, null, 4)) } fs.writeFileSync(this.destinationPath("package.json"), JSON.stringify(uimodulePackageJson, null, 4)) } From ca7f56c58b59cb247ebbd71d340eb4bddc6d382b Mon Sep 17 00:00:00 2001 From: D048415 Date: Wed, 24 Jul 2024 19:06:58 +0200 Subject: [PATCH 06/10] add void to jQuery promises --- generators/opa5/templates/Journey.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generators/opa5/templates/Journey.ts b/generators/opa5/templates/Journey.ts index f600aed..669e83b 100644 --- a/generators/opa5/templates/Journey.ts +++ b/generators/opa5/templates/Journey.ts @@ -13,7 +13,7 @@ QUnit.module("<%= viewName %>"); opaTest("Should have correct title", function() { // Arrangements - onThe<%= viewName %>Page.iStartMyUIComponent({ + void onThe<%= viewName %>Page.iStartMyUIComponent({ componentConfig: { name: "<%= uimoduleName %>", async: true @@ -24,5 +24,5 @@ opaTest("Should have correct title", function() { onThe<%= viewName %>Page.theTitleShouldBeCorrect(); // Cleanup - onThe<%= viewName %>Page.iTeardownMyApp(); + void onThe<%= viewName %>Page.iTeardownMyApp(); }); From 3371ba57fb2e51b994bca9b56ce45c5ae9341dc5 Mon Sep 17 00:00:00 2001 From: D048415 Date: Wed, 24 Jul 2024 19:08:14 +0200 Subject: [PATCH 07/10] fix indentation --- generators/dependencies.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/dependencies.js b/generators/dependencies.js index 67937be..ffd9728 100644 --- a/generators/dependencies.js +++ b/generators/dependencies.js @@ -4,7 +4,7 @@ export default { "@sap-ux/eslint-plugin-fiori-tools": "^0.2", "@sap/approuter": "latest", "@ui5/linter": "latest", - "@types/qunit": "2.5.4", + "@types/qunit": "2.5.4", "mbt": "^1", "rimraf": "latest", "OpenUI5": "1.120.13", From 55061d7060d2bc9b1053371c1030055111bab936 Mon Sep 17 00:00:00 2001 From: D048415 Date: Fri, 18 Oct 2024 10:20:20 +0200 Subject: [PATCH 08/10] add IDE project settings folders to gitignore --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 73007d2..7961cad 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,8 @@ test-output/ .DS_Store .marskman.toml + +# IntelliJ project settings +.idea +# VSCode project settings +.vscode \ No newline at end of file From 6075447d9536706e486fb4c01eec04514e809016 Mon Sep 17 00:00:00 2001 From: D048415 Date: Fri, 18 Oct 2024 10:58:40 +0200 Subject: [PATCH 09/10] move adjustment of .tsconfig to post-processing and remove dependency @types/qunit as it comes with OpenUI5 or SAPUI5 --- generators/dependencies.js | 1 - generators/opa5/index.js | 9 ++------- generators/qunit/index.js | 9 ++------- generators/uimodule/index.js | 5 +++++ 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/generators/dependencies.js b/generators/dependencies.js index 3f32ca9..aed7e90 100644 --- a/generators/dependencies.js +++ b/generators/dependencies.js @@ -6,7 +6,6 @@ export default { "@sap/approuter": "latest", "@ui5/linter": "latest", "cds-plugin-ui5": "latest", - "@types/qunit": "2.5.4", "mbt": "^1", "rimraf": "latest", "OpenUI5": "1.120.13", diff --git a/generators/opa5/index.js b/generators/opa5/index.js index 69f7a7c..8a9ee50 100644 --- a/generators/opa5/index.js +++ b/generators/opa5/index.js @@ -9,7 +9,6 @@ import { } from "../helpers.js" import path, { dirname } from "path" import { fileURLToPath } from "url" -import dependencies from "../dependencies.js"; const __dirname = dirname(fileURLToPath(import.meta.url)) export default class extends Generator { @@ -28,6 +27,8 @@ export default class extends Generator { // prioritize manually passed parameter over config from file, as the latter is not up to date when subgenerator is composed this.options.config.uimoduleName = this.options.uimoduleName } + // remember tests were generated for post-processing to add the respective types to .tsconfig as well + this.options.config.enableTests = true } async writing() { @@ -60,12 +61,6 @@ export default class extends Generator { const uimodulePackageJson = JSON.parse(fs.readFileSync(this.destinationPath("package.json"))) uimodulePackageJson.scripts["opa5"] = "fiori run --open test/opaTests.qunit.html" - if (this.options.config.enableTypescript) { - uimodulePackageJson["devDependencies"]["@types/qunit"] = dependencies["@types/qunit"] - const tsconfigJson = JSON.parse(fs.readFileSync(this.destinationPath("tsconfig.json"))) - tsconfigJson.compilerOptions.types.includes("qunit") || tsconfigJson.compilerOptions.types.push( "qunit" ) - fs.writeFileSync(this.destinationPath("tsconfig.json"), JSON.stringify(tsconfigJson, null, 4)) - } fs.writeFileSync(this.destinationPath("package.json"), JSON.stringify(uimodulePackageJson, null, 4)) } diff --git a/generators/qunit/index.js b/generators/qunit/index.js index a2f3b47..f3f1ee9 100644 --- a/generators/qunit/index.js +++ b/generators/qunit/index.js @@ -1,7 +1,6 @@ import chalk from "chalk" import fs from "fs" import Generator from "yeoman-generator" -import dependencies from "../dependencies.js" import prompts from "./prompts.js" import { lookForParentUI5ProjectAndPrompt, @@ -25,6 +24,8 @@ export default class extends Generator { // prioritize manually passed parameter over config from file, as the latter is not up to date when subgenerator is composed this.options.config.uimoduleName = this.options.uimoduleName } + // remember tests were generated for post-processing to add the respective types to .tsconfig as well + this.options.config.enableTests = true } async writing() { @@ -43,12 +44,6 @@ export default class extends Generator { const uimodulePackageJson = JSON.parse(fs.readFileSync(this.destinationPath("package.json"))) uimodulePackageJson.scripts["qunit"] = "fiori run --open test/unitTests.qunit.html" - if (this.options.config.enableTypescript) { - uimodulePackageJson["devDependencies"]["@types/qunit"] = dependencies["@types/qunit"] - const tsconfigJson = JSON.parse(fs.readFileSync(this.destinationPath("tsconfig.json"))) - tsconfigJson.compilerOptions.types.includes("qunit") || tsconfigJson.compilerOptions.types.push( "qunit" ) - fs.writeFileSync(this.destinationPath("tsconfig.json"), JSON.stringify(tsconfigJson, null, 4)) - } fs.writeFileSync(this.destinationPath("package.json"), JSON.stringify(uimodulePackageJson, null, 4)) } diff --git a/generators/uimodule/index.js b/generators/uimodule/index.js index 5d2bb9c..6b93f5f 100644 --- a/generators/uimodule/index.js +++ b/generators/uimodule/index.js @@ -137,6 +137,11 @@ export default class extends Generator { tsconfigJson.compilerOptions.types = [ "@sapui5/types" ] fs.writeFileSync(this.destinationPath("tsconfig.json"), JSON.stringify(tsconfigJson, null, 4)) } + if (this.options.config.enableTypescript && this.options.config.enableTests) { + const tsconfigJson = JSON.parse(fs.readFileSync(this.destinationPath("tsconfig.json"))) + tsconfigJson.compilerOptions.types.includes("qunit") || tsconfigJson.compilerOptions.types.push( "qunit" ) + fs.writeFileSync(this.destinationPath("tsconfig.json"), JSON.stringify(tsconfigJson, null, 4)) + } } end() { From 34e8bcebecb4a405453a86d89734224975c16314 Mon Sep 17 00:00:00 2001 From: D048415 Date: Fri, 18 Oct 2024 13:01:56 +0200 Subject: [PATCH 10/10] adjust tests --- test/all-projects.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/test/all-projects.js b/test/all-projects.js index ed12509..749c3bd 100644 --- a/test/all-projects.js +++ b/test/all-projects.js @@ -112,10 +112,17 @@ export const allProjects = (testCase, testDir, projectId, uimoduleName, uimodule }) it("should have qunit test", async function() { - assert.fileContent( - path.join(uimodulePath, "webapp/test/unit/FirstTest.js"), - "QUnit" - ) + if (testCase.enableTypescript) { + assert.fileContent( + path.join(uimodulePath, "webapp/test/unit/FirstTest.ts"), + "QUnit" + ) + } else { + assert.fileContent( + path.join(uimodulePath, "webapp/test/unit/FirstTest.js"), + "QUnit" + ) + } }) if (!testCase.enableFPM) { @@ -131,10 +138,17 @@ export const allProjects = (testCase, testDir, projectId, uimoduleName, uimodule }) it("should have opa5 journey and page object", async function() { - assert.fileContent( - path.join(uimodulePath, "webapp/test/integration/FirstJourney.js"), - "Opa5" - ) + if (testCase.enableTypescript) { + assert.fileContent( + path.join(uimodulePath, "webapp/test/integration/FirstJourney.ts"), + "Opa5" + ) + } else { + assert.fileContent( + path.join(uimodulePath, "webapp/test/integration/FirstJourney.js"), + "Opa5" + ) + } assert.file(path.join(uimodulePath, "webapp/test/integration/pages")) }) }