Skip to content

Commit 673c761

Browse files
author
Fatme
authored
Merge pull request #4482 from NativeScript/fatme/ts-unit-tests
feat: add sample test in TypeScript for ts projects on test init command
2 parents 4aa3de6 + 54bf922 commit 673c761

File tree

8 files changed

+54
-8
lines changed

8 files changed

+54
-8
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

+8-4
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 projectFilesExtension = 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 === projectFilesExtension);
44+
4145
for (const mod of modulesToInstall) {
4246
let moduleToInstall = mod.name;
4347
moduleToInstall += `@${mod.version}`;
@@ -96,16 +100,16 @@ 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)}/**/*${projectFilesExtension}'`;
100104
const karmaConfTemplate = this.$resources.readText('test/karma.conf.js');
101105
const karmaConf = _.template(karmaConfTemplate)({ frameworks, testFiles });
102106

103107
this.$fs.writeFile(path.join(projectDir, 'karma.conf.js'), karmaConf);
104108

105-
const exampleFilePath = this.$resources.resolvePath(`test/example.${frameworkToInstall}.js`);
109+
const exampleFilePath = this.$resources.resolvePath(`test/example.${frameworkToInstall}${projectFilesExtension}`);
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${projectFilesExtension}`));
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
@@ -1479,6 +1479,7 @@ interface IProcessService {
14791479
interface IDependencyInformation {
14801480
name: string;
14811481
version?: string;
1482+
projectType?: string;
14821483
excludedPeerDependencies?: string[];
14831484
}
14841485

resources/test/example.jasmine.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// A sample Jasmine test
2+
describe("A suite", function() {
3+
it("contains spec with an expectation", function() {
4+
expect(true).toBe(true);
5+
});
6+
});

resources/test/example.mocha.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// A sample Mocha test
2+
describe('Array', function () {
3+
describe('#indexOf()', function () {
4+
it('should return -1 when the value is not present', function () {
5+
assert.equal(-1, [1, 2, 3].indexOf(5));
6+
assert.equal(-1, [1, 2, 3].indexOf(0));
7+
});
8+
});
9+
});

resources/test/example.qunit.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// A sample QUnit test
2+
QUnit.test("equal test", function (assert) {
3+
assert.equal( 0, 0, "Zero, Zero; equal succeeds" );
4+
assert.equal( "", 0, "Empty, Zero; equal succeeds" );
5+
assert.equal( "", "", "Empty, Empty; equal succeeds" );
6+
assert.equal( 0, false, "Zero, false; equal succeeds" );
7+
});

resources/test/karma.conf.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ module.exports = function(config) {
1111

1212

1313
// list of files / patterns to load in the browser
14-
files: [
15-
${ testFiles }
16-
],
14+
files: [ ${ testFiles } ],
1715

1816

1917
// list of files to exclude

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"node_modules",
1515
"lib/common/node_modules",
1616
"scratch",
17-
"coverage"
17+
"coverage",
18+
"resources/test"
1819
]
1920
}

0 commit comments

Comments
 (0)