Skip to content

Commit ce5a81f

Browse files
committed
feat: add sample test in TypeScript for ts projects on test init command
#1798
1 parent 4971447 commit ce5a81f

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

config/test-dependencies.json

+20
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,25 @@
2828
{
2929
"name": "karma-qunit",
3030
"framework": "qunit"
31+
},
32+
{
33+
"name": "@types/karma-chai",
34+
"framework": "mocha",
35+
"projectType": ".ts"
36+
},
37+
{
38+
"name": "@types/mocha",
39+
"framework": "mocha",
40+
"projectType": ".ts"
41+
},
42+
{
43+
"name": "@types/jasmine",
44+
"framework": "jasmine",
45+
"projectType": ".ts"
46+
},
47+
{
48+
"name": "@types/qunit",
49+
"framework": "qunit",
50+
"projectType": ".ts"
3151
}
3252
]

lib/commands/test-init.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from 'path';
2-
import { TESTING_FRAMEWORKS } from '../constants';
2+
import { TESTING_FRAMEWORKS, ProjectTypes } from '../constants';
33
import { fromWindowsRelativePathToUnix } from '../common/helpers';
44

55
class TestInitCommand implements ICommand {
@@ -31,13 +31,17 @@ class TestInitCommand implements ICommand {
3131
this.$errors.fail(`Unknown or unsupported unit testing framework: ${frameworkToInstall}`);
3232
}
3333

34+
const projectType = this.$projectData.projectType === ProjectTypes.TsFlavorName || this.$projectData.projectType === ProjectTypes.NgFlavorName ? ".ts" : ".js";
35+
3436
let modulesToInstall: IDependencyInformation[] = [];
3537
try {
3638
modulesToInstall = this.$testInitializationService.getDependencies(frameworkToInstall);
3739
} catch (err) {
3840
this.$errors.failWithoutHelp(`Unable to install the unit testing dependencies. Error: '${err.message}'`);
3941
}
4042

43+
modulesToInstall = modulesToInstall.filter(moduleToInstall => !moduleToInstall.projectType || moduleToInstall.projectType === projectType);
44+
4145
for (const mod of modulesToInstall) {
4246
let moduleToInstall = mod.name;
4347
moduleToInstall += `@${mod.version}`;
@@ -96,7 +100,7 @@ class TestInitCommand implements ICommand {
96100
const frameworks = [frameworkToInstall].concat(this.karmaConfigAdditionalFrameworks[frameworkToInstall] || [])
97101
.map(fw => `'${fw}'`)
98102
.join(', ');
99-
const testFiles = `'${fromWindowsRelativePathToUnix(relativeTestsDir)}/**/*.js'`;
103+
const testFiles = `'${fromWindowsRelativePathToUnix(relativeTestsDir)}/**/*${projectType}'`;
100104
const karmaConfTemplate = this.$resources.readText('test/karma.conf.js');
101105
const karmaConf = _.template(karmaConfTemplate)({ frameworks, testFiles });
102106

@@ -105,7 +109,7 @@ class TestInitCommand implements ICommand {
105109
const exampleFilePath = this.$resources.resolvePath(`test/example.${frameworkToInstall}.js`);
106110

107111
if (shouldCreateSampleTests && this.$fs.exists(exampleFilePath)) {
108-
this.$fs.copyFile(exampleFilePath, path.join(testsDir, 'example.js'));
112+
this.$fs.copyFile(exampleFilePath, path.join(testsDir, `example${projectType}`));
109113
this.$logger.info(`\nExample test file created in ${relativeTestsDir}`.yellow);
110114
} else {
111115
this.$logger.info(`\nPlace your test files under ${relativeTestsDir}`.yellow);

lib/common/declarations.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,7 @@ interface IProcessService {
14671467
interface IDependencyInformation {
14681468
name: string;
14691469
version?: string;
1470+
projectType?: string;
14701471
excludedPeerDependencies?: string[];
14711472
}
14721473

0 commit comments

Comments
 (0)