-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathtest-init.ts
118 lines (98 loc) · 4.52 KB
/
test-init.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import * as path from 'path';
import { TESTING_FRAMEWORKS } from '../constants';
import { fromWindowsRelativePathToUnix } from '../common/helpers';
class TestInitCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];
private karmaConfigAdditionalFrameworks: IDictionary<string[]> = {
mocha: ['chai']
};
constructor(private $packageManager: INodePackageManager,
private $projectData: IProjectData,
private $errors: IErrors,
private $options: IOptions,
private $prompter: IPrompter,
private $fs: IFileSystem,
private $resources: IResourceLoader,
private $pluginsService: IPluginsService,
private $logger: ILogger,
private $testInitializationService: ITestInitializationService) {
this.$projectData.initializeProjectData();
}
public async execute(args: string[]): Promise<void> {
const projectDir = this.$projectData.projectDir;
const frameworkToInstall = this.$options.framework ||
await this.$prompter.promptForChoice('Select testing framework:', TESTING_FRAMEWORKS);
if (TESTING_FRAMEWORKS.indexOf(frameworkToInstall) === -1) {
this.$errors.fail(`Unknown or unsupported unit testing framework: ${frameworkToInstall}`);
}
let modulesToInstall: IDependencyInformation[] = [];
try {
modulesToInstall = this.$testInitializationService.getDependencies(frameworkToInstall);
} catch (err) {
this.$errors.failWithoutHelp(`Unable to install the unit testing dependencies. Error: '${err.message}'`);
}
for (const mod of modulesToInstall) {
let moduleToInstall = mod.name;
moduleToInstall += `@${mod.version}`;
await this.$packageManager.install(moduleToInstall, projectDir, {
'save-dev': true,
'save-exact': true,
optional: false,
disableNpmInstall: this.$options.disableNpmInstall,
frameworkPath: this.$options.frameworkPath,
ignoreScripts: this.$options.ignoreScripts,
path: this.$options.path
});
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 || {};
for (const peerDependency in modulePeerDependencies) {
const isPeerDependencyExcluded = _.includes(mod.excludedPeerDependencies, peerDependency);
if (isPeerDependencyExcluded) {
continue;
}
const dependencyVersion = modulePeerDependencies[peerDependency] || "*";
// catch errors when a peerDependency is already installed
// e.g karma is installed; karma-jasmine depends on karma and will try to install it again
try {
await this.$packageManager.install(`${peerDependency}@${dependencyVersion}`, projectDir, {
'save-dev': true,
'save-exact': true,
disableNpmInstall: false,
frameworkPath: this.$options.frameworkPath,
ignoreScripts: this.$options.ignoreScripts,
path: this.$options.path
});
} catch (e) {
this.$logger.error(e.message);
}
}
}
await this.$pluginsService.add('nativescript-unit-test-runner', this.$projectData);
const testsDir = path.join(this.$projectData.appDirectoryPath, 'tests');
const relativeTestsDir = path.relative(this.$projectData.projectDir, testsDir);
let shouldCreateSampleTests = true;
if (this.$fs.exists(testsDir)) {
this.$logger.info(`${relativeTestsDir} directory already exists, will not create an example test project.`);
shouldCreateSampleTests = false;
}
this.$fs.ensureDirectoryExists(testsDir);
const frameworks = [frameworkToInstall].concat(this.karmaConfigAdditionalFrameworks[frameworkToInstall] || [])
.map(fw => `'${fw}'`)
.join(', ');
const testFiles = `'${fromWindowsRelativePathToUnix(relativeTestsDir)}/**/*.js'`;
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`);
if (shouldCreateSampleTests && this.$fs.exists(exampleFilePath)) {
this.$fs.copyFile(exampleFilePath, path.join(testsDir, 'example.js'));
this.$logger.info(`\nExample test file created in ${relativeTestsDir}`.yellow);
} else {
this.$logger.info(`\nPlace your test files under ${relativeTestsDir}`.yellow);
}
this.$logger.info('Run your tests using the "$ tns test <platform>" command.'.yellow);
}
}
$injector.registerCommand("test|init", TestInitCommand);