Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ff5e94f

Browse filesBrowse files
committed
refactor tests to use memory-fs
1 parent 64e0401 commit ff5e94f
Copy full SHA for ff5e94f

File tree

Expand file treeCollapse file tree

2 files changed

+73
-83
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+73
-83
lines changed

‎package.json

Copy file name to clipboardExpand all lines: package.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"inject-loader": "^2.0.1",
6161
"jade": "^1.11.0",
6262
"jsdom": "^9.2.1",
63+
"memory-fs": "^0.3.0",
6364
"mkdirp": "^0.5.1",
6465
"mocha": "^2.2.5",
6566
"node-libs-browser": "^1.0.0",

‎test/test.js

Copy file name to clipboardExpand all lines: test/test.js
+72-83Lines changed: 72 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
process.env.VUE_LOADER_TEST = true
22

3-
var fs = require('fs')
43
var path = require('path')
54
var webpack = require('webpack')
5+
var MemoryFS = require('memory-fs')
66
var jsdom = require('jsdom')
77
var expect = require('chai').expect
88
var rimraf = require('rimraf')
@@ -11,66 +11,61 @@ var SourceMapConsumer = require('source-map').SourceMapConsumer
1111
var ExtractTextPlugin = require("extract-text-webpack-plugin")
1212
var compiler = require('vue-template-compiler')
1313

14-
function assertRenderFn (options, template) {
15-
var compiled = compiler.compile(template)
16-
expect(options.render.toString()).to.equal('function (){' + compiled.render + '}')
14+
var loaderPath = 'expose?vueModule!' + path.resolve(__dirname, '../')
15+
var mfs = new MemoryFS()
16+
var globalConfig = {
17+
output: {
18+
path: '/',
19+
filename: 'test.build.js'
20+
},
21+
module: {
22+
loaders: [
23+
{
24+
test: /\.vue$/,
25+
loader: loaderPath
26+
}
27+
]
28+
}
1729
}
1830

19-
describe('vue-loader', function () {
20-
var testHTML = '<!DOCTYPE html><html><head></head><body></body></html>'
21-
var outputDir = path.resolve(__dirname, './output')
22-
var loaderPath = 'expose?vueModule!' + path.resolve(__dirname, '../')
23-
var globalConfig = {
24-
output: {
25-
path: outputDir,
26-
filename: 'test.build.js'
27-
},
28-
module: {
29-
loaders: [
30-
{
31-
test: /\.vue$/,
32-
loader: loaderPath
33-
}
34-
]
31+
function bundle (options, cb) {
32+
var config = Object.assign({}, globalConfig, options)
33+
var webpackCompiler = webpack(config)
34+
webpackCompiler.outputFileSystem = mfs
35+
webpackCompiler.run(function (err, stats) {
36+
expect(err).to.be.null
37+
if (stats.compilation.errors.length) {
38+
stats.compilation.errors.forEach(function (err) {
39+
console.error(err.message)
40+
})
3541
}
36-
}
37-
38-
beforeEach(function (done) {
39-
rimraf(outputDir, done)
42+
expect(stats.compilation.errors).to.be.empty
43+
cb(mfs.readFileSync('/test.build.js').toString())
4044
})
45+
}
4146

42-
function getFile (file, cb) {
43-
fs.readFile(path.resolve(outputDir, file), 'utf-8', function (err, data) {
44-
expect(err).to.be.not.exist
45-
cb(data)
46-
})
47-
}
48-
49-
function test (options, assert) {
50-
var config = Object.assign({}, globalConfig, options)
51-
webpack(config, function (err, stats) {
52-
if (stats.compilation.errors.length) {
53-
stats.compilation.errors.forEach(function (err) {
54-
console.error(err.message)
55-
})
47+
function test (options, assert) {
48+
bundle(options, function (code) {
49+
jsdom.env({
50+
html: '<!DOCTYPE html><html><head></head><body></body></html>',
51+
src: [code],
52+
done: function (err, window) {
53+
if (err) {
54+
console.log(err[0].data.error.stack)
55+
expect(err).to.be.null
56+
}
57+
assert(window)
5658
}
57-
expect(stats.compilation.errors).to.be.empty
58-
getFile('test.build.js', function (data) {
59-
jsdom.env({
60-
html: testHTML,
61-
src: [data],
62-
done: function (err, window) {
63-
if (err) {
64-
console.log(err[0].data.error.stack)
65-
expect(err).to.be.null
66-
}
67-
assert(window)
68-
}
69-
})
70-
})
7159
})
72-
}
60+
})
61+
}
62+
63+
function assertRenderFn (options, template) {
64+
var compiled = compiler.compile(template)
65+
expect(options.render.toString()).to.equal('function (){' + compiled.render + '}')
66+
}
7367

68+
describe('vue-loader', function () {
7469
it('basic', function (done) {
7570
test({
7671
entry: './test/fixtures/basic.vue'
@@ -164,30 +159,26 @@ describe('vue-loader', function () {
164159
entry: './test/fixtures/basic.vue',
165160
devtool: 'source-map'
166161
})
167-
webpack(config, function (err) {
168-
expect(err).to.be.null
169-
getFile('test.build.js.map', function (map) {
170-
var smc = new SourceMapConsumer(JSON.parse(map))
171-
getFile('test.build.js', function (code) {
172-
var line
173-
var col
174-
var targetRE = /^\s+msg: 'Hello from Component A!'/
175-
code.split(/\r?\n/g).some(function (l, i) {
176-
if (targetRE.test(l)) {
177-
line = i + 1
178-
col = l.length
179-
return true
180-
}
181-
})
182-
var pos = smc.originalPositionFor({
183-
line: line,
184-
column: col
185-
})
186-
expect(pos.source.indexOf('basic.vue') > -1)
187-
expect(pos.line).to.equal(9)
188-
done()
189-
})
162+
bundle(config, function (code) {
163+
var map = mfs.readFileSync('/test.build.js.map').toString()
164+
var smc = new SourceMapConsumer(JSON.parse(map))
165+
var line
166+
var col
167+
var targetRE = /^\s+msg: 'Hello from Component A!'/
168+
code.split(/\r?\n/g).some(function (l, i) {
169+
if (targetRE.test(l)) {
170+
line = i + 1
171+
col = l.length
172+
return true
173+
}
190174
})
175+
var pos = smc.originalPositionFor({
176+
line: line,
177+
column: col
178+
})
179+
expect(pos.source.indexOf('basic.vue') > -1)
180+
expect(pos.line).to.equal(9)
181+
done()
191182
})
192183
})
193184

@@ -203,7 +194,7 @@ describe('vue-loader', function () {
203194
})
204195

205196
it('extract CSS', function (done) {
206-
webpack(Object.assign({}, globalConfig, {
197+
bundle(Object.assign({}, globalConfig, {
207198
entry: './test/fixtures/extract-css.vue',
208199
vue: {
209200
loaders: {
@@ -214,12 +205,10 @@ describe('vue-loader', function () {
214205
plugins: [
215206
new ExtractTextPlugin('test.output.css')
216207
]
217-
}), function (err) {
218-
expect(err).to.be.null
219-
getFile('test.output.css', function (data) {
220-
expect(data).to.contain('h1 {\n color: #f00;\n}\n\n\n\n\n\n\nh2 {\n color: green;\n}')
221-
done()
222-
})
208+
}), function () {
209+
var css = mfs.readFileSync('/test.output.css').toString()
210+
expect(css).to.contain('h1 {\n color: #f00;\n}\n\n\n\n\n\n\nh2 {\n color: green;\n}')
211+
done()
223212
})
224213
})
225214

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.