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

Commit 08578d2

Browse files
committed
test(*): add regression test on watch
1 parent 6442a87 commit 08578d2

File tree

6 files changed

+548
-12
lines changed

6 files changed

+548
-12
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
"mocha": "^2.3.4",
6868
"ps-node": "0.0.5",
6969
"rimraf": "^2.5.0",
70+
"sinon": "^1.17.4",
71+
"temp": "^0.8.3",
7072
"tslint": "3.5.0-dev.1",
7173
"typescript": "^1.9.0-dev.20160509",
7274
"webpack": "2.1.0-beta.4"

src/test/checker.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ describe('checker test', function() {
4949

5050
it('should fork checker in separate process', async function() {
5151
await cleanOutputDir();
52-
let watcher = await watch(config, () => {});
52+
53+
let watcher = watch(config);
54+
await watcher.wait();
55+
5356
let pid = await getCheckerRuntimeProcess();
5457
expect(pid).ok;
5558
watcher.close();
@@ -60,7 +63,10 @@ describe('checker test', function() {
6063
// I didn't get how to test it more precise, so it's more like a proof of work
6164

6265
await cleanOutputDir();
63-
let watcher = await watch(config, () => {});
66+
67+
let watcher = watch(config, () => {});
68+
await watcher.wait();
69+
6470
let pid = await getCheckerRuntimeProcess();
6571

6672
expect(pid).ok;

src/test/utils.ts

+59-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as path from 'path';
22
import * as fs from 'fs';
33
import * as _ from 'lodash';
44

5+
const temp = require('temp').track();
6+
57
require('babel-polyfill');
68
require('source-map-support').install();
79

@@ -133,17 +135,64 @@ export function compile(config): Promise<any> {
133135
});
134136
}
135137

136-
export function watch(config, cb: (err, stats) => void): Promise<{ close(): void }> {
138+
export function watch(config, cb?: (err, stats) => void): Watch {
137139
let compiler = webpack(config);
138-
return new Promise((resolve, reject) => {
139-
let watcher = compiler.watch({}, (err, stats) => {
140-
if (err) {
141-
reject(err);
142-
} else {
143-
resolve(watcher);
144-
}
145-
140+
let watch = new Watch();
141+
let webpackWatcher = compiler.watch({}, (err, stats) => {
142+
watch.call(err, stats);
143+
if (cb) {
146144
cb(err, stats);
147-
});
145+
}
148146
});
147+
148+
watch.close = webpackWatcher.close;
149+
return watch;
150+
}
151+
152+
class Watch {
153+
resolves: ((arg: any[]) => void)[] = [];
154+
close: () => void;
155+
156+
call(err, stats) {
157+
this.resolves.forEach(resolver => {
158+
resolver([err, stats]);
159+
});
160+
this.resolves = [];
161+
}
162+
163+
wait(): Promise<any> {
164+
return new Promise((resolve, reject) => {
165+
this.resolves.push(resolve);
166+
});
167+
}
168+
}
169+
170+
export class Fixture {
171+
private text: string;
172+
private fileName: string;
173+
constructor(text: string, ext = '.tsx') {
174+
this.text = text;
175+
let tmpobj = temp.openSync({
176+
prefix: 'atl-',
177+
suffix: '.tsx'
178+
});
179+
180+
this.fileName = tmpobj.path;
181+
182+
fs.writeFileSync(this.fileName, text);
183+
}
184+
185+
path() {
186+
return this.fileName;
187+
}
188+
189+
touch() {
190+
touchFile(this.fileName);
191+
}
192+
193+
update(updater: (text: string) => string) {
194+
let newText = updater(this.text);
195+
this.text = newText;
196+
fs.writeFileSync(this.fileName, newText);
197+
}
149198
}

src/test/watch-type-errors.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
expect, Fixture, createConfig, cleanOutputDir, watch
3+
} from './utils';
4+
5+
describe('checker test', function() {
6+
this.timeout(5000);
7+
8+
let fixture = new Fixture(`
9+
let a: string;
10+
function check(arg1: string) { }
11+
check(a);
12+
`, '.ts');
13+
14+
let config = createConfig(
15+
{
16+
entry: fixture.path(),
17+
},
18+
{
19+
watch: true,
20+
}
21+
);
22+
23+
it('should watch changes', async function() {
24+
await cleanOutputDir();
25+
let watcher = await watch(config);
26+
27+
{
28+
const [err, stats] = await watcher.wait();
29+
expect(err).not.ok;
30+
expect(stats.compilation.errors).lengthOf(0);
31+
}
32+
33+
{
34+
fixture.update(text => text.replace('let a: string;', 'let a: number;'));
35+
36+
const [err, stats] = await watcher.wait();
37+
expect(err).not.ok;
38+
expect(stats.compilation.errors).lengthOf(1);
39+
}
40+
41+
watcher.close();
42+
});
43+
});

0 commit comments

Comments
 (0)