UNPKG

2.79 kBJavaScriptView Raw
1const assert = require('assert')
2const { applyExtends } = require('./../../lib/config/config-file')
3
4describe('applyExtends', () => {
5 it('should return the same config if the extends property does not exist', () => {
6 const initialConfig = {
7 rules: {
8 rule0: 'error'
9 }
10 }
11 const result = applyExtends(initialConfig)
12
13 assert.deepStrictEqual(result, initialConfig)
14 })
15
16 it('should use the given config if the extends array is empty', () => {
17 const initialConfig = {
18 extends: [],
19 rules: {
20 rule0: 'error'
21 }
22 }
23 const result = applyExtends(initialConfig)
24
25 assert.deepStrictEqual(result, initialConfig)
26 })
27
28 it('should add the rules in the config', () => {
29 const initialConfig = {
30 extends: ['config1'],
31 rules: {
32 rule0: 'error'
33 }
34 }
35 const config1 = {
36 rules: {
37 rule1: 'warning'
38 }
39 }
40 const result = applyExtends(initialConfig, configName => ({ config1 }[configName]))
41
42 assert.deepStrictEqual(result, {
43 extends: ['config1'],
44 rules: {
45 rule0: 'error',
46 rule1: 'warning'
47 }
48 })
49 })
50
51 it('should accept a string as the value of extends', () => {
52 const initialConfig = {
53 extends: 'config1',
54 rules: {
55 rule0: 'error'
56 }
57 }
58 const config1 = {
59 rules: {
60 rule1: 'warning'
61 }
62 }
63 const result = applyExtends(initialConfig, configName => ({ config1 }[configName]))
64
65 assert.deepStrictEqual(result, {
66 extends: ['config1'],
67 rules: {
68 rule0: 'error',
69 rule1: 'warning'
70 }
71 })
72 })
73
74 it('should give higher priority to later configs', () => {
75 const initialConfig = {
76 extends: ['config1', 'config2'],
77 rules: {
78 rule0: 'error'
79 }
80 }
81 const config1 = {
82 rules: {
83 rule1: 'warning'
84 }
85 }
86 const config2 = {
87 rules: {
88 rule1: 'off'
89 }
90 }
91 const result = applyExtends(initialConfig, configName => ({ config1, config2 }[configName]))
92
93 assert.deepStrictEqual(result, {
94 extends: ['config1', 'config2'],
95 rules: {
96 rule0: 'error',
97 rule1: 'off'
98 }
99 })
100 })
101
102 it('should give higher priority to rules in the config file', () => {
103 const initialConfig = {
104 extends: ['config1', 'config2'],
105 rules: {
106 rule0: 'error'
107 }
108 }
109 const config1 = {
110 rules: {
111 rule0: 'warning'
112 }
113 }
114 const config2 = {
115 rules: {
116 rule0: 'off'
117 }
118 }
119 const result = applyExtends(initialConfig, configName => ({ config1, config2 }[configName]))
120
121 assert.deepStrictEqual(result, {
122 extends: ['config1', 'config2'],
123 rules: {
124 rule0: 'error'
125 }
126 })
127 })
128})