1+ import * as ts from 'typescript' ;
12import * as assert from 'assert' ;
23import * as lib from './index' ;
4+ import mockFs = require( 'mock-fs' ) ;
35
46const root = process . cwd ( ) ;
57
@@ -28,13 +30,13 @@ describe('tsconfig-files', () => {
2830
2931 it ( 'get source file which are not in files' , ( ) => {
3032 const testFile = `${ root } /test-project/file.spec.ts` ;
31- const sourceFile = service . getSourceFile ( testFile ) ;
33+ const sourceFile = service . getSourceFile ( testFile , undefined ) ;
3234 assert ( sourceFile ) ;
3335 } ) ;
3436
3537 it ( 'typescript checker (file which is not defined in tsconfig)' , ( ) => {
3638 const testFile = `${ root } /test-project/file.spec.ts` ;
37- const sourceFile = service . getSourceFile ( testFile ) ;
39+ const sourceFile = service . getSourceFile ( testFile , undefined ) ;
3840 const checker = service . getProgram ( ) . getTypeChecker ( ) ;
3941 const [ itstmt ] = sourceFile . statements . filter ( x => x . getText ( ) === `it('example test');` ) ;
4042 const itid = ( itstmt as any ) . expression . expression ;
@@ -58,7 +60,7 @@ describe('create service', () => {
5860 const testFile = `${ root } /test-project/errors.ts` ;
5961 const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
6062 assert ( sourceFile ) ;
61- const diagnostics = service . getDiagnostics ( testFile ) ;
63+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
6264 assert . equal ( diagnostics . length , 2 ) ;
6365 assert . equal ( diagnostics [ 0 ] . messageText , `Type '1' is not assignable to type 'string'.` ) ;
6466 assert . equal ( diagnostics [ 1 ] . messageText , `Type '"foo"' is not assignable to type 'number'.` ) ;
@@ -68,48 +70,81 @@ describe('create service', () => {
6870 const testFile = `${ root } /test-project/number.ts` ;
6971 const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
7072 assert ( sourceFile ) ;
71- const diagnostics = service . getDiagnostics ( testFile ) ;
73+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
7274 assert . equal ( diagnostics . length , 0 ) ;
7375 } ) ;
7476
7577 it ( 'built in' , ( ) => {
7678 const testFile = `${ root } /test-project/builtin.ts` ;
7779 const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
7880 assert ( sourceFile ) ;
79- const diagnostics = service . getDiagnostics ( testFile ) ;
81+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
8082 assert . equal ( diagnostics . length , 0 ) ;
8183 } ) ;
8284
8385 it ( 'types' , ( ) => {
8486 const testFile = `${ root } /test-project/types.ts` ;
8587 const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
8688 assert ( sourceFile ) ;
87- const diagnostics = service . getDiagnostics ( testFile ) ;
89+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
8890 assert . equal ( diagnostics . length , 0 ) ;
8991 } ) ;
9092
9193 it ( 'decorator' , ( ) => {
9294 const testFile = `${ root } /test-project/decorator.ts` ;
9395 const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
9496 assert ( sourceFile ) ;
95- const diagnostics = service . getDiagnostics ( testFile ) ;
97+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
9698 assert . equal ( diagnostics . length , 0 ) ;
9799 } ) ;
98100
99101 it ( 'global types' , ( ) => {
100102 const testFile = `${ root } /test-project/global-types.ts` ;
101103 const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
102104 assert ( sourceFile ) ;
103- const diagnostics = service . getDiagnostics ( testFile ) ;
105+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
104106 assert . equal ( diagnostics . length , 0 ) ;
105107 } ) ;
106108
107109 it ( 'date' , ( ) => {
108110 const testFile = `${ root } /test-project/date.ts` ;
109111 const sourceFile = service . getProgram ( ) . getSourceFile ( testFile ) ;
110112 assert ( sourceFile ) ;
111- const diagnostics = service . getDiagnostics ( testFile ) ;
113+ const diagnostics = service . getDiagnostics ( testFile , undefined ) ;
112114 assert . equal ( diagnostics . length , 0 ) ;
113115 } ) ;
114116
115117} ) ;
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