-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.test.js
145 lines (123 loc) · 4.27 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
import path from 'path'
import sinon from 'sinon'
import configs from '../test/passing-configs'
import failingConfigs from '../test/failing-configs'
import { validateRoot as validate, Joi } from './'
const validatecjs = require('./')
describe('.', () => {
let sandbox
let processExitStub
let consoleInfoStub
let consoleErrorStub
beforeEach(() => {
sandbox = sinon.sandbox.create()
consoleInfoStub = sandbox.stub(console, 'info')
consoleErrorStub = sandbox.stub(console, 'error')
processExitStub = sandbox.stub(process, 'exit')
})
afterEach(() => {
sandbox.restore()
})
configs.forEach(({ config, name }) => {
it(`validates ${name}`, () => {
validate(config)
// The success message should have been printed
assert(consoleInfoStub.callCount === 1)
// The error message should not have been printed
if (consoleErrorStub.callCount !== 0) {
throw new Error(consoleErrorStub.args[0])
}
// process.exit should not have been called
assert(processExitStub.callCount === 0)
})
})
configs.forEach(({ config, name }) => {
// This is not the multi-compiler, so we explictly pull that configuration out
if (name === 'webpack-multi-compiler') return
it(`validates ${name} using CJS`, () => {
validatecjs(config)
// The success message should have been printed
assert(consoleInfoStub.callCount === 0)
// The error message should not have been printed
if (consoleErrorStub.callCount !== 0) {
throw new Error(consoleErrorStub.args[0])
}
// process.exit should not have been called
assert(processExitStub.callCount === 0)
})
})
failingConfigs.forEach(({ config, name }) => {
it(`throws for ${name}`, () => {
validate(config)
// The error message should have been printed
assert(consoleErrorStub.callCount === 1)
// process.exit should have been called
assert(processExitStub.callCount === 1)
})
})
describe('version-validation', () => {
const cwd = process.cwd()
afterEach(() => {
process.chdir(cwd)
})
it('throws when the project is using webpack 2', () => {
const dir = path.resolve('./test/version-validation/fail')
process.chdir(dir)
try {
validate({ entry: './here.js', output: { filename: 'bundle.js' } })
throw new Error(`validate should throw when cwd is: ${dir}`)
} catch (error) {
if (error.message.indexOf('version 2') === -1) {
throw error
}
}
})
it('does not throw when the project is using webpack 1', () => {
const dir = path.resolve('./test/version-validation/pass')
process.chdir(dir)
// validate should not throw an error...
validate({ entry: './here.js', output: { filename: 'bundle.js' } })
})
})
it('should allow console output to be muted', () => {
validate({}, { quiet: true })
// The success message should not have been printed
assert(consoleInfoStub.callCount === 0)
// The error message should not have been printed
if (consoleErrorStub.callCount !== 0) {
throw new Error(consoleErrorStub.args[0])
}
// process.exit should not have been called
assert(processExitStub.callCount === 0)
})
const fooSchema = Joi.object({ foo: Joi.string() })
it('should allow the schema to be extended', () => {
const result1 = validate({ foo: 'bar' }, { returnValidation: true })
const result2 = validate({ foo: 'bar' }, {
returnValidation: true,
schemaExtension: fooSchema,
})
assert(result1.error)
assert(!result2.error)
})
it('should allow the schema to be overridden', () => {
const result = validate({ foo: 'bar' }, {
schema: fooSchema,
returnValidation: true,
})
assert(!result.error)
})
it('should allow overriding rules', () => {
const result = validate({ foo: 'bar' }, {
rules: {
foo: true,
'no-root-files-node-modules-nameclash': false,
},
returnValidation: true,
})
assert(result.schemaOptions.rules.foo)
assert(result.schemaOptions.rules['no-root-files-node-modules-nameclash'] === false)
// Will be merged with default rules, so length def greater then 1
assert(Object.keys(result.schemaOptions.rules).length > 1)
})
})