Skip to content

Commit daf4a9a

Browse files
authored
Use jest-preset-stylelint (YozhikM#54)
1 parent c96dd46 commit daf4a9a

File tree

16 files changed

+54
-182
lines changed

16 files changed

+54
-182
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
preset: 'jest-preset-stylelint',
23
clearMocks: true,
34
collectCoverage: false,
45
collectCoverageFrom: ['src/**/*.js'],

jest.setup.js

Lines changed: 3 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,6 @@
1-
'use strict'; // eslint-disable-line
1+
'use strict';
22

3-
const _ = require('lodash');
43
const stylelint = require('stylelint');
4+
const getTestRule = require('jest-preset-stylelint/getTestRule');
55

6-
global.testRule = (rule, schema) => {
7-
expect.extend({
8-
toHaveMessage(testCase) {
9-
if (testCase.message === undefined) {
10-
return {
11-
message: () => 'Expected "reject" test case to have a "message" property',
12-
pass: false,
13-
};
14-
}
15-
16-
return {
17-
pass: true,
18-
};
19-
},
20-
});
21-
22-
describe(schema.ruleName, () => {
23-
const stylelintConfig = {
24-
plugins: ['./src'],
25-
rules: {
26-
[schema.ruleName]: schema.config,
27-
},
28-
};
29-
30-
if (schema.accept && schema.accept.length) {
31-
describe('accept', () => {
32-
schema.accept.forEach(testCase => {
33-
const spec = testCase.only ? it.only : it;
34-
35-
spec(testCase.description || 'no description', () => {
36-
const options = {
37-
code: testCase.code,
38-
config: stylelintConfig,
39-
syntax: schema.syntax,
40-
};
41-
42-
return stylelint.lint(options).then(output => {
43-
expect(output.results[0].warnings).toEqual([]);
44-
if (!schema.fix) {
45-
return;
46-
}
47-
48-
// Check the fix
49-
return stylelint.lint(Object.assign({ fix: true }, options)).then(output2 => {
50-
const fixedCode = getOutputCss(output2);
51-
52-
expect(fixedCode).toBe(testCase.code);
53-
});
54-
});
55-
});
56-
});
57-
});
58-
}
59-
60-
if (schema.reject && schema.reject.length) {
61-
describe('reject', () => {
62-
schema.reject.forEach(testCase => {
63-
const spec = testCase.only ? it.only : it;
64-
65-
spec(testCase.description || 'no description', () => {
66-
const options = {
67-
code: testCase.code,
68-
config: stylelintConfig,
69-
syntax: schema.syntax,
70-
};
71-
72-
return stylelint.lint(options).then(output => {
73-
const warnings = output.results[0].warnings;
74-
const warning = warnings[0];
75-
76-
expect(warnings.length).toBeGreaterThanOrEqual(1);
77-
// expect(testCase).toHaveMessage();
78-
79-
if (testCase.message !== undefined) {
80-
expect(_.get(warning, 'text')).toBe(testCase.message);
81-
}
82-
83-
if (testCase.line !== undefined) {
84-
expect(_.get(warning, 'line')).toBe(testCase.line);
85-
}
86-
87-
if (testCase.column !== undefined) {
88-
expect(_.get(warning, 'column')).toBe(testCase.column);
89-
}
90-
91-
if (!schema.fix) {
92-
return;
93-
}
94-
95-
if (!testCase.fixed) {
96-
throw new Error(
97-
'If using { fix: true } in test schema, all reject cases must have { fixed: .. }'
98-
);
99-
}
100-
101-
// Check the fix
102-
return stylelint.lint(Object.assign({ fix: true }, options)).then(output2 => {
103-
const fixedCode = getOutputCss(output2);
104-
105-
expect(fixedCode).toBe(testCase.fixed);
106-
expect(output2.results[0].warnings.length).toBe(0); // Ensure errors are not reported on fixed code
107-
});
108-
});
109-
});
110-
});
111-
});
112-
}
113-
});
114-
};
115-
116-
function getOutputCss(output) {
117-
const result = output.results[0]._postcssResult;
118-
const css = result.root.toString(result.opts.syntax);
119-
120-
return css;
121-
}
122-
123-
global.testConfig = input => {
124-
let testFn;
125-
126-
if (input.only) {
127-
testFn = test.only;
128-
} else if (input.skip) {
129-
testFn = test.skip;
130-
} else {
131-
testFn = test;
132-
}
133-
134-
testFn(input.description, () => {
135-
const config = {
136-
plugins: ['./'],
137-
rules: {
138-
[input.ruleName]: input.config,
139-
},
140-
};
141-
142-
return stylelint
143-
.lint({
144-
code: '',
145-
config,
146-
})
147-
.then(function(data) {
148-
const invalidOptionWarnings = data.results[0].invalidOptionWarnings;
149-
150-
if (input.valid) {
151-
expect(invalidOptionWarnings.length).toBe(0);
152-
} else {
153-
expect(invalidOptionWarnings[0].text).toBe(input.message);
154-
}
155-
});
156-
});
157-
};
6+
global.testRule = getTestRule(stylelint, { plugins: ['./src'] });

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"extend": ">=3.0.2",
5656
"jest": "^23.6.0",
5757
"jest-cli": "^23.6.0",
58+
"jest-preset-stylelint": "^2.0.0",
5859
"lodash": "^4.17.11",
5960
"prettier": "^1.14.3",
6061
"rimraf": "^2.6.2",

src/rules/content-property-no-static-value/__tests__/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import rule, { messages, ruleName } from '../index';
1+
import { messages, ruleName } from '../index';
22

3-
testRule(rule, {
3+
testRule({
44
ruleName,
55
config: [true],
66

@@ -40,6 +40,9 @@ testRule(rule, {
4040
},
4141
{
4242
code: ".foo:before, .bar { content: ''; }",
43+
message: messages.expected('.foo:before, .bar'),
44+
line: 1,
45+
column: 3,
4346
},
4447
],
4548
});

src/rules/font-size-is-readable/__tests__/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import rule, { messages, ruleName } from '../index';
1+
import { messages, ruleName } from '../index';
22

3-
testRule(rule, {
3+
testRule({
44
ruleName,
55
config: [true],
66

src/rules/line-height-is-vertical-rhythmed/__tests__/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import rule, { messages, ruleName } from '../index';
1+
import { messages, ruleName } from '../index';
22

3-
testRule(rule, {
3+
testRule({
44
ruleName,
55
config: [true],
66

src/rules/media-prefers-color-scheme/__tests__/index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import rule, { messages, ruleName } from '../index';
1+
import { messages, ruleName } from '../index';
22

3-
testRule(rule, {
3+
testRule({
44
ruleName,
55
config: [true],
66

@@ -41,9 +41,18 @@ testRule(rule, {
4141
{
4242
code:
4343
'.bar { color: red; } .baz { background-color: red; } @media screen and (prefers-color-scheme: dark) { .baz { color: blue; } }',
44-
message: messages.expected('.bar'),
45-
line: 1,
46-
column: 3,
44+
warnings: [
45+
{
46+
message: messages.expected('.bar'),
47+
line: 1,
48+
column: 3,
49+
},
50+
{
51+
message: messages.expected('.baz'),
52+
line: 1,
53+
column: 24,
54+
},
55+
],
4756
},
4857
{
4958
code:

src/rules/media-prefers-reduced-motion/__tests__/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import rule, { messages, ruleName } from '../index';
1+
import { messages, ruleName } from '../index';
22

3-
testRule(rule, {
3+
testRule({
44
ruleName,
55
config: [true],
66
fix: true,

src/rules/no-display-none/__tests__/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import rule, { messages, ruleName } from '../index';
1+
import { messages, ruleName } from '../index';
22

3-
testRule(rule, {
3+
testRule({
44
ruleName,
55
config: [true],
66

0 commit comments

Comments
 (0)