Skip to content

Commit 80056b4

Browse files
committed
Feature: Add ava testing framework and prettier
1 parent 61e298e commit 80056b4

14 files changed

+215
-95
lines changed

README.md

+22-15
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
> For my 1.x release documentation for TypeScript 1.8, refer to [readme file for 1.x release](./README-1x.md).
66
7-
I'm a minimal [Yeoman](http://yeoman.io) generator for creating NodeJS modules using TypeScript. I let you quickly setup a project with latest available tools and best practices.
7+
I'm a minimal [Yeoman](http://yeoman.io) generator for creating NodeJS packages using TypeScript. I let you quickly setup a project with latest available tools and best practices.
88

99
I use:
1010

1111
- _npm_ - as task runner. You can choose to use _gulp_ instead.
1212
- _tslint_ - as linter for TypeScript source files.
13-
- _mocha_ - as testing framework to write specs in **TypeScript** itself.
14-
- _istanbul_ - a JavaScript code coverage tool working on TypeScript files.
13+
- _ava_ - as [testing framework](https://github.com/avajs/ava) to write specs in **TypeScript** itself. You can choose to use _mocha_ instead.
14+
- _nyc_ - a JavaScript code coverage tool working on TypeScript files.
1515

16-
You want to know if you can change any of these? Of course, why not? It is your module after all. I simply get down to business of generating, no questions asked. Once done, I get out of the way and you can do as you please!
16+
You want to know if you can change any of these? Of course, why not? It is your package after all. I simply get down to business of generating, no questions asked. Once done, I get out of the way and you can do as you please!
1717

1818
## Usage
1919

@@ -38,25 +38,31 @@ $yo node-typescript
3838

3939
You can choose to use _gulp_ as your build system using command - `$yo node-typescript --gulp`
4040

41+
You can choose to use _mocha_ as your test framework using command - `$yo node-typescript --mocha`
42+
4143
Run `npm run` for information on available tasks.
4244

4345
```sh
4446
$npm run
45-
Lifecycle scripts included in node-ts:
47+
Lifecycle scripts included in node-typescript-demo:
48+
prepublish
49+
npm run build
4650
test
47-
npm run build && mocha --compilers ts:ts-node/register --recursive test/**/*-spec.ts
48-
coverage
49-
nyc --reporter=text --reporter=html mocha --compilers ts:ts-node/register
51+
npm run clean && tsc -p tsconfig.test.json --pretty && nyc --exclude "**/*-spec.js" ava "**/*-spec.js" --verbose
5052

5153
available via `npm run-script`:
5254
clean
53-
rimraf lib
55+
rimraf lib && rimraf coverage && rimraf .nyc_output && rimraf lib_test
56+
prettier
57+
prettier --write "{src,test}/**/*.ts"
5458
lint
55-
tslint --format verbose 'src/**/*.ts'
59+
tslint --force --format verbose "src/**/*.ts"
60+
prebuild
61+
npm run prettify && npm run clean && npm run lint && echo Using TypeScript && tsc --version
5662
build
57-
npm run clean && npm run lint && echo Using TypeScript && tsc --version && tsc --pretty
63+
tsc --pretty
5864
coverage
59-
nyc --reporter=text --reporter=html mocha --compilers ts:ts-node/register
65+
nyc report --reporter=lcov --reporter=text --reporter=html
6066
watch
6167
npm run build -- --watch
6268
watch:test
@@ -84,10 +90,11 @@ Available tasks
8490

8591
- I use latest version of **TypeScript**.
8692
- I use _npm_ to fetch type definitions making life so much easier. You can find more information on [https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/](https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/).
87-
- I use _mocha_ as testing framework as it allows easier test runs from command line. Also, one of the most important things regarding testing is **now you can write tests in TypeScript itself**. The out-of-box configuration includes use of [ts-node](https://github.com/TypeStrong/ts-node) as mocha compiler allowing executing specs written in TypeScript without compiling them first.
93+
- I use _ava_ which is a [Futuristic JavaScript test runner](https://github.com/avajs/ava) as testing framework (optionally _mocha_). Also, one of the most important things regarding testing is **you can write tests in TypeScript itself**.
94+
- I use _prettier_ integrated with _tslint_ to provide no-fuss code formatting and linting.
8895
- I need **no global dependencies**. Every dependency such as _TypeScript_ and _tslint_ is installed as local dev dependency allowing you to freely use different versions of these for different modules.
89-
- I provide test coverage support using _istanbul_.
90-
- I provide nice integration with [VS Code editor](https://code.visualstudio.com/). I configure `build`, `clean`, `lint`, `coverage` and `test` tasks that you can run using `Run Task` option.
96+
- I provide test coverage support using _nyc_.
97+
- I provide nice integration with [VS Code editor](https://code.visualstudio.com/). I configure `build`, `clean`, `lint`, `coverage`, 'prettier' and `test` tasks that you can run using `Run Task` option.
9198

9299
## License
93100

generators/app/index.js

+51-24
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,29 @@ module.exports = yeoman.generators.Base.extend({
2929
dir: function () {
3030
this.directory('src', 'src');
3131

32-
// 2.0.0-beta: copying the spec file needs templating due to the ts-node problem on windows
33-
// this.directory('test', 'test');
34-
this.fs.copyTpl(
35-
this.templatePath('test/greeter-spec.ts'),
36-
this.destinationPath('test/greeter-spec.ts'),
37-
{ isWindows: process.platform === 'win32' }
38-
);
39-
this.fs.copyTpl(
40-
this.templatePath('test/index-spec.ts'),
41-
this.destinationPath('test/index-spec.ts'),
42-
{ isWindows: process.platform === 'win32' }
43-
);
32+
if (this.options.mocha) {
33+
// 2.0.0-beta: copying the spec file needs templating due to the ts-node problem on windows
34+
// this.directory('test', 'test');
35+
this.fs.copyTpl(
36+
this.templatePath('test/greeter-spec_mocha.ts'),
37+
this.destinationPath('test/greeter-spec.ts'),
38+
{ isWindows: process.platform === 'win32' }
39+
);
40+
this.fs.copyTpl(
41+
this.templatePath('test/index-spec_mocha.ts'),
42+
this.destinationPath('test/index-spec.ts'),
43+
{ isWindows: process.platform === 'win32' }
44+
);
45+
} else {
46+
this.fs.copyTpl(
47+
this.templatePath('test/greeter-spec.ts'),
48+
this.destinationPath('test/greeter-spec.ts')
49+
);
50+
this.fs.copyTpl(
51+
this.templatePath('test/index-spec.ts'),
52+
this.destinationPath('test/index-spec.ts')
53+
);
54+
}
4455
},
4556

4657
projectfiles: function () {
@@ -74,16 +85,34 @@ module.exports = yeoman.generators.Base.extend({
7485
this.destinationPath('.vscode/tasks.json')
7586
);
7687

77-
this.fs.copyTpl(
78-
this.templatePath('_package.json'),
79-
this.destinationPath('package.json'),
80-
{ appname: _.kebabCase(path.basename(process.cwd())) }
81-
);
82-
83-
this.fs.copy(
84-
this.templatePath('travis.yml'),
85-
this.destinationPath('.travis.yml')
86-
);
88+
if (this.options.mocha) {
89+
this.fs.copyTpl(
90+
this.templatePath('_package_mocha.json'),
91+
this.destinationPath('package.json'),
92+
{ appname: _.kebabCase(path.basename(process.cwd())) }
93+
);
94+
95+
this.fs.copy(
96+
this.templatePath('travis_mocha.yml'),
97+
this.destinationPath('.travis.yml')
98+
);
99+
} else {
100+
this.fs.copyTpl(
101+
this.templatePath('_package.json'),
102+
this.destinationPath('package.json'),
103+
{ appname: _.kebabCase(path.basename(process.cwd())) }
104+
);
105+
106+
this.fs.copy(
107+
this.templatePath('travis.yml'),
108+
this.destinationPath('.travis.yml')
109+
);
110+
111+
this.fs.copy(
112+
this.templatePath('_tsconfig.test.json'),
113+
this.destinationPath('tsconfig.test.json')
114+
);
115+
}
87116

88117
this.fs.copy(
89118
this.templatePath('README.md'),
@@ -123,8 +152,6 @@ module.exports = yeoman.generators.Base.extend({
123152
npmInstall: function () {
124153
var generator = this;
125154
generator.npmInstall(null, { skipInstall: this.options['skip-install'] }, function () {
126-
//generator.spawnCommandSync('typings', ['init']); //typings init
127-
//generator.spawnCommandSync('typings', ['install', 'dt~node', '--save', '--global']) //typings install --save dt~node --global
128155
});
129156
}
130157
}

generators/app/templates/_package.json

+13-27
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,36 @@
1414
"main": "lib/index.js",
1515
"typings": "lib/index.d.ts",
1616
"scripts": {
17-
"clean": "rimraf lib",
18-
"prettify": "prettier --write \"{src,test}/**/*.ts\"",
17+
"clean": "rimraf lib && rimraf coverage && rimraf .nyc_output && rimraf lib_test",
18+
"prettier": "prettier --write \"{src,test}/**/*.ts\"",
1919
"lint": "tslint --force --format verbose \"src/**/*.ts\"",
20-
"prepublish": "npm run build",
21-
"build": "npm run prettify && npm run clean && npm run lint && echo Using TypeScript && tsc --version && tsc --pretty",
22-
"test": "npm run build && mocha --compilers ts:ts-node/register --recursive \"test/**/*-spec.ts\"",
23-
"coverage": "nyc --include=\"src/**/*.ts\" --reporter=text --reporter=html --reporter=lcov mocha --compilers ts:ts-node/register --recursive \"test/**/*-spec.ts\"",
20+
"prepublish": "npm run build",
21+
"prebuild": "npm run clean && npm run prettier && npm run lint && echo Using TypeScript && tsc --version",
22+
"build": "tsc --pretty",
23+
"test": "npm run clean && tsc -p tsconfig.test.json --pretty && nyc --exclude \"**/*-spec.js\" ava \"**/*-spec.js\" --verbose",
24+
"coverage": "nyc report --reporter=lcov --reporter=text --reporter=html",
2425
"watch": "npm run build -- --watch",
2526
"watch:test": "npm run test -- --watch"
2627
},
2728
"dependencies": {
2829
},
2930
"devDependencies": {
30-
"@types/chai": "^3.0.0",
31-
"@types/mocha": "^2.0.0",
3231
"@types/node": "^7.0.0",
33-
"chai": "^3.0.0",
34-
"mocha": "^3.0.0",
32+
"ava": "^0.20.0",
33+
"coveralls": "^2.0.0",
3534
"nyc": "^10.0.0",
3635
"prettier": "^1.5.2",
3736
"rimraf": "^2.0.0",
38-
"ts-node": "^3.0.0",
3937
"tslint": "^5.0.0",
4038
"tslint-config-prettier": "^1.1.0",
41-
"typescript": "^2.0.0",
42-
"coveralls": "^2.0.0"
39+
"typescript": "^2.0.0"
4340
},
4441
"engines": {
4542
"node": ">=6.0.0"
4643
},
47-
"nyc": {
48-
"include": [
49-
"src/**/*.ts"
50-
],
51-
"exclude": [
52-
"lib"
53-
],
54-
"extension": [
55-
".ts"
56-
],
44+
"ava": {
5745
"require": [
58-
"ts-node/register"
59-
],
60-
"reporter": [],
61-
"all": true
46+
"babel-register"
47+
]
6248
}
6349
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "<%= appname %>",
3+
"version": "0.0.0",
4+
"description": "<%= appname %>",
5+
"license": "MIT",
6+
"repository": "",
7+
"author": "",
8+
"keywords": [
9+
""
10+
],
11+
"files": [
12+
"lib"
13+
],
14+
"main": "lib/index.js",
15+
"typings": "lib/index.d.ts",
16+
"scripts": {
17+
"clean": "rimraf lib",
18+
"prettify": "prettier --write \"{src,test}/**/*.ts\"",
19+
"lint": "tslint --force --format verbose \"src/**/*.ts\"",
20+
"prepublish": "npm run build",
21+
"build": "npm run prettify && npm run clean && npm run lint && echo Using TypeScript && tsc --version && tsc --pretty",
22+
"test": "npm run build && mocha --compilers ts:ts-node/register --recursive \"test/**/*-spec.ts\"",
23+
"coverage": "nyc --include=\"src/**/*.ts\" --reporter=text --reporter=html --reporter=lcov mocha --compilers ts:ts-node/register --recursive \"test/**/*-spec.ts\"",
24+
"watch": "npm run build -- --watch",
25+
"watch:test": "npm run test -- --watch"
26+
},
27+
"dependencies": {
28+
},
29+
"devDependencies": {
30+
"@types/chai": "^3.0.0",
31+
"@types/mocha": "^2.0.0",
32+
"@types/node": "^7.0.0",
33+
"chai": "^3.0.0",
34+
"mocha": "^3.0.0",
35+
"nyc": "^10.0.0",
36+
"prettier": "^1.5.2",
37+
"rimraf": "^2.0.0",
38+
"ts-node": "^3.0.0",
39+
"tslint": "^5.0.0",
40+
"tslint-config-prettier": "^1.1.0",
41+
"typescript": "^2.0.0",
42+
"coveralls": "^2.0.0"
43+
},
44+
"engines": {
45+
"node": ">=6.0.0"
46+
},
47+
"nyc": {
48+
"include": [
49+
"src/**/*.ts"
50+
],
51+
"exclude": [
52+
"lib"
53+
],
54+
"extension": [
55+
".ts"
56+
],
57+
"require": [
58+
"ts-node/register"
59+
],
60+
"reporter": [],
61+
"all": true
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"outDir": "lib_test",
6+
"declaration": false,
7+
"noImplicitAny": true,
8+
"removeComments": true,
9+
"inlineSourceMap": true
10+
},
11+
"include": [
12+
"test/**/*"
13+
]
14+
}

generators/app/templates/_vscode/tasks.json

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
{
1616
"taskName": "lint"
1717
},
18+
{
19+
"taskName": "prettier"
20+
},
1821
{
1922
"taskName": "coverage"
2023
},

generators/app/templates/gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules/
33
npm-debug.log
44
.nyc_output
55
lib
6+
lib_test
+4-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
<% if (isWindows) { %>
2-
/// <reference path="../node_modules/@types/mocha/index.d.ts" />
3-
<% } %>
1+
import test from 'ava';
42
import { Greeter } from "../src/greeter";
5-
import * as chai from "chai";
63

7-
const expect = chai.expect;
8-
9-
describe("greeter", () => {
10-
it("should greet with message", () => {
11-
const greeter = new Greeter("friend");
12-
expect(greeter.greet()).to.equal("Bonjour, friend!");
13-
});
4+
test("Should greet with message", t => {
5+
const greeter = new Greeter("friend");
6+
t.is(greeter.greet(), "Bonjour, friend!");
147
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<% if (isWindows) { %>
2+
/// <reference path="../node_modules/@types/mocha/index.d.ts" />
3+
<% } %>
4+
import { Greeter } from "../src/greeter";
5+
import * as chai from "chai";
6+
7+
const expect = chai.expect;
8+
9+
describe("greeter", () => {
10+
it("should greet with message", () => {
11+
const greeter = new Greeter("friend");
12+
expect(greeter.greet()).to.equal("Bonjour, friend!");
13+
});
14+
});
+5-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
<% if (isWindows) { %>
2-
/// <reference path="../node_modules/@types/mocha/index.d.ts" />
3-
<% } %>
4-
import index = require("../src/index");
5-
import * as chai from "chai";
1+
import test from "ava";
2+
import * as index from "../src/index";
63

7-
const expect = chai.expect;
8-
9-
describe("index", () => {
10-
it("should provide Greeter", () => {
11-
expect(index.Greeter).to.not.be.undefined;
12-
});
4+
test("Should have Greeter available", t => {
5+
t.truthy(index.Greeter);
136
});
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<% if (isWindows) { %>
2+
/// <reference path="../node_modules/@types/mocha/index.d.ts" />
3+
<% } %>
4+
import index = require("../src/index");
5+
import * as chai from "chai";
6+
7+
const expect = chai.expect;
8+
9+
describe("index", () => {
10+
it("should provide Greeter", () => {
11+
expect(index.Greeter).to.not.be.undefined;
12+
});
13+
});

0 commit comments

Comments
 (0)