/
path-mapping.spec.ts
132 lines (117 loc) · 3.57 KB
/
path-mapping.spec.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import path from 'path'
import ts from 'typescript'
import { createConfigSet, makeCompiler } from '../__helpers__/fakers'
import { TsCompiler } from '../legacy/compiler'
import { normalizeSlashes } from '../utils/normalize-slashes'
import { factory as pathMapping, name, version } from './path-mapping'
const TS_JS_CODE_WITH_PATH_ALIAS = `
import { parse } from '@utils/json'
import hoo from '@utils/json'
import * as json from '@utils/json'
export * as jsonUtils from '@utils/json'
const { stringify } = require('@utils/json')
import foo = require('@utils/json')
import('@utils/json').then(module => {
module.parse('{foo:1}')
})
import type { Foo} from '@utils/json'
stringify({ foo: 1 })
parse('{foo:1}')
console.log(json)
console.log(foo)
const bar: Foo = { foo: 2 }
hoo.foo(1)
`
const printer = ts.createPrinter()
describe('path-mapping', () => {
test('should have correct transformer name and version', () => {
expect(name).toBe('path-mapping')
expect(version).toBe(2)
})
test.each([
{
baseUrl: '.',
paths: {
'@utils/*': ['src/utils/*'],
},
},
{
rootDirs: ['./', 'foo'],
baseUrl: '.',
paths: {
'@utils/*': ['src/utils/*'],
},
},
])(
'should replace alias path with relative path which is resolved from paths tsconfig with js/ts extension',
(tsconfig) => {
const configSet = createConfigSet({
tsJestConfig: {
tsconfig,
},
})
const createFactory = () => pathMapping(new TsCompiler(configSet, new Map()))
const transpile = (source: string) => ts.transpileModule(source, { transformers: { before: [createFactory()] } })
jest.spyOn(ts, 'resolveModuleName').mockReturnValue({
resolvedModule: {
resolvedFileName: require.resolve('../utils/json'),
extension: 'ts',
} as any, // eslint-disable-line @typescript-eslint/no-explicit-any
})
const out = transpile(TS_JS_CODE_WITH_PATH_ALIAS)
expect(normalizeSlashes(out.outputText).replace(/\/\//g, '/')).toMatchSnapshot()
jest.resetAllMocks()
},
)
test.each([
{
code: `
import styles from '@utils/app.css'
console.log(styles)
`,
extension: 'css',
},
{
code: `
import jsonData from '@utils/data.json'
console.log(jsonData)
`,
extension: 'json',
},
{
code: `
import vueComponent from '@utils/component.vue'
console.log(vueComponent)
`,
extension: 'vue',
},
])(
'should replace alias path with relative path which is resolved from paths tsconfig with custom extensions',
({ code, extension }) => {
const resolvedFileNameStub = path.join('..', `utils/json.${extension}`)
jest.spyOn(ts, 'resolveModuleName').mockReturnValue({
resolvedModule: {
resolvedFileName: resolvedFileNameStub,
extension,
} as any, // eslint-disable-line @typescript-eslint/no-explicit-any
})
const sourceFile = ts.createSourceFile(__filename, code, ts.ScriptTarget.ES2015)
const result = ts.transform(sourceFile, [
pathMapping(
makeCompiler({
tsJestConfig: {
tsconfig: {
baseUrl: '.',
paths: {
'@utils/*': ['src/utils/*'],
},
},
},
}),
),
])
const transformedSourceFile = result.transformed[0]
expect(printer.printFile(transformedSourceFile).replace(/\\\\/g, '/')).toMatchSnapshot()
},
)
})