Skip to content

Commit acd1c9c

Browse files
authored
Merge pull request #52 from suitcss/test-refactor
Refactor unit tests
2 parents 51e4caa + 5c0a33c commit acd1c9c

File tree

10 files changed

+506
-492
lines changed

10 files changed

+506
-492
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@
4242
"mocha": "^2.3.4",
4343
"postcss-property-lookup": "^1.1.4",
4444
"rewire": "^2.5.1",
45-
"sinon": "^1.17.2",
46-
"sinon-as-promised": "^4.0.0",
47-
"sinon-chai": "^2.8.0"
45+
"sinon": "^1.17.2"
4846
},
4947
"scripts": {
5048
"test": "npm run mocha && npm run lint",
51-
"mocha": "mocha --reporter spec --slow 400 --timeout 8000",
49+
"mocha": "mocha test/index.js --reporter spec --slow 400 --timeout 8000",
5250
"lint": "eslint lib/index.js test/test.js bin/suitcss",
5351
"watch": "npm run mocha -- --watch"
5452
},

test/cli.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
var chai = require('chai');
2+
var child = require('child_process');
3+
var util = require('./util');
4+
var exec = child.exec;
5+
var expect = chai.expect;
6+
7+
describe('cli', function() {
8+
var input = util.read('fixtures/cli/input');
9+
var output = util.read('fixtures/cli/input.out');
10+
11+
afterEach(function() {
12+
util.remove('fixtures/cli/output');
13+
});
14+
15+
it('should read from a file and write to a file', function(done) {
16+
exec('node bin/suitcss -c test/config/noautoprefixer.js test/fixtures/cli/input.css test/fixtures/cli/output.css', function(err) {
17+
if (err) return done(err);
18+
var res = util.read('fixtures/cli/output');
19+
expect(res).to.equal(output);
20+
done();
21+
});
22+
});
23+
24+
it('should read from a file and write to stdout', function(done) {
25+
exec('node bin/suitcss -L -c test/config/noautoprefixer.js test/fixtures/cli/input.css', function(err, stdout) {
26+
if (err) return done(err);
27+
expect(stdout).to.equal(output);
28+
done();
29+
});
30+
});
31+
32+
it('should read from stdin and write to stdout', function(done) {
33+
var testChild = exec('node bin/suitcss -L -c test/config/noautoprefixer.js', function(err, stdout) {
34+
if (err) return done(err);
35+
expect(stdout).to.equal(output);
36+
done();
37+
});
38+
39+
testChild.stdin.write(new Buffer(input));
40+
testChild.stdin.end();
41+
});
42+
43+
it('should log on verbose', function(done) {
44+
exec('node bin/suitcss -v -c test/config/noautoprefixer.js test/fixtures/cli/input.css test/fixtures/cli/output.css', function(err, stdout) {
45+
if (err) return done(err);
46+
expect(stdout).to.contain('write');
47+
done();
48+
});
49+
});
50+
51+
it('should allow configurable import root', function(done) {
52+
exec('node bin/suitcss -i test/fixtures -c test/config/noautoprefixer.js test/fixtures/import.css test/fixtures/cli/output.css', function(err) {
53+
if (err) return done(err);
54+
var res = util.read('fixtures/cli/output');
55+
var expected = util.read('fixtures/component.out');
56+
expect(res).to.equal(expected);
57+
done();
58+
});
59+
});
60+
61+
it('should output stylelint warnings', function(done) {
62+
exec('node bin/suitcss -i test/fixtures -c test/config/noautoprefixer.js test/fixtures/stylelint-import.css test/fixtures/cli/output.css -l', function(err, stdout) {
63+
if (err) return done(err);
64+
expect(stdout).to.contain('Expected indentation of 2 spaces');
65+
done();
66+
});
67+
});
68+
69+
it('should minify the output', function(done) {
70+
exec('node bin/suitcss -i test/fixtures -c test/config/noautoprefixer.js test/fixtures/import.css test/fixtures/cli/output.css -m', function(err) {
71+
if (err) return done(err);
72+
var res = util.read('fixtures/cli/output');
73+
var expected = util.read('fixtures/minify.out');
74+
expect(res).to.equal(expected);
75+
done();
76+
});
77+
});
78+
79+
it('should allow a config file to be passed', function(done) {
80+
exec('node bin/suitcss -i test/fixtures -c test/config/test.js test/fixtures/config.css test/fixtures/cli/output.css', function(err) {
81+
if (err) return done(err);
82+
var res = util.read('fixtures/cli/output');
83+
var expected = util.read('fixtures/config.out');
84+
expect(res).to.equal(expected);
85+
done();
86+
});
87+
});
88+
89+
it('should output an error to stderr on conformance failure when --throw-error is set', function(done) {
90+
exec('node bin/suitcss -i test/fixtures -e test/fixtures/import-error.css test/fixtures/cli/output.css', function(err, stdout, stderr) {
91+
expect(err).to.be.an('error');
92+
expect(err.code).to.equal(1);
93+
expect(stderr).to.contain('postcss-reporter: warnings or errors were found');
94+
done();
95+
});
96+
});
97+
98+
it('should log on non-existant file', function(done) {
99+
exec('node bin/suitcss -c test/config/noautoprefixer.js test/fixtures/cli/non-existant.css', function(err, stdout, stderr) {
100+
expect(err).to.be.an('error');
101+
expect(err.code).to.equal(1);
102+
expect(stderr).to.contain('not found');
103+
done();
104+
});
105+
});
106+
});
107+
File renamed without changes.

test/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require('./node');
2+
require('./options');
3+
require('./cli');
4+
require('./linting');

test/linting.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var chai = require('chai');
2+
var chaiAsPromised = require('chai-as-promised');
3+
var suitcss = require('../lib');
4+
var util = require('./util');
5+
var expect = chai.expect;
6+
7+
chai.use(chaiAsPromised);
8+
9+
describe('postcss-bem-linter', function() {
10+
it('should pass if component conforms', function() {
11+
return expect(
12+
suitcss('/** @define Foo */\n\n.Foo { color: red; }\n', {
13+
'postcss-reporter': {
14+
throwError: true
15+
}
16+
})
17+
).to.be.fulfilled;
18+
});
19+
20+
it('should fail if component does not conform', function() {
21+
return expect(
22+
suitcss('/** @define Foo */\n\n.foo { color: red; }\n', {
23+
'postcss-reporter': {
24+
throwError: true
25+
}
26+
})
27+
).to.be.rejectedWith(Error, 'postcss-reporter: warnings or errors were found');
28+
});
29+
});
30+
31+
describe('stylelint', function() {
32+
it('should lint the component', function() {
33+
return expect(
34+
suitcss(util.read('fixtures/component'), {
35+
root: 'test/fixtures',
36+
'postcss-reporter': {
37+
throwError: true
38+
}
39+
})
40+
).to.be.fulfilled;
41+
});
42+
43+
it('should allow the config to be overidden', function() {
44+
return expect(
45+
suitcss('@import "./stylelint.css"\n\n', {
46+
root: 'test/fixtures',
47+
stylelint: {
48+
extends: 'stylelint-config-suitcss',
49+
rules: {
50+
indentation: 4
51+
}
52+
},
53+
'postcss-reporter': {
54+
throwError: true
55+
}
56+
})
57+
).to.be.fulfilled;
58+
});
59+
60+
it('should throw an error if stylelint fails', function() {
61+
return expect(
62+
suitcss('@import "./stylelint.css"\n\n', {
63+
root: 'test/fixtures',
64+
'postcss-reporter': {
65+
throwError: true
66+
}
67+
})
68+
).to.be.rejectedWith(Error, 'postcss-reporter: warnings or errors were found');
69+
});
70+
71+
it('should lint the input file', function() {
72+
return expect(
73+
suitcss('@import "./stylelint.css"\n\n', {
74+
root: 'test/fixtures',
75+
'postcss-reporter': {
76+
throwError: true
77+
}
78+
})
79+
).to.be.rejectedWith(Error, 'postcss-reporter: warnings or errors were found');
80+
});
81+
});

test/node.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var chai = require('chai');
2+
var suitcss = require('../lib');
3+
var util = require('./util');
4+
var expect = chai.expect;
5+
6+
describe('node API', function() {
7+
it('should return a css string', function(done) {
8+
suitcss('body {}', {lint: false}).then(function(result) {
9+
expect(result.css).to.be.a('string');
10+
done();
11+
});
12+
});
13+
14+
it('should handle invalid input', function() {
15+
expect(function() {
16+
suitcss(null, {lint: false});
17+
}).to.throw(TypeError);
18+
});
19+
20+
it('should preprocess CSS correctly', function(done) {
21+
var input = util.read('fixtures/component');
22+
var output = util.read('fixtures/component.out');
23+
24+
suitcss(input, {
25+
root: 'test/fixtures',
26+
lint: false,
27+
// disable autoprefixer
28+
autoprefixer: {add: false, remove: false}
29+
}).then(function(result) {
30+
expect(result.css.trim()).to.be.equal(output.trim());
31+
done();
32+
}).catch(done);
33+
});
34+
35+
it('should add vendor prefixes', function(done) {
36+
var input = '.test { filter: blur(1px) }';
37+
var output = '.test { -webkit-filter: blur(1px); filter: blur(1px) }';
38+
39+
suitcss(input, {
40+
lint: false,
41+
autoprefixer: {
42+
browsers: 'Chrome 50'
43+
}
44+
}).then(function(result) {
45+
expect(result.css.trim()).to.be.equal(output.trim());
46+
done();
47+
}).catch(done);
48+
});
49+
});

0 commit comments

Comments
 (0)