-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathtest-init.ts
94 lines (75 loc) · 3.36 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
import * as path from 'path';
import { TESTING_FRAMEWORKS } from '../constants';
class TestInitCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];
private frameworkDependencies: IDictionary<string[]> = {
mocha: ['chai'],
};
constructor(private $npm: 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) {
this.$projectData.initializeProjectData();
}
public async execute(args: string[]): Promise<void> {
let projectDir = this.$projectData.projectDir;
let 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 dependencies = this.frameworkDependencies[frameworkToInstall] || [];
let modulesToInstall = ['karma', 'karma-' + frameworkToInstall, 'karma-nativescript-launcher'].concat(dependencies.map(f => 'karma-' + f));
for (let mod of modulesToInstall) {
await this.$npm.install(mod, projectDir, {
'save-dev': true,
optional: false,
});
const modulePath = path.join(projectDir, "node_modules", mod);
const modulePackageJsonPath = path.join(modulePath, "package.json");
const modulePackageJsonContent = this.$fs.readJson(modulePackageJsonPath);
const modulePeerDependencies = modulePackageJsonContent.peerDependencies || {};
for (let peerDependency in modulePeerDependencies) {
let 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.$npm.install(`${peerDependency}@${dependencyVersion}`, projectDir, {
'save-dev': true
});
} catch (e) {
this.$logger.error(e.message);
}
}
}
await this.$pluginsService.add('nativescript-unit-test-runner', this.$projectData);
let testsDir = path.join(projectDir, 'app/tests');
let shouldCreateSampleTests = true;
if (this.$fs.exists(testsDir)) {
this.$logger.info('app/tests/ directory already exists, will not create an example test project.');
shouldCreateSampleTests = false;
}
this.$fs.ensureDirectoryExists(testsDir);
let karmaConfTemplate = this.$resources.readText('test/karma.conf.js');
let karmaConf = _.template(karmaConfTemplate)({
frameworks: [frameworkToInstall].concat(dependencies)
.map(fw => `'${fw}'`)
.join(', ')
});
this.$fs.writeFile(path.join(projectDir, 'karma.conf.js'), karmaConf);
let 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 app/tests/'.yellow);
} else {
this.$logger.info('\nPlace your test files under app/tests/'.yellow);
}
this.$logger.info('Run your tests using the "$ tns test <platform>" command.'.yellow);
}
}
$injector.registerCommand("test|init", TestInitCommand);