Skip to content

Commit ce7802e

Browse files
committed
Added tests
1 parent c7ccb95 commit ce7802e

File tree

9 files changed

+118
-5
lines changed

9 files changed

+118
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"jest": "^21.0.0",
2121
"jsdoc-to-markdown": "^3.0.0",
2222
"memory-fs": "^0.4.0",
23+
"postcss-import": "^11.0.0",
2324
"postcss-js": "^1.0.0",
2425
"standard": "^10.0.0",
2526
"standard-version": "^4.0.0",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Loader Default 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
4+
5+
exports[`Loader Watching Deps After An Error Default 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
6+
7+
exports[`Loader Watching Deps After An Error Default 2`] = `"throw new Error(\\"Module build failed: Syntax Error \\\\n\\\\n(1:5) Unknown word\\\\n\\\\n\\\\u001b[31m\\\\u001b[1m>\\\\u001b[22m\\\\u001b[39m\\\\u001b[90m 1 | \\\\u001b[39ma \\\\u001b[33m{\\\\u001b[39m color black \\\\u001b[33m}\\\\u001b[39m\\\\n \\\\u001b[90m | \\\\u001b[39m \\\\u001b[31m\\\\u001b[1m^\\\\u001b[22m\\\\u001b[39m\\\\n \\\\u001b[90m 2 | \\\\u001b[39m\\\\n\\");"`;
8+
9+
exports[`Loader Watching Deps After An Error Default 3`] = `"module.exports = \\"a { color: black }\\\\n\\""`;

test/fixtures/css-watching/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import style from './style.css'
2+
3+
export default style
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a { color: black }

test/fixtures/css-watching/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "./styleDep";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a { color black }

test/helpers/compiler.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ module.exports = function compiler (fixture, config, options) {
4343

4444
if (!options.emit) compiler.outputFileSystem = new MemoryFS()
4545

46-
return new Promise((resolve, reject) => {
47-
return compiler.run((err, stats) => {
48-
if (err) reject(err)
46+
if (options.watching) {
47+
return new Promise((resolve, reject) => {
48+
const c = compiler.watch({}, (err, stats) => {
49+
options.handler(err, stats, (s) => {
50+
c.close(resolve)
51+
})
52+
})
53+
})
54+
} else {
55+
return new Promise((resolve, reject) => {
56+
return compiler.run((err, stats) => {
57+
if (err) reject(err)
4958

50-
resolve(stats)
59+
resolve(stats)
60+
})
5161
})
52-
})
62+
}
5363
}

test/helpers/fileChange.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const path = require('path')
2+
const { readFile, writeFile, unlink } = require('fs')
3+
const { promisify } = require('util')
4+
5+
const rf = promisify(readFile)
6+
const wf = promisify(writeFile)
7+
const rm = promisify(unlink)
8+
9+
function readCssFile (name) {
10+
const fileName = path.join(__dirname, '../fixtures', name)
11+
12+
return rf(fileName)
13+
.then(c => c.toString())
14+
}
15+
16+
function writeCssFile (name, contents) {
17+
const fileName = path.join(__dirname, '../fixtures', name)
18+
19+
return wf(fileName, contents)
20+
}
21+
22+
module.exports.copyCssFile = function copyCssFile (src, dest) {
23+
return readCssFile(src)
24+
.then(contents => writeCssFile(dest, contents))
25+
}
26+
27+
module.exports.deleteCssFile = function deleteCssFile (name) {
28+
const fileName = path.join(__dirname, '../fixtures', name)
29+
30+
return rm(fileName)
31+
}

test/loader.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const webpack = require('./helpers/compiler')
44
const { loader } = require('./helpers/compilation')
5+
const { copyCssFile, deleteCssFile } = require('./helpers/fileChange');
56

67
describe('Loader', () => {
78
test('Default', () => {
@@ -20,4 +21,62 @@ describe('Loader', () => {
2021
expect(src).toMatchSnapshot()
2122
})
2223
})
24+
25+
describe('Watching Deps After An Error', () => {
26+
const files = {
27+
syntaxError: "css-watching/syntaxError.css",
28+
noSyntaxError: "css-watching/noSyntaxError.css",
29+
changingFile: "css-watching/styleDep.css"
30+
}
31+
32+
beforeAll(() => copyCssFile(files.noSyntaxError, files.changingFile))
33+
34+
afterAll(() => deleteCssFile(files.changingFile))
35+
36+
test('Default', () => {
37+
const config = {
38+
loader: {
39+
options: {
40+
plugins: [require("postcss-import")],
41+
watching: true
42+
}
43+
}
44+
}
45+
46+
const testSteps = [
47+
(stats) => {
48+
const { err, src } = loader(stats)
49+
expect(src).toMatchSnapshot()
50+
expect(err.length).toEqual(0)
51+
return copyCssFile(files.syntaxError, files.changingFile)
52+
},
53+
(stats) => {
54+
const { err, src } = loader(stats)
55+
expect(src).toMatchSnapshot()
56+
expect(err.length).toEqual(1)
57+
return copyCssFile(files.noSyntaxError, files.changingFile)
58+
},
59+
(stats, close) => {
60+
const { err, src } = loader(stats)
61+
expect(src).toMatchSnapshot()
62+
expect(src).toEqual("module.exports = \"a { color: black }\\n\"")
63+
expect(err.length).toEqual(0)
64+
return close()
65+
}
66+
];
67+
68+
var currentStep = 0
69+
70+
const options = {
71+
watching: true,
72+
handler: (err, stats, close) => {
73+
testSteps[currentStep](stats, close)
74+
currentStep++
75+
}
76+
}
77+
78+
return webpack('css-watching/index.js', config, options)
79+
})
80+
})
81+
2382
})

0 commit comments

Comments
 (0)