forked from browserslist/browserslist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.test.ts
129 lines (109 loc) · 3.52 KB
/
config.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { join } from 'path'
import browserslist from '../'
let RC = join(__dirname, 'fixtures', 'rc', 'test.css')
let FILE = join(__dirname, 'fixtures', 'dir', 'test.css')
let TYPO = join(__dirname, 'fixtures', 'typo', 'test.css')
let BOTH1 = join(__dirname, 'fixtures', 'both1', 'test.css')
let BOTH2 = join(__dirname, 'fixtures', 'both2', 'test.css')
let BOTH3 = join(__dirname, 'fixtures', 'both3', 'test.css')
let WRONG1 = join(__dirname, 'fixtures', 'wrong1', 'test.css')
let WRONG2 = join(__dirname, 'fixtures', 'wrong2', 'test.css')
let BROKEN = join(__dirname, 'fixtures', 'broken', 'test.css')
let STRING = join(__dirname, 'fixtures', 'string', 'test.css')
let PACKAGE = join(__dirname, 'fixtures', 'package', 'test.css')
let originCwd = process.cwd()
afterEach(() => {
process.chdir(originCwd)
})
it('parses queries', () => {
expect(browserslist.parseConfig('ie 10\n> 1%')).toEqual({
defaults: ['ie 10', '> 1%']
})
})
it('parses comma', () => {
expect(browserslist.parseConfig('ie 10, > 1%')).toEqual({
defaults: ['ie 10', '> 1%']
})
})
it('removes comments', () => {
let config = '# support list\nie 10#bad\n> 1%'
expect(browserslist.parseConfig(config)).toEqual({
defaults: ['ie 10', '> 1%']
})
})
it('supports sections', () => {
expect(browserslist.parseConfig('ie 10\n[test]\nie 11')).toEqual({
defaults: ['ie 10'],
test: ['ie 11']
})
})
it('throws on duplicate sections', () => {
let config = '[test]\nie 10\n[production test]\nie 11'
expect(() => {
browserslist.parseConfig(config)
}).toThrow(/Duplicate section test in Browserslist config/)
})
it('trims whitespaces', () => {
expect(browserslist.parseConfig('ie 9\n\n [ test] \n \n > 1%\n')).toEqual({
defaults: ['ie 9'],
test: ['> 1%']
})
})
it('returns undefined on no config', () => {
expect(browserslist.findConfig(__dirname)).not.toBeDefined()
})
it('reads config', () => {
expect(browserslist.findConfig(FILE)).toEqual({
defaults: ['ie 11', 'ie 10']
})
})
it('reads .browserslistrc config', () => {
expect(browserslist.findConfig(RC)).toEqual({
defaults: ['ie 11']
})
})
it('reads config from package.json', () => {
expect(browserslist.findConfig(PACKAGE)).toEqual({
defaults: ['ie 9', 'ie 10']
})
})
it('shows warning on broken package.json', () => {
jest.spyOn(console, 'warn').mockImplementation(() => true)
expect(browserslist.findConfig(BROKEN)).toEqual({
defaults: ['ie 11', 'ie 10']
})
expect(console.warn).toHaveBeenCalledTimes(1)
})
it('shows error on key typo', () => {
expect(() => {
browserslist.findConfig(TYPO)
}).toThrow(/browserlist/)
})
it('reads from dir wich contains both browserslist and package.json', () => {
expect(() => {
browserslist.findConfig(BOTH1)
}).toThrow(/contains both browserslist and package\.json/)
})
it('reads from dir wich contains both .browserslistrc and package.json', () => {
expect(() => {
browserslist.findConfig(BOTH2)
}).toThrow(/contains both .browserslistrc and package\.json/)
})
it('reads from dir wich contains both .browserslistrc and browserslist', () => {
expect(() => {
browserslist.findConfig(BOTH3)
}).toThrow(/contains both .browserslistrc and browserslist/)
})
it('checks config format', () => {
expect(() => {
browserslist.findConfig(WRONG1)
}).toThrow(/Browserslist config should/)
expect(() => {
browserslist.findConfig(WRONG2)
}).toThrow(/Browserslist config should/)
})
it('reads config with one string', () => {
expect(browserslist.findConfig(STRING)).toEqual({
defaults: 'ie 9, ie 8'
})
})