@@ -22,17 +22,25 @@ const changing = require('../');
22
22
const fixtures = path . join ( __dirname , 'fixtures' ) ;
23
23
24
24
describe ( 'changing.test.js' , function ( ) {
25
- afterEach ( function ( ) {
25
+ function cleanup ( ) {
26
26
fs . writeFileSync ( path . join ( fixtures , 'foo.js' ) , 'bar\n' ) ;
27
- } ) ;
27
+ try {
28
+ fs . unlinkSync ( path . join ( fixtures , '.tmpfile' ) ) ;
29
+ } catch ( _ ) {
30
+ // ignore error
31
+ }
32
+ }
33
+
34
+ beforeEach ( cleanup ) ;
35
+ afterEach ( cleanup ) ;
28
36
29
37
it ( 'should create watch without options' , function ( ) {
30
38
var watcher = changing ( ) ;
31
39
watcher . close ( ) ;
32
40
} ) ;
33
41
34
42
it ( 'should watching fixtures/foo.js change' , function ( done ) {
35
- let watcher = changing ( { interval : '1s' } ) ;
43
+ let watcher = changing ( { interval : 500 } ) ;
36
44
watcher . add ( path . join ( fixtures , 'foo.js' ) ) ;
37
45
watcher . add ( path . join ( fixtures , 'foo.js' ) ) ;
38
46
watcher . on ( 'change' , function ( info ) {
@@ -47,20 +55,51 @@ describe('changing.test.js', function () {
47
55
48
56
setTimeout ( function ( ) {
49
57
fs . writeFileSync ( path . join ( fixtures , 'foo.js' ) , 'bar update\n' ) ;
50
- } , 1500 ) ;
58
+ } , 1000 ) ;
51
59
} ) ;
52
60
53
61
it ( 'should got stat-error when watch filepath not exists' , function ( done ) {
54
- let watcher = changing ( { interval : '1s ' } ) ;
62
+ let watcher = changing ( { interval : '100ms ' } ) ;
55
63
watcher . add ( path . join ( fixtures , 'foo.js-not-exists' ) ) ;
56
64
watcher . add ( path . join ( fixtures , 'foo.js-not-exists' ) ) ;
57
65
watcher . on ( 'change' , function ( ) {
58
66
throw new Error ( 'should not run this' ) ;
59
67
} ) ;
60
68
watcher . on ( 'stat-error' , function ( err ) {
61
69
assert . equal ( err . code , 'ENOENT' ) ;
62
- watcher . close ( ) ;
63
- done ( ) ;
70
+ // and emit once
71
+ setTimeout ( function ( ) {
72
+ watcher . close ( ) ;
73
+ done ( ) ;
74
+ } , 200 ) ;
75
+ } ) ;
76
+ } ) ;
77
+
78
+ it ( 'should watching fixtures/.tmpfile become exists' , function ( done ) {
79
+ let watcher = changing ( { interval : 100 } ) ;
80
+ watcher . add ( path . join ( fixtures , '.tmpfile' ) ) ;
81
+ watcher . on ( 'change' , function ( info ) {
82
+ assert . equal ( info . event , 'change' ) ;
83
+ assert . equal ( info . path , path . join ( fixtures , '.tmpfile' ) ) ;
84
+ assert ( info . stat ) ;
85
+
86
+ setTimeout ( function ( ) {
87
+ // remove it and emit stat-error again
88
+ fs . unlinkSync ( path . join ( fixtures , '.tmpfile' ) ) ;
89
+ watcher . on ( 'stat-error' , function ( err ) {
90
+ assert . equal ( err . code , 'ENOENT' ) ;
91
+
92
+ // and emit once
93
+ setTimeout ( function ( ) {
94
+ watcher . close ( ) ;
95
+ done ( ) ;
96
+ } , 200 ) ;
97
+ } ) ;
98
+ } , 200 ) ;
64
99
} ) ;
100
+
101
+ setTimeout ( function ( ) {
102
+ fs . writeFileSync ( path . join ( fixtures , '.tmpfile' ) , 'tmpfile update\n' ) ;
103
+ } , 200 ) ;
65
104
} ) ;
66
105
} ) ;
0 commit comments