Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit 41df56b

Browse files
committed
fix: refactor tests, add angular-webpack-starter test, also fixes #286
1 parent e98303f commit 41df56b

10 files changed

+133
-31
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ After that, you will be able to build TypeScript files with webpack.
6464

6565
## NodeJS versions
6666

67-
The loader supports NodeJS 4 and newer.
67+
**The loader supports NodeJS 4 and newer.**
6868

6969
## tsconfig.json
7070

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,25 @@
3737
"loader-utils": "^0.2.16",
3838
"lodash": "^4.17.4",
3939
"object-assign": "^4.1.1",
40-
"source-map-support": "^0.4.11"
40+
"source-map-support": "^0.4.11",
41+
"mkdirp": "^0.5.1"
4142
},
4243
"devDependencies": {
4344
"@types/chai": "^3.4.34",
4445
"@types/colors": "^1.1.1",
4546
"@types/lodash": "^4.14.52",
4647
"@types/mocha": "^2.2.39",
4748
"@types/node": "^7.0.5",
49+
"@types/shelljs": "^0.6.0",
4850
"@types/sinon": "^1.16.34",
4951
"bluebird": "^3.4.7",
5052
"chai": "^3.5.0",
5153
"empty-module": "0.0.2",
5254
"fs-extra": "^2.0.0",
53-
"mkdirp": "^0.5.1",
5455
"mocha": "^3.2.0",
5556
"ps-node": "^0.1.1",
5657
"rimraf": "^2.5.0",
58+
"shelljs": "^0.7.6",
5759
"standard-version": "^4.0.0",
5860
"temp": "^0.8.3",
5961
"tslint": "^4.4.2",
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
exec as run, spec, expect, stdout
3+
} from './utils';
4+
5+
import { exec, ln } from 'shelljs';
6+
7+
spec(__filename, async function(_env, done) {
8+
this.timeout(5 * 60 * 1000);
9+
10+
exec('rimraf package.json');
11+
exec('git clone --depth 1 https://github.com/angularclass/angular2-webpack-starter.git .');
12+
exec('yarn install');
13+
exec('rimraf node_modules/awesome-typescript-loader');
14+
ln('-s', _env.LOADER, './node_modules/awesome-typescript-loader');
15+
16+
const wp = run('npm', ['run', 'webpack']);
17+
18+
await wp.wait(
19+
stdout('[at-loader] Ok')
20+
);
21+
22+
const code = await wp.alive();
23+
expect(code).eq(0);
24+
25+
done();
26+
});

src/__test__/compile-output.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
src, tsconfig, stdout, stderr,
3-
spec, file, exec
3+
spec, file, execWebpack
44
} from './utils';
55

66
export function config(env) {
@@ -43,7 +43,8 @@ spec(__filename, async function(env, done) {
4343
tsconfig();
4444
config(env);
4545

46-
const webpack = exec('webpack');
46+
const webpack = execWebpack();
47+
webpack.strictOutput();
4748

4849
await webpack.wait(
4950
stderr('Checking finished with 1 errors'),

src/__test__/create-cache-dir.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {
2+
src, webpackConfig, tsconfig, compile, expect,
3+
query, spec
4+
} from './utils';
5+
6+
import * as path from 'path';
7+
import * as fs from 'fs';
8+
9+
spec(__filename, async function() {
10+
src('index.ts', `
11+
class HiThere {
12+
constructor(a: number, b: string) {
13+
const t = a + b;
14+
}
15+
}
16+
`);
17+
18+
tsconfig();
19+
20+
const config = webpackConfig(query({
21+
useCache: true,
22+
// test that we create cache dir
23+
cacheDirectory: path.join(process.cwd(), 'cache', '.cache'),
24+
}));
25+
26+
await compile(config);
27+
28+
const exists = fs.existsSync(path.join(process.cwd(), 'cache', '.cache'));
29+
expect(exists).false;
30+
});

src/__test__/utils.ts

+30-10
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,17 @@ export class Exec {
9999
matchers: OutputMatcher[],
100100
}[] = [];
101101

102+
exitCode: number | null;
103+
private _strictOutput = false;
104+
102105
close() {
103106
this.process.kill();
104107
}
105108

109+
strictOutput() {
110+
this._strictOutput = true;
111+
}
112+
106113
invoke({stdout, stderr}) {
107114
this.watchers = this.watchers.filter(watcher => {
108115
const output: Output = {
@@ -112,7 +119,7 @@ export class Exec {
112119

113120
const index = watcher.matchers.findIndex(m => m(output));
114121

115-
if (index === -1) {
122+
if (this._strictOutput && index === -1) {
116123
watcher.reject(new Error(`Unexpected ${output.type}:\n${output.data}`));
117124
return false;
118125
}
@@ -141,7 +148,11 @@ export class Exec {
141148

142149
alive(): Promise<any> {
143150
return new Promise((resolve, reject) => {
144-
this.process.on('exit', resolve);
151+
if (this.exitCode != null) {
152+
resolve(this.exitCode);
153+
} else {
154+
this.process.on('exit', resolve);
155+
}
145156
});
146157
}
147158
}
@@ -177,8 +188,16 @@ export function streamTest(stream = 'stdout', test: Test) {
177188
export const stdout = (test: Test) => streamTest('stdout', test);
178189
export const stderr = (test: Test) => streamTest('stderr', test);
179190

180-
export function exec(command: string, args?: string[], options?: child.SpawnOptions) {
181-
const p = child.spawn('node', [WEBPACK].concat(args), {
191+
export function execWebpack(args?: string[]) {
192+
return execNode(WEBPACK, args);
193+
}
194+
195+
export function execNode(command: string, args: string[] = []) {
196+
return exec('node', [command].concat(args));
197+
}
198+
199+
export function exec(command: string, args?: string[]) {
200+
const p = child.spawn(command, args, {
182201
shell: false,
183202
stdio: 'pipe',
184203
env: process.env
@@ -200,7 +219,8 @@ export function exec(command: string, args?: string[], options?: child.SpawnOpti
200219
p.kill();
201220
});
202221

203-
process.on('exit', () => {
222+
process.on('exit', (code) => {
223+
waiter.exitCode = code;
204224
p.kill();
205225
});
206226

@@ -275,7 +295,7 @@ export interface TestEnv {
275295
}
276296

277297
export function spec<T>(name: string, cb: (env: TestEnv, done?: () => void) => Promise<T>, disable = false) {
278-
const runner = (done?) => {
298+
const runner = function (done?) {
279299
const temp = path.join(
280300
TEST_DIR,
281301
path.basename(name).replace('.', '') + '-' +
@@ -297,7 +317,7 @@ export function spec<T>(name: string, cb: (env: TestEnv, done?: () => void) => P
297317
WEBPACK
298318
};
299319

300-
const promise = cb(env, done);
320+
const promise = cb.call(this, env, done);
301321
return promise
302322
.then(a => {
303323
process.chdir(cwd);
@@ -310,8 +330,8 @@ export function spec<T>(name: string, cb: (env: TestEnv, done?: () => void) => P
310330
};
311331

312332
const asyncRunner = cb.length === 2
313-
? (done) => { runner(done).catch(done); return; }
314-
: () => runner();
333+
? function (done) { runner.call(this, done).catch(done); return; }
334+
: function () { return runner.call(this); };
315335

316336
if (disable) {
317337
xit(name, asyncRunner);
@@ -320,7 +340,7 @@ export function spec<T>(name: string, cb: (env: TestEnv, done?: () => void) => P
320340
}
321341
}
322342

323-
export function xspec<T>(name: string, cb: () => Promise<T>) {
343+
export function xspec<T>(name: string, cb: (env: TestEnv, done?: () => void) => Promise<T>) {
324344
return spec(name, cb, true);
325345
}
326346

src/__test__/watch-output.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
src, tsconfig, stdout, stderr,
3-
spec, exec
3+
spec, execWebpack
44
} from './utils';
55

66
import { config } from './compile-output';
@@ -17,14 +17,15 @@ spec(__filename, async function(env, done) {
1717
tsconfig();
1818
config(env);
1919

20-
const webpack = exec('webpack', ['--watch']);
20+
const webpack = execWebpack([ '--watch' ]);
21+
webpack.strictOutput();
2122

2223
await webpack.wait(
2324
stdout('Webpack is watching the files…'),
2425
stderr('Checking finished with 1 errors'),
2526
stdout([
2627
'ERROR in [at-loader]',
27-
`Argument of type '"1"' is not assignable to parameter of type 'number'`
28+
`TS2345: Argument of type '"1"' is not assignable to parameter of type 'number'`
2829
]),
2930
);
3031

src/checker/runtime.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,15 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
401401
let pretty = '';
402402
let line = 0;
403403
let character = 0;
404+
let code = diagnostic.code;
404405

405406
if (diagnostic.file) {
406407
const pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
407408
line = pos.line;
408409
character = pos.character;
409-
pretty = (`[${ instanceName }] ${colors.red(fileName)}:${line + 1}:${character + 1} \n ${colors.red(message)}`);
410+
pretty = (`[${ instanceName }] ${colors.red(fileName)}:${line + 1}:${character + 1} \n TS${code}: ${colors.red(message)}`);
410411
} else {
411-
pretty = (colors.red(`[${ instanceName }] ${ message }`));
412+
pretty = (colors.red(`[${ instanceName }] TS${code}: ${ message }`));
412413
}
413414

414415
return {

src/instance.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { WatchModeSymbol } from './watch-mode';
99

1010
let colors = require('colors/safe');
1111
let pkg = require('../package.json');
12+
let mkdirp = require('mkdirp');
1213

1314
export interface Instance {
1415
id: number;
@@ -185,7 +186,7 @@ function setupCache(
185186
}
186187

187188
if (!fs.existsSync(loaderConfig.cacheDirectory)) {
188-
fs.mkdirSync(loaderConfig.cacheDirectory);
189+
mkdirp.sync(loaderConfig.cacheDirectory);
189190
}
190191

191192
cacheIdentifier = {

yarn.lock

+30-10
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@
1818
version "2.2.39"
1919
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.39.tgz#f68d63db8b69c38e9558b4073525cf96c4f7a829"
2020

21-
"@types/node@^7.0.5":
21+
"@types/node@*", "@types/node@^7.0.5":
2222
version "7.0.5"
2323
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.5.tgz#96a0f0a618b7b606f1ec547403c00650210bfbb7"
2424

25+
"@types/shelljs@^0.6.0":
26+
version "0.6.0"
27+
resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.6.0.tgz#090b705c102ce7fc5c0c5ea9b524418ff15840df"
28+
dependencies:
29+
"@types/node" "*"
30+
2531
"@types/sinon@^1.16.34":
2632
version "1.16.34"
2733
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-1.16.34.tgz#a9761fff33d0f7b3fe61875b577778a2576a9a03"
@@ -1106,7 +1112,7 @@ [email protected], glob@^7.0.5:
11061112
once "^1.3.0"
11071113
path-is-absolute "^1.0.0"
11081114

1109-
glob@^7.1.1:
1115+
glob@^7.0.0, glob@^7.1.1:
11101116
version "7.1.1"
11111117
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8"
11121118
dependencies:
@@ -2049,22 +2055,22 @@ read-pkg@^1.0.0, read-pkg@^1.1.0:
20492055
normalize-package-data "^2.3.2"
20502056
path-type "^1.0.0"
20512057

2052-
readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@~2.0.0:
2053-
version "2.0.6"
2054-
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e"
2058+
readable-stream@^2.0.0, "readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.1.0:
2059+
version "2.2.2"
2060+
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.2.tgz#a9e6fec3c7dda85f8bb1b3ba7028604556fc825e"
20552061
dependencies:
2062+
buffer-shims "^1.0.0"
20562063
core-util-is "~1.0.0"
20572064
inherits "~2.0.1"
20582065
isarray "~1.0.0"
20592066
process-nextick-args "~1.0.6"
20602067
string_decoder "~0.10.x"
20612068
util-deprecate "~1.0.1"
20622069

2063-
"readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.1.0:
2064-
version "2.2.2"
2065-
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.2.tgz#a9e6fec3c7dda85f8bb1b3ba7028604556fc825e"
2070+
readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@~2.0.0:
2071+
version "2.0.6"
2072+
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e"
20662073
dependencies:
2067-
buffer-shims "^1.0.0"
20682074
core-util-is "~1.0.0"
20692075
inherits "~2.0.1"
20702076
isarray "~1.0.0"
@@ -2093,6 +2099,12 @@ readdirp@^2.0.0:
20932099
readable-stream "^2.0.2"
20942100
set-immediate-shim "^1.0.1"
20952101

2102+
rechoir@^0.6.2:
2103+
version "0.6.2"
2104+
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"
2105+
dependencies:
2106+
resolve "^1.1.6"
2107+
20962108
redent@^1.0.0:
20972109
version "1.0.0"
20982110
resolved "https://registry.yarnpkg.com/redent/-/redent-1.0.0.tgz#cf916ab1fd5f1f16dfb20822dd6ec7f730c2afde"
@@ -2166,7 +2178,7 @@ require-main-filename@^1.0.1:
21662178
version "1.0.1"
21672179
resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1"
21682180

2169-
resolve@^1.1.7:
2181+
resolve@^1.1.6, resolve@^1.1.7:
21702182
version "1.1.7"
21712183
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
21722184

@@ -2218,6 +2230,14 @@ sha.js@^2.3.6:
22182230
dependencies:
22192231
inherits "^2.0.1"
22202232

2233+
shelljs@^0.7.6:
2234+
version "0.7.6"
2235+
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.6.tgz#379cccfb56b91c8601e4793356eb5382924de9ad"
2236+
dependencies:
2237+
glob "^7.0.0"
2238+
interpret "^1.0.0"
2239+
rechoir "^0.6.2"
2240+
22212241
signal-exit@^3.0.0:
22222242
version "3.0.1"
22232243
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.1.tgz#5a4c884992b63a7acd9badb7894c3ee9cfccad81"

0 commit comments

Comments
 (0)