-
Notifications
You must be signed in to change notification settings - Fork 449
/
path-mapping.spec.ts
128 lines (114 loc) · 3.38 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
import { join } from 'path'
import { testing } from 'bs-logger'
import ts from 'typescript'
import { createConfigSet } from '../__helpers__/fakers'
import { TsCompiler } from '../compiler/ts-compiler'
import { normalizeSlashes } from '../utils/normalize-slashes'
import * as pathMapping from './path-mapping'
const logger = testing.createLoggerMock()
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)
`
describe('path-mapping', () => {
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,
},
logger,
})
const createFactory = () => pathMapping.factory(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,
})
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 configSet = createConfigSet({
tsJestConfig: {
tsconfig: {
baseUrl: '.',
paths: {
'@utils/*': ['src/utils/*'],
},
},
},
logger,
})
const createFactory = () => pathMapping.factory(new TsCompiler(configSet, new Map()))
const transpile = (source: string) => ts.transpileModule(source, { transformers: { before: [createFactory()] } })
const resolvedFileNameStub = join('..', `utils/json.${extension}`)
jest.spyOn(ts, 'resolveModuleName').mockReturnValue({
resolvedModule: {
resolvedFileName: resolvedFileNameStub,
extension,
} as any,
})
const out = transpile(code)
expect(normalizeSlashes(out.outputText).replace(/\/\//g, '/')).toMatchSnapshot()
jest.resetAllMocks()
},
)
})