Skip to content

Commit b9f22b8

Browse files
committed
Move to jest
1 parent 4a87b4f commit b9f22b8

16 files changed

+257
-123
lines changed

README.md

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@
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 packages using TypeScript. I let you quickly setup a project with latest available tools and best practices.
7+
Bonjour! 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

11-
- _npm_ - as task runner. You can choose to use _gulp_ instead.
12-
- _tslint_ - as linter for TypeScript source 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.
11+
- _npm_ - as task runner.
12+
- _jest_ - as [testing and coverage framework](https://facebook.github.io/jest/) to write specs in **TypeScript** itself. You can choose to use _mocha_ or _ava_ instead.
1513

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!
14+
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 and then quiety get out of the way!
1715

1816
## Usage
1917

20-
Install `generator-node-typescript` globally. If you are planning to use _gulp_, install `gulp-cli` globally.
18+
Install `generator-node-typescript` globally.
2119

2220
```sh
23-
$npm install -g generator-node-typescript
21+
$npm i -g generator-node-typescript
2422
```
2523

2624
Create a new directory and `cd` into it.
@@ -36,66 +34,22 @@ Run the generator.
3634
$yo node-typescript
3735
```
3836

39-
You can choose to use _gulp_ as your build system using command - `$yo node-typescript --gulp`
40-
4137
You can choose to use _mocha_ as your test framework using command - `$yo node-typescript --mocha`
4238

43-
Run `npm run` for information on available tasks.
44-
45-
```sh
46-
$npm run
47-
Lifecycle scripts included in ts-node-ava:
48-
prepublish
49-
npm run build
50-
test
51-
npm run clean && tsc -p tsconfig.test.json --pretty && nyc --exclude "**/*-spec.js" ava "**/*-spec.js" --verbose
52-
53-
available via `npm run-script`:
54-
clean
55-
rimraf lib && rimraf coverage && rimraf .nyc_output && rimraf lib_test
56-
format
57-
prettier --write "{src,test}/**/*.ts"
58-
lint
59-
tslint --force --format verbose "src/**/*.ts"
60-
prebuild
61-
npm run clean && npm run format && npm run lint && echo Using TypeScript && tsc --version
62-
build
63-
tsc --pretty
64-
coverage
65-
nyc report --reporter=lcov --reporter=text --reporter=html
66-
watch
67-
npm run build -- --watch
68-
watch:test
69-
npm run test -- --watch
70-
```
71-
72-
If you choose to use _gulp_, you can find the available tasks using command `gulp help`.
73-
74-
```sh
75-
$gulp help
76-
77-
Usage
78-
gulp [TASK] [OPTIONS...]
79-
80-
Available tasks
81-
build Compiles all TypeScript source files [lint]
82-
clean Cleans the generated js files from lib directory
83-
help Display this help text.
84-
lint Lints all TypeScript source files
85-
test Runs the mocha test specs [build]
86-
watch Watches ts source files and runs build on change
87-
```
39+
You can choose to use _ava_ as your test framework using command - `$yo node-typescript --ava`
8840

8941
## Highlights of the latest release
9042

9143
- I use latest version of **TypeScript**.
92-
- I use [yarn](https://yarnpkg.com) in place of npm if it is already installed on your machine.
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**.
44+
- I use [yarn](https://yarnpkg.com) in place of npm if it's available.
45+
- I use _jest_ which is a "batteries-included" testing framework with coverage built-in (optionally _mocha_ or _ava_). **You write tests in TypeScript itself**.
9446
- I use _prettier_ integrated with _tslint_ to provide no-fuss code formatting and linting.
95-
- 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.
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`, 'format' and `test` tasks that you can run using `Run Task` option.
98-
- **You can directly run the currently open TypeScript file in VS Code using task `ts-node: Run current file`.** I use [ts-node](https://github.com/TypeStrong/ts-node) to provide this functionality.
47+
- 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 packages.
48+
49+
### Integration with VS Code
50+
- I configure `build`, `clean`, `lint`, `coverage`, `format` and `test` tasks that you can run using `Run Task` option.
51+
- You can directly run currently open source file using task `Run current file`. I use [ts-node](https://github.com/TypeStrong/ts-node) to provide this functionality.
52+
- You can debug currently open source file using `Debug file` launch configuration. You can also debug currently open test file using `Debug test` launch configuration without the need of compiling it first.
9953

10054
## License
10155

generators/app/index.js

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = Generator.extend({
1818
this.log(
1919
chalk.cyan('I simply get down to business of generating, no questions asked!')
2020
+ '\n'
21-
+ chalk.yellow('Libraries you ask? I use npm (or optionally gulp) as task runner and mocha for testing.')
21+
+ chalk.yellow('Libraries you ask? I use npm (or optionally gulp) as task runner and jest for testing.')
2222
+ '\n'
2323
+ chalk.gray('Can you change these? Of course, it\'s your code. I get out of the way after scaffolding.')
2424
);
@@ -27,13 +27,15 @@ module.exports = Generator.extend({
2727
},
2828

2929
writing: {
30-
dir: function () {
30+
srcFiles: function () {
3131
this.fs.copy(
3232
this.templatePath('src'),
3333
this.destinationPath('src')
3434
);
35+
},
3536

36-
if (this.options.mocha) {
37+
testFiles: function () {
38+
if (this.options.mocha || this.options.gulp) {
3739
// 2.0.0-beta: copying the spec file needs templating due to the ts-node problem on windows
3840
// this.directory('test', 'test');
3941
this.fs.copyTpl(
@@ -46,27 +48,55 @@ module.exports = Generator.extend({
4648
this.destinationPath('test/index-spec.ts'),
4749
{ isWindows: process.platform === 'win32' }
4850
);
51+
} else if (this.options.ava) {
52+
this.fs.copyTpl(
53+
this.templatePath('test/greeter-spec_ava.ts'),
54+
this.destinationPath('test/greeter-spec.ts')
55+
);
56+
this.fs.copyTpl(
57+
this.templatePath('test/index-spec_ava.ts'),
58+
this.destinationPath('test/index-spec.ts')
59+
);
4960
} else {
5061
this.fs.copyTpl(
5162
this.templatePath('test/greeter-spec.ts'),
52-
this.destinationPath('test/greeter-spec.ts')
63+
this.destinationPath('__tests__/greeter-spec.ts')
5364
);
5465
this.fs.copyTpl(
5566
this.templatePath('test/index-spec.ts'),
56-
this.destinationPath('test/index-spec.ts')
67+
this.destinationPath('__tests__/index-spec.ts')
5768
);
5869
}
5970
},
6071

61-
projectfiles: function () {
62-
const today = new Date();
63-
72+
vsCodeFiles: function () {
6473
if (this.options.gulp) {
6574
this.fs.copy(
6675
this.templatePath('_vscode/tasks_gulp.json'),
6776
this.destinationPath('.vscode/tasks.json')
6877
);
78+
} else {
79+
this.fs.copy(
80+
this.templatePath('_vscode/tasks.json'),
81+
this.destinationPath('.vscode/tasks.json')
82+
);
83+
}
84+
this.fs.copy(
85+
this.templatePath('_vscode/settings.json'),
86+
this.destinationPath('.vscode/settings.json')
87+
);
88+
if (!(this.options.gulp || this.options.mocha || this.options.ava)) { // copy launch.json only for default jest configuration
89+
this.fs.copy(
90+
this.templatePath('_vscode/launch.json'),
91+
this.destinationPath('.vscode/launch.json')
92+
);
93+
}
94+
},
95+
96+
rootFiles: function () {
97+
const today = new Date();
6998

99+
if (this.options.gulp) { // copy gulp files
70100
this.fs.copyTpl(
71101
this.templatePath('_package_gulp.json'),
72102
this.destinationPath('package.json'),
@@ -84,50 +114,48 @@ module.exports = Generator.extend({
84114
this.destinationPath('README.md')
85115
);
86116
} else {
87-
this.fs.copy(
88-
this.templatePath('_vscode/tasks.json'),
89-
this.destinationPath('.vscode/tasks.json')
90-
);
91-
92-
if (this.options.mocha) {
117+
if (this.options.mocha) { // copy mocha files
93118
this.fs.copyTpl(
94119
this.templatePath('_package_mocha.json'),
95120
this.destinationPath('package.json'),
96121
{ appname: _.kebabCase(path.basename(process.cwd())) }
97122
);
98-
99123
this.fs.copy(
100124
this.templatePath('travis_mocha.yml'),
101125
this.destinationPath('.travis.yml')
102126
);
103-
} else {
127+
} if (this.options.ava) { // copy ava files
104128
this.fs.copyTpl(
105-
this.templatePath('_package.json'),
129+
this.templatePath('_package_ava.json'),
106130
this.destinationPath('package.json'),
107131
{ appname: _.kebabCase(path.basename(process.cwd())) }
108132
);
109-
110133
this.fs.copy(
111-
this.templatePath('travis.yml'),
134+
this.templatePath('travis_ava.yml'),
112135
this.destinationPath('.travis.yml')
113136
);
114-
115137
this.fs.copy(
116138
this.templatePath('_tsconfig.test.json'),
117139
this.destinationPath('tsconfig.test.json')
118140
);
141+
} else { // copy files for default jest configuration
142+
this.fs.copyTpl(
143+
this.templatePath('_package.json'),
144+
this.destinationPath('package.json'),
145+
{ appname: _.kebabCase(path.basename(process.cwd())) }
146+
);
147+
this.fs.copy(
148+
this.templatePath('travis.yml'),
149+
this.destinationPath('.travis.yml')
150+
);
119151
}
120-
152+
// copy readme for non-gulp configurations
121153
this.fs.copy(
122154
this.templatePath('README.md'),
123155
this.destinationPath('README.md')
124156
);
125157
}
126-
127-
this.fs.copy(
128-
this.templatePath('_vscode/settings.json'),
129-
this.destinationPath('.vscode/settings.json')
130-
);
158+
// copy files for default jest configuration
131159
this.fs.copy(
132160
this.templatePath('_tsconfig.json'),
133161
this.destinationPath('tsconfig.json')

generators/app/templates/_gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ gulp.task('build', 'Compiles all TypeScript source files', ['lint'], function (c
4444
});
4545
});
4646

47-
gulp.task('test', 'Runs the Jasmine test specs', ['build'], function () {
47+
gulp.task('test', 'Runs the Mocha test specs', ['build'], function () {
4848
return gulp.src('test/*.ts')
4949
.pipe(mocha({
5050
require: ['ts-node/register']

generators/app/templates/_package.json

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
"description": "<%= appname %>",
55
"license": "MIT",
66
"repository": "",
7-
"author": "",
7+
"author": {
8+
"name": "",
9+
"email": "",
10+
"url": ""
11+
},
812
"keywords": [
913
""
1014
],
@@ -14,29 +18,28 @@
1418
"main": "lib/index.js",
1519
"typings": "lib/index.d.ts",
1620
"scripts": {
17-
"clean": "rimraf lib && rimraf coverage && rimraf .nyc_output && rimraf lib_test",
18-
"format": "prettier --write \"{src,test}/**/*.ts\"",
21+
"clean": "rimraf lib && rimraf coverage",
22+
"format": "prettier --write \"{src,__tests__}/**/*.ts\" --single-quote --trailing-comma es5",
1923
"lint": "tslint --force --format verbose \"src/**/*.ts\"",
2024
"prepublish": "npm run build",
2125
"prebuild": "npm run clean && npm run format && npm run lint && echo Using TypeScript && tsc --version",
22-
"build": "tsc --pretty && tsc -p tsconfig.test.json --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",
26+
"build": "tsc --pretty",
27+
"test": "jest",
28+
"coverage": "jest --coverage",
2529
"watch": "npm run build -- --watch",
26-
"watch:test:ts": "tsc -p tsconfig.test.json --pretty --watch",
27-
"watch:test:ava": "ava \"**/*-spec.js\" --verbose --watch",
28-
"watch:test": "run-p watch:test:ts watch:test:ava"
30+
"watch:test": "jest --watch"
2931
},
3032
"dependencies": {
3133
},
3234
"devDependencies": {
35+
"@types/jest": "^20.0.5",
3336
"@types/node": "^8.0.0",
34-
"ava": "^0.20.0",
3537
"coveralls": "^2.0.0",
36-
"npm-run-all": "^4.0.2",
37-
"nyc": "^10.0.0",
38+
"jest": "^20.0.4",
39+
"jest-environment-node-debug": "^2.0.0",
3840
"prettier": "^1.5.2",
3941
"rimraf": "^2.0.0",
42+
"ts-jest": "^20.0.7",
4043
"ts-node": "^3.2.0",
4144
"tslint": "^5.0.0",
4245
"tslint-config-prettier": "^1.1.0",
@@ -45,9 +48,15 @@
4548
"engines": {
4649
"node": ">=6.0.0"
4750
},
48-
"ava": {
49-
"require": [
50-
"babel-register"
51-
]
51+
"jest": {
52+
"transform": {
53+
".(ts)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
54+
},
55+
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|js)$",
56+
"moduleFileExtensions": [
57+
"ts",
58+
"js"
59+
],
60+
"testEnvironment": "node"
5261
}
5362
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 && rimraf coverage && rimraf .nyc_output && rimraf lib_test",
18+
"format": "prettier --write \"{src,test}/**/*.ts\"",
19+
"lint": "tslint --force --format verbose \"src/**/*.ts\"",
20+
"prepublish": "npm run build",
21+
"prebuild": "npm run clean && npm run format && npm run lint && echo Using TypeScript && tsc --version",
22+
"build": "tsc --pretty && tsc -p tsconfig.test.json --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",
25+
"watch": "npm run build -- --watch",
26+
"watch:test:ts": "tsc -p tsconfig.test.json --pretty --watch",
27+
"watch:test:ava": "ava \"**/*-spec.js\" --verbose --watch",
28+
"watch:test": "run-p watch:test:ts watch:test:ava"
29+
},
30+
"dependencies": {
31+
},
32+
"devDependencies": {
33+
"@types/node": "^8.0.0",
34+
"ava": "^0.20.0",
35+
"coveralls": "^2.0.0",
36+
"npm-run-all": "^4.0.2",
37+
"nyc": "^10.0.0",
38+
"prettier": "^1.5.2",
39+
"rimraf": "^2.0.0",
40+
"ts-node": "^3.2.0",
41+
"tslint": "^5.0.0",
42+
"tslint-config-prettier": "^1.1.0",
43+
"typescript": "^2.0.0"
44+
},
45+
"engines": {
46+
"node": ">=6.0.0"
47+
},
48+
"ava": {
49+
"require": [
50+
"babel-register"
51+
]
52+
}
53+
}

0 commit comments

Comments
 (0)