-
Notifications
You must be signed in to change notification settings - Fork 61
/
to-export.test.ts
113 lines (102 loc) · 3.61 KB
/
to-export.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import type { Import } from '../src/types'
import { resolve } from 'node:path'
import process from 'node:process'
import { describe, expect, it } from 'vitest'
import { toExports } from '../src/utils'
describe('toExports', () => {
it('basic', () => {
const imports: Import[] = [{ from: 'test-id', name: 'fooBar', as: 'fooBar' }]
expect(toExports(imports))
.toMatchInlineSnapshot('"export { fooBar } from \'test-id\';"')
})
it('alias', () => {
const imports: Import[] = [{ from: 'test-id', name: 'foo', as: 'bar' }]
expect(toExports(imports))
.toMatchInlineSnapshot('"export { foo as bar } from \'test-id\';"')
})
it('multiple', () => {
const imports: Import[] = [
{ from: 'test1', name: 'foo', as: 'foo' },
{ from: 'test1', name: 'bar', as: 'bar' },
{ from: 'test2', name: 'foobar', as: 'foobar' },
]
expect(toExports(imports))
.toMatchInlineSnapshot(`
"export { foo, bar } from 'test1';
export { foobar } from 'test2';"
`)
})
it('default', () => {
const imports: Import[] = [
{ from: 'test1', name: 'default', as: 'foo' },
]
expect(toExports(imports))
.toMatchInlineSnapshot('"export { default as foo } from \'test1\';"')
})
it('import all as', () => {
const imports: Import[] = [
{ from: 'test1', name: '*', as: 'foo' },
]
expect(toExports(imports))
.toMatchInlineSnapshot('"export * as foo from \'test1\';"')
})
it('mixed', () => {
const imports: Import[] = [
{ from: 'test1', name: '*', as: 'foo' },
{ from: 'test1', name: '*', as: 'bar' },
{ from: 'test1', name: 'foo', as: 'foo' },
{ from: 'test1', name: 'bar', as: 'bar' },
{ from: 'test2', name: 'foobar', as: 'foobar' },
{ from: 'test2', name: 'default', as: 'defaultAlias' },
]
expect(toExports(imports))
.toMatchInlineSnapshot(`
"export * as foo from 'test1';
export * as bar from 'test1';
export { foo, bar } from 'test1';
export { foobar, default as defaultAlias } from 'test2';"
`)
})
it('strip extensions', () => {
const imports: Import[] = [
{ from: 'test1.ts', name: 'foo', as: 'foo' },
{ from: 'test2.mjs', name: 'foobar', as: 'foobar' },
{ from: './test1.ts', name: 'foo', as: 'foo' },
{ from: './test2.mjs', name: 'foobar', as: 'foobar' },
{ from: 'test1.ts/test1.ts', name: 'foo', as: 'foo' },
]
expect(toExports(imports))
.toMatchInlineSnapshot(`
"export { foo } from 'test1.ts';
export { foobar } from 'test2.mjs';
export { foo } from './test1';
export { foobar } from './test2';
export { foo } from 'test1.ts/test1.ts';"
`)
})
it('relative path', () => {
const root = process.cwd()
const imports: Import[] = [
{ from: resolve(root, 'foo.ts'), name: 'foo1', as: 'foo1' },
{ from: resolve(root, '../foo.ts'), name: 'foo2', as: 'foo2' },
{ from: resolve(root, 'foo/bar.ts'), name: 'foo3', as: 'foo3' },
]
expect(toExports(imports, root))
.toMatchInlineSnapshot(`
"export { foo1 } from './foo';
export { foo2 } from '../foo';
export { foo3 } from './foo/bar';"
`)
})
it('include type', () => {
const root = process.cwd()
const imports: Import[] = [
{ from: 'vue', name: 'ref', as: 'ref' },
{ from: 'vue', name: 'Ref', as: 'Ref', type: true },
]
expect(toExports(imports, root))
.toMatchInlineSnapshot('"export { ref } from \'vue\';"')
expect(toExports(imports, root, true))
.toMatchInlineSnapshot('"export { ref, Ref } from \'vue\';"')
})
})