Skip to content

Commit 3c21bd1

Browse files
fix: Trying to run unit test fails with latest karma
When tests are initialized, the `tns test <platform>` 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.
1 parent 421867f commit 3c21bd1

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

lib/commands/test-init.ts

+29-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ class TestInitCommand implements ICommand {
55
public allowedParameters: ICommandParameter[] = [];
66

77
private frameworkDependencies: IDictionary<string[]> = {
8-
mocha: ['chai'],
8+
mocha: ['karma-chai', 'mocha'],
9+
};
10+
11+
private karmaConfigAdditionalFrameworks: IDictionary<string[]> = {
12+
mocha: ['chai']
913
};
1014

1115
constructor(private $npm: INodePackageManager,
@@ -30,19 +34,38 @@ class TestInitCommand implements ICommand {
3034
}
3135

3236
const dependencies = this.frameworkDependencies[frameworkToInstall] || [];
33-
const modulesToInstall = ['karma', 'karma-' + frameworkToInstall, 'karma-nativescript-launcher'].concat(dependencies.map(f => 'karma-' + f));
37+
const modulesToInstall: IDependencyInformation[] = [
38+
{
39+
name: 'karma',
40+
// Hardcode the version unitl https://github.com/karma-runner/karma/issues/3052 is fixed
41+
version: "2.0.2"
42+
},
43+
{
44+
name: `karma-${frameworkToInstall}`
45+
},
46+
{
47+
name: 'karma-nativescript-launcher'
48+
}
49+
];
50+
51+
modulesToInstall.push(...dependencies.map(f => ({ name: f })));
3452

3553
for (const mod of modulesToInstall) {
36-
await this.$npm.install(mod, projectDir, {
54+
let moduleToInstall = mod.name;
55+
if (mod.version) {
56+
moduleToInstall += `@${mod.version}`;
57+
}
58+
await this.$npm.install(moduleToInstall, projectDir, {
3759
'save-dev': true,
60+
'save-exact': true,
3861
optional: false,
3962
disableNpmInstall: this.$options.disableNpmInstall,
4063
frameworkPath: this.$options.frameworkPath,
4164
ignoreScripts: this.$options.ignoreScripts,
4265
path: this.$options.path
4366
});
4467

45-
const modulePath = path.join(projectDir, "node_modules", mod);
68+
const modulePath = path.join(projectDir, "node_modules", mod.name);
4669
const modulePackageJsonPath = path.join(modulePath, "package.json");
4770
const modulePackageJsonContent = this.$fs.readJson(modulePackageJsonPath);
4871
const modulePeerDependencies = modulePackageJsonContent.peerDependencies || {};
@@ -55,6 +78,7 @@ class TestInitCommand implements ICommand {
5578
try {
5679
await this.$npm.install(`${peerDependency}@${dependencyVersion}`, projectDir, {
5780
'save-dev': true,
81+
'save-exact': true,
5882
disableNpmInstall: false,
5983
frameworkPath: this.$options.frameworkPath,
6084
ignoreScripts: this.$options.ignoreScripts,
@@ -79,7 +103,7 @@ class TestInitCommand implements ICommand {
79103

80104
const karmaConfTemplate = this.$resources.readText('test/karma.conf.js');
81105
const karmaConf = _.template(karmaConfTemplate)({
82-
frameworks: [frameworkToInstall].concat(dependencies)
106+
frameworks: [frameworkToInstall].concat(this.karmaConfigAdditionalFrameworks[frameworkToInstall])
83107
.map(fw => `'${fw}'`)
84108
.join(', ')
85109
});

0 commit comments

Comments
 (0)