/
index.test.js
87 lines (73 loc) · 2.24 KB
/
index.test.js
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
/* eslint-env jest */
import { join } from 'path'
import cheerio from 'cheerio'
import * as path from 'path'
import {
renderViaHTTP,
findPort,
launchApp,
killApp,
File,
} from 'next-test-utils'
import * as JSON5 from 'json5'
const appDir = join(__dirname, '..')
let appPort
let app
async function get$(path, query) {
const html = await renderViaHTTP(appPort, path, query)
return cheerio.load(html)
}
function runTests() {
describe('default behavior', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {})
})
afterAll(() => killApp(app))
it('should alias components', async () => {
const $ = await get$('/basic-alias')
expect($('body').text()).toMatch(/World/)
})
it('should resolve the first item in the array first', async () => {
const $ = await get$('/resolve-order')
expect($('body').text()).toMatch(/Hello from a/)
})
it('should resolve the second item in as a fallback', async () => {
const $ = await get$('/resolve-fallback')
expect($('body').text()).toMatch(/Hello from only b/)
})
it('should resolve a single matching alias', async () => {
const $ = await get$('/single-alias')
expect($('body').text()).toMatch(/Hello/)
})
it('should not resolve to .d.ts files', async () => {
const $ = await get$('/alias-to-d-ts')
expect($('body').text()).toMatch(/Not aliased to d\.ts file/)
})
})
}
describe('typescript paths', () => {
runTests()
})
const tsconfig = new File(path.resolve(__dirname, '../tsconfig.json'))
describe('typescript paths without baseurl', () => {
beforeAll(async () => {
const tsconfigContent = JSON5.parse(tsconfig.originalContent)
delete tsconfigContent.compilerOptions.baseUrl
tsconfigContent.compilerOptions.paths = {
'isomorphic-unfetch': ['./types/unfetch.d.ts'],
'@c/*': ['./components/*'],
'@lib/*': ['./lib/a/*', './lib/b/*'],
'@mycomponent': ['./components/hello.tsx'],
'd-ts-alias': [
'./components/alias-to-d-ts.d.ts',
'./components/alias-to-d-ts.tsx',
],
}
tsconfig.write(JSON.stringify(tsconfigContent, null, 2))
})
afterAll(() => {
tsconfig.restore()
})
runTests()
})