forked from FullHuman/purgecss-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack-integration.test.js
62 lines (57 loc) · 2.05 KB
/
webpack-integration.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
import fs from 'fs'
import path from 'path'
import webpack from 'webpack'
import ExtractPlugin from 'extract-text-webpack-plugin'
const cases = process.env.CASES
? process.env.CASES.split(',')
: fs.readdirSync(path.join(__dirname, 'cases'))
describe('Webpack Integration Tests', () => {
cases.forEach(testCase => {
it(testCase, done => {
let options = { entry: { test: './index.js' } }
const testDirectory = path.join(__dirname, 'cases', testCase)
const outputDirectory = path.join(__dirname, 'js', testCase)
const configFile = path.join(testDirectory, 'webpack.config.js')
if (fs.existsSync(configFile)) {
options = require(configFile)
}
options.context = testDirectory
if (!options.module) options.module = {}
if (!options.module.loaders) {
options.module.loaders = [
{ test: /\.txt$/, loader: ExtractPlugin.extract('raw-loader') }
]
}
if (!options.output) options.output = { filename: '[name].js' }
if (!options.output.path) options.output.path = outputDirectory
if (process.env.CASES) {
console.log(
`\nwebpack.${testCase}.config.js ${JSON.stringify(options, null, 2)}`
)
}
webpack(options, (err, stats) => {
if (err) return done(err)
if (stats.hasErrors()) return done(new Error(stats.toString()))
const testFile = path.join(outputDirectory, 'test.js')
if (fs.existsSync(testFile)) {
require(testFile)(suite)
}
const expectedDirectory = path.join(testDirectory, 'expected')
fs.readdirSync(expectedDirectory).forEach(file => {
const filePath = path.join(expectedDirectory, file)
const actualPath = path.join(outputDirectory, file)
expect(readFileOrEmpty(actualPath)).toEqual(readFileOrEmpty(filePath))
expect(readFileOrEmpty(actualPath)).toMatchSnapshot()
})
done()
})
})
})
})
function readFileOrEmpty(path) {
try {
return fs.readFileSync(path, 'utf-8')
} catch (e) {
return ''
}
}