Skip to content

feat: add sample test in TypeScript for ts projects on test init command #4482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions config/test-dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,25 @@
{
"name": "karma-qunit",
"framework": "qunit"
},
{
"name": "@types/karma-chai",
"framework": "mocha",
"projectType": ".ts"
},
{
"name": "@types/mocha",
"framework": "mocha",
"projectType": ".ts"
},
{
"name": "@types/jasmine",
"framework": "jasmine",
"projectType": ".ts"
},
{
"name": "@types/qunit",
"framework": "qunit",
"projectType": ".ts"
}
]
12 changes: 8 additions & 4 deletions lib/commands/test-init.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';
import { TESTING_FRAMEWORKS } from '../constants';
import { TESTING_FRAMEWORKS, ProjectTypes } from '../constants';
import { fromWindowsRelativePathToUnix } from '../common/helpers';

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

const projectFilesExtension = this.$projectData.projectType === ProjectTypes.TsFlavorName || this.$projectData.projectType === ProjectTypes.NgFlavorName ? ".ts" : ".js";

let modulesToInstall: IDependencyInformation[] = [];
try {
modulesToInstall = this.$testInitializationService.getDependencies(frameworkToInstall);
} catch (err) {
this.$errors.failWithoutHelp(`Unable to install the unit testing dependencies. Error: '${err.message}'`);
}

modulesToInstall = modulesToInstall.filter(moduleToInstall => !moduleToInstall.projectType || moduleToInstall.projectType === projectFilesExtension);

for (const mod of modulesToInstall) {
let moduleToInstall = mod.name;
moduleToInstall += `@${mod.version}`;
Expand Down Expand Up @@ -96,16 +100,16 @@ class TestInitCommand implements ICommand {
const frameworks = [frameworkToInstall].concat(this.karmaConfigAdditionalFrameworks[frameworkToInstall] || [])
.map(fw => `'${fw}'`)
.join(', ');
const testFiles = `'${fromWindowsRelativePathToUnix(relativeTestsDir)}/**/*.js'`;
const testFiles = `'${fromWindowsRelativePathToUnix(relativeTestsDir)}/**/*${projectFilesExtension}'`;
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`);
const exampleFilePath = this.$resources.resolvePath(`test/example.${frameworkToInstall}${projectFilesExtension}`);

if (shouldCreateSampleTests && this.$fs.exists(exampleFilePath)) {
this.$fs.copyFile(exampleFilePath, path.join(testsDir, 'example.js'));
this.$fs.copyFile(exampleFilePath, path.join(testsDir, `example${projectFilesExtension}`));
this.$logger.info(`\nExample test file created in ${relativeTestsDir}`.yellow);
} else {
this.$logger.info(`\nPlace your test files under ${relativeTestsDir}`.yellow);
Expand Down
1 change: 1 addition & 0 deletions lib/common/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,7 @@ interface IProcessService {
interface IDependencyInformation {
name: string;
version?: string;
projectType?: string;
excludedPeerDependencies?: string[];
}

Expand Down
6 changes: 6 additions & 0 deletions resources/test/example.jasmine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// A sample Jasmine test
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});
9 changes: 9 additions & 0 deletions resources/test/example.mocha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// A sample Mocha test
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
assert.equal(-1, [1, 2, 3].indexOf(5));
assert.equal(-1, [1, 2, 3].indexOf(0));
});
});
});
7 changes: 7 additions & 0 deletions resources/test/example.qunit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// A sample QUnit test
QUnit.test("equal test", function (assert) {
assert.equal( 0, 0, "Zero, Zero; equal succeeds" );
assert.equal( "", 0, "Empty, Zero; equal succeeds" );
assert.equal( "", "", "Empty, Empty; equal succeeds" );
assert.equal( 0, false, "Zero, false; equal succeeds" );
});
4 changes: 1 addition & 3 deletions resources/test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ module.exports = function(config) {


// list of files / patterns to load in the browser
files: [
${ testFiles }
],
files: [ ${ testFiles } ],


// list of files to exclude
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"node_modules",
"lib/common/node_modules",
"scratch",
"coverage"
"coverage",
"resources/test"
]
}