Skip to content

Commit 0bd9c5d

Browse files
authored
Merge pull request #52 from power-assert-js/next
merge v9 to master
2 parents 38eced7 + 14b839f commit 0bd9c5d

File tree

13 files changed

+1112
-146
lines changed

13 files changed

+1112
-146
lines changed

.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["teppeis/node-v6", "teppeis/+prettier", "teppeis/+mocha"]
3+
}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
test/**/*.js
1+
/node_modules
2+
test/test-outdir/**/*.js

.travis.yml

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,12 @@ node_js:
55
- "10"
66
sudo: false
77
script:
8-
- npm install --no-save typescript@2.2
8+
- npm install -D typescript@2.4
99
- npm test
10-
- npm install --no-save typescript@2.3
10+
- npm install -D typescript@2.8
1111
- npm test
12-
- npm install --no-save [email protected]
13-
- npm test
14-
- npm install --no-save [email protected]
15-
- npm test
16-
- npm install --no-save [email protected]
17-
- npm test
18-
- npm install --no-save [email protected]
19-
- npm test
20-
- npm install --no-save [email protected]
21-
- npm test
22-
- npm install --no-save typescript@latest
12+
- npm install -D [email protected]
2313
- npm test
2414
cache:
2515
directories:
26-
- node_modules
16+
- $HOME/.npm

README.md

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
espower-typescript
2-
====
1+
# espower-typescript
32

43
> power-assert instrumentor for TypeScript
54
@@ -11,44 +10,57 @@ espower-typescript
1110

1211
## TypeScript versions
1312

14-
* espower-typescript v8.x uses TypeScript from v2.2 to v2.9
15-
* espower-typescript v7.x uses TypeScript v2.1
16-
* espower-typescript v6.x uses TypeScript v2.0
17-
* espower-typescript v5.x uses TypeScript v1.8
18-
* espower-typescript v4.x uses TypeScript v1.7
19-
* espower-typescript v2.x and v3.x uses TypeScript v1.6
20-
* espower-typescript v1.x uses TypeScript v1.5
13+
espower-typescript v9.x is compatible with TypeScript v2.4+
2114

22-
## Usage
15+
## Usage (zero-config mode)
2316

24-
### Install
17+
NOTE: If you use older version than v9, see [older document](https://github.com/power-assert-js/espower-typescript/blob/v8.1.3/README.md).
18+
19+
Install
2520

2621
```console
27-
$ npm install -D espower-typescript power-assert mocha
22+
$ npm install -D espower-typescript power-assert mocha typescript @types/node @types/mocha
2823
```
2924

30-
### Zero-config mode
25+
Create a test file (intensionally failed)
3126

3227
```typescript
3328
// test/test.ts
3429
import assert = require('assert');
3530

3631
describe('Array#join', () => {
3732
it('joins all elements into a string with separator', () => {
38-
assert(['a', 'b', 'c'].join(':') === 'a:b:c');
33+
assert(['a', 'b', 'c'].join(':') === 'a:b:c:');
3934
});
4035
});
4136
```
4237

38+
Run test
39+
4340
```console
44-
// for mocha version >= 4
4541
$ ./node_modules/.bin/mocha --require espower-typescript/guess "test/**/*.ts"
42+
```
43+
44+
Output
4645

46+
```
47+
1) Array#join
48+
joins all elements into a string with separator:
49+
50+
AssertionError [ERR_ASSERTION]: # test.ts:6
4751
48-
// for mocha version < 4 (deprecated)
49-
$ ./node_modules/.bin/mocha --compilers ts:espower-typescript/guess "test/**/*.ts"
52+
assert(['a','b','c'].join(':') === 'a:b:c:')
53+
| | |
54+
["a","b","c"] "a:b:c" false
5055
```
5156

57+
### CAUTION: don't use `import 'assert' from 'assert'`
58+
59+
Just use old style `import 'assert' = require('assert')` for assert module.
60+
This is limitation.
61+
62+
## Configure
63+
5264
### If your tests are not in `test` directory
5365

5466
You can set test directory in your `package.json`
@@ -68,36 +80,43 @@ You can set test directory in your `package.json`
6880
Then, run mocha with `--require espower-typescript/guess`
6981

7082
```console
71-
// for mocha version >= 4
7283
$ ./node_modules/.bin/mocha --require espower-typescript/guess "spec/**/*.ts"
73-
74-
// for mocha version < 4 (deprecated)
75-
$ ./node_modules/.bin/mocha --compilers ts:espower-typescript/guess "spec/**/*.ts"
7684
```
7785

7886
Note: `'espower-typescript/guess'` is inspired by [intelli-espower-loader](https://github.com/azu/intelli-espower-loader)
7987

80-
### tsconfig.json and CompilerOptions
88+
### ts-node and `tsconfig.json`
89+
90+
espower-typescript uses [ts-node](https://github.com/TypeStrong/ts-node) internally.
91+
It loads your [tsconfig.json](https://github.com/Microsoft/TypeScript/wiki/tsconfig.json) automatically.
92+
93+
### Disable type check (transpile only)
8194

82-
If [tsconfig.json](https://github.com/Microsoft/TypeScript/wiki/tsconfig.json) is in your prject root, `'espower-typescript/guess'` loads it automatically.
95+
Use `TS_NODE_TRANSPILE_ONLY` env of ts-node
8396

84-
Note: only `compilerOptions` field in tsconfig.json is applied.
97+
```console
98+
$ TS_NODE_TRANSPILE_ONLY=1 ./node_modules/.bin/mocha --require espower-typescript/guess "test/**/*.ts"
99+
```
85100

86101
### JSX/React
87102

88103
`.tsx` files are supported.
89104

105+
### `allowJs`
106+
107+
If `allowJs: true` in your `tsconfig.json`, assertions in `test/**/*.(js|jsx)` are empowered.
108+
90109
## License
91110

92-
* MIT License: Teppei Sato &lt;[email protected]&gt;
93-
* Includes [yosuke-furukawa/espower-traceur](https://github.com/yosuke-furukawa/espower-traceur)
94-
* Includes [azu/espower-babel](https://github.com/azu/espower-babel)
111+
- MIT License: Teppei Sato &lt;[email protected]&gt;
112+
- Includes [yosuke-furukawa/espower-traceur](https://github.com/yosuke-furukawa/espower-traceur)
113+
- Includes [azu/espower-babel](https://github.com/azu/espower-babel)
95114

96115
[npm-image]: https://img.shields.io/npm/v/espower-typescript.svg
97116
[npm-url]: https://npmjs.org/package/espower-typescript
98117
[travis-image]: https://travis-ci.org/power-assert-js/espower-typescript.svg?branch=master
99118
[travis-url]: https://travis-ci.org/power-assert-js/espower-typescript
100119
[deps-image]: https://david-dm.org/power-assert-js/espower-typescript.svg
101120
[deps-url]: https://david-dm.org/power-assert-js/espower-typescript
102-
[node-version]: https://img.shields.io/badge/Node.js%20support-v4,v6,v8-brightgreen.svg
121+
[node-version]: https://img.shields.io/badge/Node.js%20support-v6,v8,v10-brightgreen.svg
103122
[license]: https://img.shields.io/npm/l/espower-typescript.svg

guess.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
1-
var fs = require('fs');
2-
var path = require('path');
1+
'use strict';
32

4-
var ts = require('typescript');
3+
const path = require('path');
4+
const ts = require('typescript');
55

6-
var pattern = 'test/**/*.@(ts|tsx)';
7-
var cwd = process.cwd();
8-
var packageData = require(path.join(cwd, 'package.json'));
9-
10-
if (packageData &&
11-
typeof packageData.directories === 'object' &&
12-
typeof packageData.directories.test === 'string') {
13-
var testDir = packageData.directories.test;
14-
pattern = testDir + ((testDir.lastIndexOf('/', 0) === 0) ? '' : '/') + '**/*.@(ts|tsx)';
6+
const cwd = process.cwd();
7+
const compilerOptions = loadCompilerOptions(cwd) || {};
8+
const extensions = ['ts', 'tsx'];
9+
if (compilerOptions.allowJs) {
10+
extensions.push('js');
11+
extensions.push('jsx');
1512
}
1613

17-
var tsconfigPath = ts.findConfigFile(cwd, fs.existsSync);
18-
var tsconfigBasepath = null;
19-
var compilerOptions = null;
20-
if (tsconfigPath) {
21-
compilerOptions = parseTsConfig(tsconfigPath);
22-
tsconfigBasepath = path.dirname(tsconfigPath);
14+
let testDir = 'test';
15+
const packageData = require(path.join(cwd, 'package.json'));
16+
if (
17+
packageData &&
18+
typeof packageData.directories === 'object' &&
19+
typeof packageData.directories.test === 'string'
20+
) {
21+
testDir = packageData.directories.test;
2322
}
23+
const pattern = path.join(testDir, `**/*.@(${extensions.join('|')})`);
2424

25-
require('./index')({
26-
cwd: cwd,
27-
pattern: pattern,
28-
compilerOptions: compilerOptions,
29-
basepath: tsconfigBasepath
30-
});
31-
32-
function parseTsConfig(tsconfigPath) {
33-
var parsed = ts.parseConfigFileTextToJson(tsconfigPath, fs.readFileSync(tsconfigPath, 'utf8'));
34-
if (parsed.error) {
35-
throw new Error(parsed.error.messageText);
36-
}
25+
require('./index')({cwd, pattern, extensions});
3726

38-
if (!parsed.config || !parsed.config.compilerOptions) {
27+
function loadCompilerOptions(cwd) {
28+
const tsconfigPath = ts.findConfigFile(cwd, ts.sys.fileExists);
29+
if (!tsconfigPath) {
3930
return null;
4031
}
41-
42-
return parsed.config.compilerOptions;
32+
const result = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
33+
if (result.error) {
34+
throw new Error(result.error.messageText);
35+
}
36+
if (result.config && result.config.compilerOptions) {
37+
const basepath = path.dirname(tsconfigPath);
38+
const {options} = ts.parseJsonConfigFileContent(result.config, ts.sys, basepath);
39+
return options;
40+
}
41+
return null;
4342
}

index.js

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
1-
'use strict';
1+
/* eslint node/no-deprecated-api: [error, {ignoreGlobalItems: ["require.extensions"]}] */
22

3-
var fs = require('fs');
4-
var path = require('path');
3+
'use strict';
54

6-
var espowerSource = require('espower-source');
7-
var minimatch = require('minimatch');
8-
var ts = require('typescript');
9-
var TypeScriptSimple = require('typescript-simple').TypeScriptSimple;
5+
const path = require('path');
6+
const espowerSource = require('espower-source');
7+
const minimatch = require('minimatch');
8+
const tsNodeRegister = require('ts-node').register;
9+
10+
function espowerTypeScript(options, tsNodeOptions) {
11+
tsNodeRegister(tsNodeOptions);
12+
const {extensions = ['ts', 'tsx']} = options;
13+
extensions.forEach(ext => {
14+
espowerTsRegister(`.${ext}`, options);
15+
});
16+
}
1017

11-
function espowerTypeScript(options) {
12-
var cwd = options.cwd || process.cwd();
13-
var separator = (options.pattern.lastIndexOf('/', 0) === 0) ? '' : '/';
14-
var pattern = cwd + separator + options.pattern;
15-
var compilerOptions = convertCompilerOptions(options.compilerOptions, options.basepath || cwd);
16-
var tss = new TypeScriptSimple(compilerOptions, false);
18+
function espowerTsRegister(ext, options) {
19+
const cwd = options.cwd || process.cwd();
20+
const pattern = path.join(cwd, options.pattern);
1721

18-
function loadTypeScript(localModule, filepath) {
19-
var result = tss.compile(fs.readFileSync(filepath, 'utf-8'), path.relative(cwd, filepath));
20-
if (minimatch(filepath, pattern)) {
21-
result = espowerSource(result, filepath, options);
22+
const originalExtension = require.extensions[ext];
23+
require.extensions[ext] = (module, filepath) => {
24+
if (!minimatch(filepath, pattern)) {
25+
return originalExtension(module, filepath);
2226
}
23-
localModule._compile(result, filepath);
27+
const originalCompile = module._compile;
28+
module._compile = function(code, filepath) {
29+
return originalCompile.call(this, espowerSource(code, filepath, options), filepath);
30+
};
31+
return originalExtension(module, filepath);
2432
};
25-
26-
require.extensions['.ts'] = loadTypeScript;
27-
require.extensions['.tsx'] = loadTypeScript;
28-
}
29-
30-
function convertCompilerOptions(compilerOptions, basepath) {
31-
if (!compilerOptions) {
32-
return null;
33-
}
34-
35-
var converted = ts.convertCompilerOptionsFromJson(compilerOptions, basepath);
36-
if (converted.errors && converted.errors.length > 0) {
37-
var msg = converted.errors.map(function(e) {return e.messageText}).join(', ');
38-
throw new Error(msg);
39-
}
40-
return converted.options;
4133
}
4234

4335
module.exports = espowerTypeScript;

0 commit comments

Comments
 (0)