parse_imports.spec.ts (1740B)
1 export const description = ` 2 Test for "parseImports" utility. 3 `; 4 5 import { makeTestGroup } from '../common/framework/test_group.js'; 6 import { parseImports } from '../common/util/parse_imports.js'; 7 8 import { UnitTest } from './unit_test.js'; 9 10 class F extends UnitTest { 11 test(content: string, expect: string[]): void { 12 const got = parseImports('a/b/c.js', content); 13 const expectJoined = expect.join('\n'); 14 const gotJoined = got.join('\n'); 15 this.expect( 16 expectJoined === gotJoined, 17 ` 18 expected: ${expectJoined} 19 got: ${gotJoined}` 20 ); 21 } 22 } 23 24 export const g = makeTestGroup(F); 25 26 g.test('empty').fn(t => { 27 t.test(``, []); 28 t.test(`\n`, []); 29 t.test(`\n\n`, []); 30 }); 31 32 g.test('simple').fn(t => { 33 t.test(`import 'x/y/z.js';`, ['a/b/x/y/z.js']); 34 t.test(`import * as blah from 'x/y/z.js';`, ['a/b/x/y/z.js']); 35 t.test(`import { blah } from 'x/y/z.js';`, ['a/b/x/y/z.js']); 36 }); 37 38 g.test('multiple').fn(t => { 39 t.test( 40 ` 41 blah blah blah 42 import 'x/y/z.js'; 43 more blah 44 import * as blah from 'm/n/o.js'; 45 extra blah 46 import { blah } from '../h.js'; 47 ending with blah 48 `, 49 ['a/b/x/y/z.js', 'a/b/m/n/o.js', 'a/h.js'] 50 ); 51 }); 52 53 g.test('multiline').fn(t => { 54 t.test( 55 `import { 56 blah 57 } from 'x/y/z.js';`, 58 ['a/b/x/y/z.js'] 59 ); 60 t.test( 61 `import { 62 blahA, 63 blahB, 64 } from 'x/y/z.js';`, 65 ['a/b/x/y/z.js'] 66 ); 67 }); 68 69 g.test('file_characters').fn(t => { 70 t.test(`import '01234_56789.js';`, ['a/b/01234_56789.js']); 71 }); 72 73 g.test('relative_paths').fn(t => { 74 t.test(`import '../x.js';`, ['a/x.js']); 75 t.test(`import '../x/y.js';`, ['a/x/y.js']); 76 t.test(`import '../../x.js';`, ['x.js']); 77 t.test(`import '../../../x.js';`, ['../x.js']); 78 t.test(`import '../../../../x.js';`, ['../../x.js']); 79 });