11import { expect } from 'chai' ;
22import semver from 'semver' ;
3+ import sinon from 'sinon' ;
34import eslintPkg from 'eslint/package.json' ;
5+ import * as tsConfigLoader from 'tsconfig-paths/lib/tsconfig-loader' ;
46import ExportMap from '../../../src/ExportMap' ;
57
68import * as fs from 'fs' ;
@@ -51,7 +53,8 @@ describe('ExportMap', function () {
5153 const differentSettings = Object . assign (
5254 { } ,
5355 fakeContext ,
54- { parserPath : 'espree' } ) ;
56+ { parserPath : 'espree' } ,
57+ ) ;
5558
5659 expect ( ExportMap . get ( './named-exports' , differentSettings ) )
5760 . to . exist . and
@@ -338,11 +341,11 @@ describe('ExportMap', function () {
338341 // ['string form', { 'typescript-eslint-parser': '.ts' }],
339342 ] ;
340343
341- if ( semver . satisfies ( eslintPkg . version , '>5.0.0 ' ) ) {
344+ if ( semver . satisfies ( eslintPkg . version , '>5' ) ) {
342345 configs . push ( [ 'array form' , { '@typescript-eslint/parser' : [ '.ts' , '.tsx' ] } ] ) ;
343346 }
344347
345- if ( semver . satisfies ( eslintPkg . version , '<6.0.0 ' ) ) {
348+ if ( semver . satisfies ( eslintPkg . version , '<6' ) ) {
346349 configs . push ( [ 'array form' , { 'typescript-eslint-parser' : [ '.ts' , '.tsx' ] } ] ) ;
347350 }
348351
@@ -358,8 +361,12 @@ describe('ExportMap', function () {
358361 let imports ;
359362 before ( 'load imports' , function ( ) {
360363 this . timeout ( 20000 ) ; // takes a long time :shrug:
364+ sinon . spy ( tsConfigLoader , 'tsConfigLoader' ) ;
361365 imports = ExportMap . get ( './typescript.ts' , context ) ;
362366 } ) ;
367+ after ( 'clear spies' , function ( ) {
368+ tsConfigLoader . tsConfigLoader . restore ( ) ;
369+ } ) ;
363370
364371 it ( 'returns something for a TypeScript file' , function ( ) {
365372 expect ( imports ) . to . exist ;
@@ -388,9 +395,38 @@ describe('ExportMap', function () {
388395 it ( 'has exported abstract class' , function ( ) {
389396 expect ( imports . has ( 'Bar' ) ) . to . be . true ;
390397 } ) ;
398+
399+ it ( 'should cache tsconfig until tsconfigRootDir parser option changes' , function ( ) {
400+ const customContext = Object . assign (
401+ { } ,
402+ context ,
403+ {
404+ parserOptions : {
405+ tsconfigRootDir : null ,
406+ } ,
407+ } ,
408+ ) ;
409+ expect ( tsConfigLoader . tsConfigLoader . callCount ) . to . equal ( 0 ) ;
410+ ExportMap . parse ( './baz.ts' , 'export const baz = 5' , customContext ) ;
411+ expect ( tsConfigLoader . tsConfigLoader . callCount ) . to . equal ( 1 ) ;
412+ ExportMap . parse ( './baz.ts' , 'export const baz = 5' , customContext ) ;
413+ expect ( tsConfigLoader . tsConfigLoader . callCount ) . to . equal ( 1 ) ;
414+
415+ const differentContext = Object . assign (
416+ { } ,
417+ context ,
418+ {
419+ parserOptions : {
420+ tsconfigRootDir : process . cwd ( ) ,
421+ } ,
422+ } ,
423+ ) ;
424+
425+ ExportMap . parse ( './baz.ts' , 'export const baz = 5' , differentContext ) ;
426+ expect ( tsConfigLoader . tsConfigLoader . callCount ) . to . equal ( 2 ) ;
427+ } ) ;
391428 } ) ;
392429 } ) ;
393-
394430 } ) ;
395431
396432 // todo: move to utils
0 commit comments