|
16 | 16 | import test from 'ava';
|
17 | 17 | import * as path from 'path';
|
18 | 18 |
|
19 |
| -import {getTSConfig} from '../src/util'; |
| 19 | +import {ConfigFile, getTSConfig} from '../src/util'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Creates a fake promisified readFile function from a map |
| 23 | + * @param myMap contains a filepath as the key and a ConfigFile object as the |
| 24 | + * value. |
| 25 | + * The returned function has the same interface as fs.readFile |
| 26 | + */ |
| 27 | +function createFakeReadFilep(myMap: Map<string, ConfigFile>) { |
| 28 | + return (configPath: string) => { |
| 29 | + const configFile = myMap.get(configPath); |
| 30 | + if (configFile) { |
| 31 | + return Promise.resolve(JSON.stringify(configFile)); |
| 32 | + } else { |
| 33 | + return Promise.reject(`${configPath} Not Found`); |
| 34 | + } |
| 35 | + }; |
| 36 | +} |
20 | 37 |
|
21 | 38 | test('get should parse the correct tsconfig file', async t => {
|
22 | 39 | const FAKE_DIRECTORY = '/some/fake/directory';
|
23 |
| - const FAKE_CONFIG = {a: 'b'}; |
| 40 | + const FAKE_CONFIG1 = {files: ['b']}; |
| 41 | + |
24 | 42 | function fakeReadFilep(
|
25 | 43 | configPath: string, encoding: string): Promise<string> {
|
26 | 44 | t.is(configPath, path.join(FAKE_DIRECTORY, 'tsconfig.json'));
|
27 | 45 | t.is(encoding, 'utf8');
|
28 |
| - return Promise.resolve(JSON.stringify(FAKE_CONFIG)); |
| 46 | + return Promise.resolve(JSON.stringify(FAKE_CONFIG1)); |
29 | 47 | }
|
30 | 48 | const contents = await getTSConfig(FAKE_DIRECTORY, fakeReadFilep);
|
31 |
| - t.deepEqual(contents, FAKE_CONFIG); |
| 49 | + t.deepEqual(contents, FAKE_CONFIG1); |
32 | 50 | });
|
33 | 51 |
|
| 52 | +test('should throw an error if it finds a circular reference', async t => { |
| 53 | + const FAKE_DIRECTORY = '/some/fake/directory'; |
| 54 | + const FAKE_CONFIG1 = {files: ['b'], extends: 'FAKE_CONFIG2'}; |
| 55 | + const FAKE_CONFIG2 = {extends: 'FAKE_CONFIG3'}; |
| 56 | + const FAKE_CONFIG3 = {extends: 'tsconfig.json'}; |
| 57 | + const myMap = new Map(); |
| 58 | + myMap.set('/some/fake/directory/tsconfig.json', FAKE_CONFIG1); |
| 59 | + myMap.set('/some/fake/directory/FAKE_CONFIG2', FAKE_CONFIG2); |
| 60 | + myMap.set('/some/fake/directory/FAKE_CONFIG3', FAKE_CONFIG3); |
| 61 | + |
| 62 | + |
| 63 | + await t.throws( |
| 64 | + getTSConfig(FAKE_DIRECTORY, createFakeReadFilep(myMap)), Error, |
| 65 | + 'Circular Reference Detected'); |
| 66 | +}); |
| 67 | + |
| 68 | +test('should follow dependency chain caused by extends files', async t => { |
| 69 | + const FAKE_DIRECTORY = '/some/fake/directory'; |
| 70 | + const FAKE_CONFIG1 = { |
| 71 | + compilerOptions: {a: 'n'}, |
| 72 | + files: ['b'], |
| 73 | + extends: 'FAKE_CONFIG2' |
| 74 | + }; |
| 75 | + const FAKE_CONFIG2 = {include: ['/stuff/*'], extends: 'FAKE_CONFIG3'}; |
| 76 | + const FAKE_CONFIG3 = {exclude: ['doesnt/look/like/anything/to/me']}; |
| 77 | + const combinedConfig = { |
| 78 | + compilerOptions: {a: 'n'}, |
| 79 | + files: ['b'], |
| 80 | + include: ['/stuff/*'], |
| 81 | + exclude: ['doesnt/look/like/anything/to/me'] |
| 82 | + }; |
| 83 | + |
| 84 | + const myMap = new Map(); |
| 85 | + myMap.set('/some/fake/directory/tsconfig.json', FAKE_CONFIG1); |
| 86 | + myMap.set('/some/fake/directory/FAKE_CONFIG2', FAKE_CONFIG2); |
| 87 | + myMap.set('/some/fake/directory/FAKE_CONFIG3', FAKE_CONFIG3); |
| 88 | + |
| 89 | + const contents = |
| 90 | + await getTSConfig(FAKE_DIRECTORY, createFakeReadFilep(myMap)); |
| 91 | + t.deepEqual(contents, combinedConfig); |
| 92 | +}); |
| 93 | + |
| 94 | +test( |
| 95 | + 'when a file contains an extends field, the base file is loaded first then overridden by the inherited files', |
| 96 | + async t => { |
| 97 | + const FAKE_DIRECTORY = '/some/fake/directory'; |
| 98 | + const FAKE_CONFIG1 = {files: ['b'], extends: 'FAKE_CONFIG2'}; |
| 99 | + const FAKE_CONFIG2 = {files: ['c'], extends: 'FAKE_CONFIG3'}; |
| 100 | + const FAKE_CONFIG3 = {files: ['d']}; |
| 101 | + const combinedConfig = {compilerOptions: {}, files: ['b']}; |
| 102 | + const myMap = new Map(); |
| 103 | + myMap.set('/some/fake/directory/tsconfig.json', FAKE_CONFIG1); |
| 104 | + myMap.set('/some/fake/directory/FAKE_CONFIG2', FAKE_CONFIG2); |
| 105 | + myMap.set('/some/fake/directory/FAKE_CONFIG3', FAKE_CONFIG3); |
| 106 | + |
| 107 | + const contents = |
| 108 | + await getTSConfig(FAKE_DIRECTORY, createFakeReadFilep(myMap)); |
| 109 | + t.deepEqual(contents, combinedConfig); |
| 110 | + }); |
| 111 | + |
| 112 | +test( |
| 113 | + 'when reading a file, all filepaths should be relative to the config file currently being read', |
| 114 | + async t => { |
| 115 | + const FAKE_DIRECTORY = '/some/fake/directory'; |
| 116 | + const FAKE_CONFIG1 = {files: ['b'], extends: './foo/FAKE_CONFIG2'}; |
| 117 | + const FAKE_CONFIG2 = {include: ['c'], extends: './bar/FAKE_CONFIG3'}; |
| 118 | + const FAKE_CONFIG3 = {exclude: ['d']}; |
| 119 | + const combinedConfig = |
| 120 | + {compilerOptions: {}, exclude: ['d'], files: ['b'], include: ['c']}; |
| 121 | + const myMap = new Map(); |
| 122 | + myMap.set('/some/fake/directory/tsconfig.json', FAKE_CONFIG1); |
| 123 | + myMap.set('/some/fake/directory/foo/FAKE_CONFIG2', FAKE_CONFIG2); |
| 124 | + myMap.set('/some/fake/directory/foo/bar/FAKE_CONFIG3', FAKE_CONFIG3); |
| 125 | + |
| 126 | + const contents = |
| 127 | + await getTSConfig(FAKE_DIRECTORY, createFakeReadFilep(myMap)); |
| 128 | + t.deepEqual(contents, combinedConfig); |
| 129 | + }); |
| 130 | + |
| 131 | +test( |
| 132 | + 'function throws an error when reading a file that does not exist', |
| 133 | + async t => { |
| 134 | + const FAKE_DIRECTORY = '/some/fake/directory'; |
| 135 | + const myMap = new Map(); |
| 136 | + |
| 137 | + await t.throws( |
| 138 | + getTSConfig(FAKE_DIRECTORY, createFakeReadFilep(myMap)), Error, |
| 139 | + `${FAKE_DIRECTORY}/tsconfig.json Not Found`); |
| 140 | + }); |
| 141 | + |
| 142 | + |
34 | 143 | // TODO: test errors in readFile, JSON.parse.
|
0 commit comments