From 3c21bd1997fea0cd4d93da9f5ce4d0b172596fa8 Mon Sep 17 00:00:00 2001 From: rosen-vladimirov Date: Thu, 21 Jun 2018 18:31:17 +0300 Subject: [PATCH] fix: Trying to run unit test fails with latest karma When tests are initialized, the `tns test ` command fails in case karma 2.0.3 is used. In order to fix it, until karma resolves the issue on their side, hardcode the version of karma that is installed by CLI on `tns test init` command. Also install mocha when it is selected for testing framework. In case we do not install it, the test command fails again. --- lib/commands/test-init.ts | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/commands/test-init.ts b/lib/commands/test-init.ts index f80b2b0b06..e1e0adc9d2 100644 --- a/lib/commands/test-init.ts +++ b/lib/commands/test-init.ts @@ -5,7 +5,11 @@ class TestInitCommand implements ICommand { public allowedParameters: ICommandParameter[] = []; private frameworkDependencies: IDictionary = { - mocha: ['chai'], + mocha: ['karma-chai', 'mocha'], + }; + + private karmaConfigAdditionalFrameworks: IDictionary = { + mocha: ['chai'] }; constructor(private $npm: INodePackageManager, @@ -30,11 +34,30 @@ class TestInitCommand implements ICommand { } const dependencies = this.frameworkDependencies[frameworkToInstall] || []; - const modulesToInstall = ['karma', 'karma-' + frameworkToInstall, 'karma-nativescript-launcher'].concat(dependencies.map(f => 'karma-' + f)); + const modulesToInstall: IDependencyInformation[] = [ + { + name: 'karma', + // Hardcode the version unitl https://github.com/karma-runner/karma/issues/3052 is fixed + version: "2.0.2" + }, + { + name: `karma-${frameworkToInstall}` + }, + { + name: 'karma-nativescript-launcher' + } + ]; + + modulesToInstall.push(...dependencies.map(f => ({ name: f }))); for (const mod of modulesToInstall) { - await this.$npm.install(mod, projectDir, { + let moduleToInstall = mod.name; + if (mod.version) { + moduleToInstall += `@${mod.version}`; + } + await this.$npm.install(moduleToInstall, projectDir, { 'save-dev': true, + 'save-exact': true, optional: false, disableNpmInstall: this.$options.disableNpmInstall, frameworkPath: this.$options.frameworkPath, @@ -42,7 +65,7 @@ class TestInitCommand implements ICommand { path: this.$options.path }); - const modulePath = path.join(projectDir, "node_modules", mod); + const modulePath = path.join(projectDir, "node_modules", mod.name); const modulePackageJsonPath = path.join(modulePath, "package.json"); const modulePackageJsonContent = this.$fs.readJson(modulePackageJsonPath); const modulePeerDependencies = modulePackageJsonContent.peerDependencies || {}; @@ -55,6 +78,7 @@ class TestInitCommand implements ICommand { try { await this.$npm.install(`${peerDependency}@${dependencyVersion}`, projectDir, { 'save-dev': true, + 'save-exact': true, disableNpmInstall: false, frameworkPath: this.$options.frameworkPath, ignoreScripts: this.$options.ignoreScripts, @@ -79,7 +103,7 @@ class TestInitCommand implements ICommand { const karmaConfTemplate = this.$resources.readText('test/karma.conf.js'); const karmaConf = _.template(karmaConfTemplate)({ - frameworks: [frameworkToInstall].concat(dependencies) + frameworks: [frameworkToInstall].concat(this.karmaConfigAdditionalFrameworks[frameworkToInstall]) .map(fw => `'${fw}'`) .join(', ') });