-
Notifications
You must be signed in to change notification settings - Fork 20
/
scss-processor.tests.js
177 lines (138 loc) · 6.71 KB
/
scss-processor.tests.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* eslint-env node, mocha */
import './test-helpers/global-variables.stub';
import chai from 'chai';
import ScssProcessor from './scss-processor';
import { reloadOptions } from './options';
import logger from './logger';
import generateFileObject from './test-helpers/generate-file-object';
const expect = chai.expect;
describe('ScssProcessor', function() {
describe('#isRoot', function() {
it('should return false if the filename starts with an underscore', function z() {
const file = generateFileObject('./_test.scss', '');
const options = { ...reloadOptions(), enableSassCompilation: true };
const processor = new ScssProcessor(options);
expect(processor.isRoot(file)).to.be.false;
});
it('should return false if the file options "isImport" property is true', function z() {
const file = generateFileObject('./test.scss', '');
file.fileOptions.isImport = true;
const options = { ...reloadOptions(), enableSassCompilation: true };
const processor = new ScssProcessor(options);
expect(processor.isRoot(file)).to.be.false;
});
it('should return true if the above conditions are not met', function z() {
const file = generateFileObject('./test.scss', '');
const options = { ...reloadOptions(), enableSassCompilation: true };
const processor = new ScssProcessor(options);
expect(processor.isRoot(file)).to.be.true;
});
});
describe('#shouldProcess', function() {
it('should return true if enableSassCompilation is true', function z() {
const file = generateFileObject('./test.scss', '');
const options = { ...reloadOptions(), enableSassCompilation: true };
const processor = new ScssProcessor(options);
expect(processor.shouldProcess(file)).to.be.true;
});
it('should return false if enableSassCompilation is false', function z() {
const file = generateFileObject('./test.scss', '');
const options = { ...reloadOptions(), enableSassCompilation: false };
const processor = new ScssProcessor(options);
expect(processor.shouldProcess(file)).to.be.false;
});
it('should return true if the enableSassCompilation array contains the file\'s extension', function z() {
const file = generateFileObject('./test.scss', '');
const options = { ...reloadOptions(), enableSassCompilation: ['scss'] };
const processor = new ScssProcessor(options);
expect(processor.shouldProcess(file)).to.be.true;
});
it('should return false if the enableSassCompilation array does not contain the file\'s extension', function z() {
const file = generateFileObject('./test.scss', '');
const options = { ...reloadOptions(), enableSassCompilation: [] };
const processor = new ScssProcessor(options);
expect(processor.shouldProcess(file)).to.be.false;
});
});
describe('#process', function() {
describe('file.contents', function() {
it('should transpile the passed in file', async function z() {
const file = generateFileObject('./test.scss', '.test { .nested { color: red; } } .test2 { color: blue; } // a comment');
const options = { ...reloadOptions(), enableSassCompilation: true };
const processor = new ScssProcessor(options);
await processor.process(file);
expect(file.contents).to.equal('.test .nested {\n color: red; }\n\n.test2 {\n color: blue; }\n\n/*# sourceMappingURL=.test.scss.map */');
});
});
describe('file.referencedImportPaths', function() {
it('should list all of the files that the current file imports', async function z() {
const allFiles = new Map();
addFile(generateFileObject('./direct-import1.scss', '.test { color: red; }'));
addFile(generateFileObject('./direct-import2.scss', '@import "./indirect-import.scss"; .test { color: red; }'));
addFile(generateFileObject('./indirect-import.scss', '.test { color: red; }'));
const file = generateFileObject('./test.scss', '@import "./direct-import1.scss"; @import "./direct-import2"; .test { color: blue; }');
const options = { ...reloadOptions(), enableSassCompilation: true, extensions: ['scss'] };
const processor = new ScssProcessor(options);
await processor.process(file, allFiles);
expect(file.referencedImportPaths).to.eql([
'D:/projects/meteor-css-modules/direct-import1.scss',
'D:/projects/meteor-css-modules/direct-import2.scss',
'D:/projects/meteor-css-modules/indirect-import.scss'
]);
function addFile(file) {
allFiles.set(file.importPath, file);
}
});
});
describe('file.sourcemap', function() {
it('should generate a sourcemap', async function z() {
const file = generateFileObject('./test.scss', '.test { color: red; } .test2 { color: blue; }');
const options = { ...reloadOptions(), enableSassCompilation: true };
const processor = new ScssProcessor(options);
await processor.process(file);
expect(file.sourceMap).to.eql({
'version': 3,
'sourceRoot': '.',
'file': '.test.scss',
'sources': [
'test.scss'
],
'sourcesContent': [
'.test { color: red; } .test2 { color: blue; }'
],
'mappings': 'AAAA,AAAA,KAAK,CAAC;EAAE,KAAK,EAAE,GAAI,GAAI;;AAAA,AAAA,MAAM,CAAC;EAAE,KAAK,EAAE,IAAK,GAAI',
'names': []
});
});
});
it('should log a friendly error when node-sass encounters an error', function z(done) {
const file = generateFileObject('./test.scss', '.test { error! }');
let fullErrorMessage = '';
logger.test(function() {
logger.error.addHook(errorMessage => {
fullErrorMessage += errorMessage;
});
const options = { ...reloadOptions(), enableSassCompilation: true };
const processor = new ScssProcessor(options);
try {
processor.process(file);
} catch (err) {
expect(fullErrorMessage).to.equal('\n/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Processing Step: SCSS compilationUnable to compile D:/projects/meteor-css-modules/test.scss\nLine: 1, Column: 9\nError: property "error" must be followed by a \':\'\n/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
done();
}
});
});
it('should throw an error when node-sass encounters an error', function z(done) {
const file = generateFileObject('./test.scss', '.test { error! }');
logger.test(function() {
const options = { ...reloadOptions(), enableSassCompilation: true };
const processor = new ScssProcessor(options);
try {
processor.process(file);
} catch (err) {
done();
}
});
});
});
});