From 365b484e80ce7eae2f0f0b0f9503f2139a144b55 Mon Sep 17 00:00:00 2001 From: fatme Date: Thu, 28 Mar 2019 16:44:35 +0200 Subject: [PATCH 1/3] feat: add sample test in TypeScript for ts projects on test init command https://github.com/NativeScript/nativescript-cli/issues/1798 --- config/test-dependencies.json | 20 ++++++++++++++++++++ lib/commands/test-init.ts | 12 ++++++++---- lib/common/declarations.d.ts | 1 + resources/test/example.jasmine.ts | 6 ++++++ resources/test/example.mocha.ts | 9 +++++++++ resources/test/example.qunit.ts | 7 +++++++ 6 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 resources/test/example.jasmine.ts create mode 100644 resources/test/example.mocha.ts create mode 100644 resources/test/example.qunit.ts diff --git a/config/test-dependencies.json b/config/test-dependencies.json index 7a4868e2fb..879c04a950 100644 --- a/config/test-dependencies.json +++ b/config/test-dependencies.json @@ -28,5 +28,25 @@ { "name": "karma-qunit", "framework": "qunit" + }, + { + "name": "@types/karma-chai", + "framework": "mocha", + "projectType": ".ts" + }, + { + "name": "@types/mocha", + "framework": "mocha", + "projectType": ".ts" + }, + { + "name": "@types/jasmine", + "framework": "jasmine", + "projectType": ".ts" + }, + { + "name": "@types/qunit", + "framework": "qunit", + "projectType": ".ts" } ] \ No newline at end of file diff --git a/lib/commands/test-init.ts b/lib/commands/test-init.ts index 5a17fb42f8..fb0985fa65 100644 --- a/lib/commands/test-init.ts +++ b/lib/commands/test-init.ts @@ -1,5 +1,5 @@ import * as path from 'path'; -import { TESTING_FRAMEWORKS } from '../constants'; +import { TESTING_FRAMEWORKS, ProjectTypes } from '../constants'; import { fromWindowsRelativePathToUnix } from '../common/helpers'; class TestInitCommand implements ICommand { @@ -31,6 +31,8 @@ class TestInitCommand implements ICommand { this.$errors.fail(`Unknown or unsupported unit testing framework: ${frameworkToInstall}`); } + const projectFilesExtension = this.$projectData.projectType === ProjectTypes.TsFlavorName || this.$projectData.projectType === ProjectTypes.NgFlavorName ? ".ts" : ".js"; + let modulesToInstall: IDependencyInformation[] = []; try { modulesToInstall = this.$testInitializationService.getDependencies(frameworkToInstall); @@ -38,6 +40,8 @@ class TestInitCommand implements ICommand { this.$errors.failWithoutHelp(`Unable to install the unit testing dependencies. Error: '${err.message}'`); } + modulesToInstall = modulesToInstall.filter(moduleToInstall => !moduleToInstall.projectType || moduleToInstall.projectType === projectFilesExtension); + for (const mod of modulesToInstall) { let moduleToInstall = mod.name; moduleToInstall += `@${mod.version}`; @@ -96,16 +100,16 @@ class TestInitCommand implements ICommand { const frameworks = [frameworkToInstall].concat(this.karmaConfigAdditionalFrameworks[frameworkToInstall] || []) .map(fw => `'${fw}'`) .join(', '); - const testFiles = `'${fromWindowsRelativePathToUnix(relativeTestsDir)}/**/*.js'`; + const testFiles = `'${fromWindowsRelativePathToUnix(relativeTestsDir)}/**/*${projectFilesExtension}'`; const karmaConfTemplate = this.$resources.readText('test/karma.conf.js'); const karmaConf = _.template(karmaConfTemplate)({ frameworks, testFiles }); this.$fs.writeFile(path.join(projectDir, 'karma.conf.js'), karmaConf); - const exampleFilePath = this.$resources.resolvePath(`test/example.${frameworkToInstall}.js`); + const exampleFilePath = this.$resources.resolvePath(`test/example.${frameworkToInstall}${projectFilesExtension}`); if (shouldCreateSampleTests && this.$fs.exists(exampleFilePath)) { - this.$fs.copyFile(exampleFilePath, path.join(testsDir, 'example.js')); + this.$fs.copyFile(exampleFilePath, path.join(testsDir, `example${projectFilesExtension}`)); this.$logger.info(`\nExample test file created in ${relativeTestsDir}`.yellow); } else { this.$logger.info(`\nPlace your test files under ${relativeTestsDir}`.yellow); diff --git a/lib/common/declarations.d.ts b/lib/common/declarations.d.ts index 5e096b0368..d07d3efd7e 100644 --- a/lib/common/declarations.d.ts +++ b/lib/common/declarations.d.ts @@ -1467,6 +1467,7 @@ interface IProcessService { interface IDependencyInformation { name: string; version?: string; + projectType?: string; excludedPeerDependencies?: string[]; } diff --git a/resources/test/example.jasmine.ts b/resources/test/example.jasmine.ts new file mode 100644 index 0000000000..c31134716f --- /dev/null +++ b/resources/test/example.jasmine.ts @@ -0,0 +1,6 @@ +// A sample Jasmine test +describe("A suite", function() { + it("contains spec with an expectation", function() { + expect(true).toBe(true); + }); +}); diff --git a/resources/test/example.mocha.ts b/resources/test/example.mocha.ts new file mode 100644 index 0000000000..a287a2eba2 --- /dev/null +++ b/resources/test/example.mocha.ts @@ -0,0 +1,9 @@ +// A sample Mocha test +describe('Array', function () { + describe('#indexOf()', function () { + it('should return -1 when the value is not present', function () { + assert.equal(-1, [1, 2, 3].indexOf(5)); + assert.equal(-1, [1, 2, 3].indexOf(0)); + }); + }); +}); diff --git a/resources/test/example.qunit.ts b/resources/test/example.qunit.ts new file mode 100644 index 0000000000..6933144e37 --- /dev/null +++ b/resources/test/example.qunit.ts @@ -0,0 +1,7 @@ +// A sample QUnit test +QUnit.test("equal test", function (assert) { + assert.equal( 0, 0, "Zero, Zero; equal succeeds" ); + assert.equal( "", 0, "Empty, Zero; equal succeeds" ); + assert.equal( "", "", "Empty, Empty; equal succeeds" ); + assert.equal( 0, false, "Zero, false; equal succeeds" ); +}); From 51cb7caf3dd2b0e1069a4ef037ba49bdb67c2795 Mon Sep 17 00:00:00 2001 From: fatme Date: Fri, 29 Mar 2019 11:18:16 +0200 Subject: [PATCH 2/3] chore: exclude example test ts files from compilation --- tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index d3eeb57049..3253e8f14e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,6 +14,7 @@ "node_modules", "lib/common/node_modules", "scratch", - "coverage" + "coverage", + "resources/test" ] } From 54bf922c86ac2c701e6dda75500d3b7dd5ec9761 Mon Sep 17 00:00:00 2001 From: fatme Date: Mon, 1 Apr 2019 11:08:47 +0300 Subject: [PATCH 3/3] chore: add separate test files for `--bundle` and no-bundle case --- resources/test/karma.conf.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/resources/test/karma.conf.js b/resources/test/karma.conf.js index 1cc877d6e4..918a6cee93 100644 --- a/resources/test/karma.conf.js +++ b/resources/test/karma.conf.js @@ -11,9 +11,7 @@ module.exports = function(config) { // list of files / patterns to load in the browser - files: [ - ${ testFiles } - ], + files: [ ${ testFiles } ], // list of files to exclude