1
+ import * as ts from 'typescript' ;
1
2
import * as assert from 'assert' ;
2
3
import * as lib from './index' ;
4
+ import mockFs = require( 'mock-fs' ) ;
3
5
4
6
const root = process . cwd ( ) ;
5
7
@@ -28,13 +30,13 @@ describe('tsconfig-files', () => {
28
30
29
31
it ( 'get source file which are not in files' , ( ) => {
30
32
const testFile = `${ root } /test-project/file.spec.ts` ;
31
- const sourceFile = service . getSourceFile ( testFile ) ;
33
+ const sourceFile = service . getSourceFile ( testFile , undefined ) ;
32
34
assert ( sourceFile ) ;
33
35
} ) ;
34
36
35
37
it ( 'typescript checker (file which is not defined in tsconfig)' , ( ) => {
36
38
const testFile = `${ root } /test-project/file.spec.ts` ;
37
- const sourceFile = service . getSourceFile ( testFile ) ;
39
+ const sourceFile = service . getSourceFile ( testFile , undefined ) ;
38
40
const checker = service . getProgram ( ) . getTypeChecker ( ) ;
39
41
const [ itstmt ] = sourceFile . statements . filter ( x => x . getText ( ) === `it('example test');` ) ;
40
42
const itid = ( itstmt as any ) . expression . expression ;
@@ -58,7 +60,7 @@ describe('create service', () => {
58
60
const testFile = `${ root } /test-project/errors.ts` ;
59
61
const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
60
62
assert ( sourceFile ) ;
61
- const diagnostics = service . getDiagnostics ( testFile ) ;
63
+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
62
64
assert . equal ( diagnostics . length , 2 ) ;
63
65
assert . equal ( diagnostics [ 0 ] . messageText , `Type '1' is not assignable to type 'string'.` ) ;
64
66
assert . equal ( diagnostics [ 1 ] . messageText , `Type '"foo"' is not assignable to type 'number'.` ) ;
@@ -68,48 +70,81 @@ describe('create service', () => {
68
70
const testFile = `${ root } /test-project/number.ts` ;
69
71
const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
70
72
assert ( sourceFile ) ;
71
- const diagnostics = service . getDiagnostics ( testFile ) ;
73
+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
72
74
assert . equal ( diagnostics . length , 0 ) ;
73
75
} ) ;
74
76
75
77
it ( 'built in' , ( ) => {
76
78
const testFile = `${ root } /test-project/builtin.ts` ;
77
79
const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
78
80
assert ( sourceFile ) ;
79
- const diagnostics = service . getDiagnostics ( testFile ) ;
81
+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
80
82
assert . equal ( diagnostics . length , 0 ) ;
81
83
} ) ;
82
84
83
85
it ( 'types' , ( ) => {
84
86
const testFile = `${ root } /test-project/types.ts` ;
85
87
const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
86
88
assert ( sourceFile ) ;
87
- const diagnostics = service . getDiagnostics ( testFile ) ;
89
+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
88
90
assert . equal ( diagnostics . length , 0 ) ;
89
91
} ) ;
90
92
91
93
it ( 'decorator' , ( ) => {
92
94
const testFile = `${ root } /test-project/decorator.ts` ;
93
95
const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
94
96
assert ( sourceFile ) ;
95
- const diagnostics = service . getDiagnostics ( testFile ) ;
97
+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
96
98
assert . equal ( diagnostics . length , 0 ) ;
97
99
} ) ;
98
100
99
101
it ( 'global types' , ( ) => {
100
102
const testFile = `${ root } /test-project/global-types.ts` ;
101
103
const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
102
104
assert ( sourceFile ) ;
103
- const diagnostics = service . getDiagnostics ( testFile ) ;
105
+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
104
106
assert . equal ( diagnostics . length , 0 ) ;
105
107
} ) ;
106
108
107
109
it ( 'date' , ( ) => {
108
110
const testFile = `${ root } /test-project/date.ts` ;
109
111
const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
110
112
assert ( sourceFile ) ;
111
- const diagnostics = service . getDiagnostics ( testFile ) ;
113
+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
112
114
assert . equal ( diagnostics . length , 0 ) ;
113
115
} ) ;
114
116
115
117
} ) ;
118
+
119
+ describe ( 'source file of changed file' , ( ) => {
120
+
121
+ const testFile = `${ root } /test-project/amok.ts` ;
122
+ const configFile = `${ root } /test-project/tsconfig-empty.json` ;
123
+ let sourceFile : ts . SourceFile ;
124
+
125
+ before ( ( ) => {
126
+ mockFs ( {
127
+ [ testFile ] : 'const x = 1' ,
128
+ [ configFile ] : '{}' ,
129
+ } ) ;
130
+ service = lib . createService ( { configFile } ) ;
131
+ sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
132
+ } ) ;
133
+
134
+ after ( ( ) => {
135
+ mockFs . restore ( ) ;
136
+ } ) ;
137
+
138
+ it ( 'smoke' , ( ) => {
139
+ assert ( service ) ;
140
+ assert ( sourceFile ) ;
141
+ } ) ;
142
+
143
+ it ( 'changes should be reflected' , ( ) => {
144
+ mockFs ( { [ testFile ] : 'let x = 2' } ) ;
145
+ sourceFile = service . getSourceFile ( testFile , 'let x = 2' ) ;
146
+ assert ( sourceFile ) ;
147
+ assert . equal ( sourceFile . getText ( ) , 'let x = 2' ) ;
148
+ } ) ;
149
+
150
+ } ) ;
0 commit comments