Skip to content

Commit

Permalink
Refactor lib/__tests__/integration.test.js (#5284)
Browse files Browse the repository at this point in the history
* Refactor `lib/__tests__/integration.test.js`

- Reduce callbacks as much as possible via `async/await` syntax. (see also <https://jestjs.io/docs/asynchronous>)
- Unify indentation of tested CSS-like code for readability.
- Add a utility function to reduce duplication.

* Re-indent template literals via `stripIndent()`
  • Loading branch information
ybiquitous committed May 12, 2021
1 parent 5950580 commit 5434bd1
Showing 1 changed file with 63 additions and 61 deletions.
124 changes: 63 additions & 61 deletions lib/__tests__/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ const path = require('path');
const postcss = require('postcss');
const sassSyntax = require('postcss-sass');
const scssSyntax = require('postcss-scss');
const { stripIndent } = require('common-tags');

const stylelint = require('..');

const fixtures = (...elem) => path.join(__dirname, 'fixtures', ...elem);

const config = {
rules: {
'block-opening-brace-newline-after': 'always',
Expand All @@ -26,36 +30,33 @@ const config = {
},
};

const css = `
a { background: pink; }
const css = stripIndent`
a { background: pink; }
b {
height: 1rem;
display: block;
width: 10px;
color: #zzz;
}
b {
height: 1rem;
display: block;
width: 10px;
color: #zzz;
}
/* stylelint-disable color-no-invalid-hex */
.foo {
color: #yyy;
}
/* stylelint-enable */
/* stylelint-disable color-no-invalid-hex */
.foo {
color: #yyy;
}
/* stylelint-enable */
.bar:before {
color: #mmm;
height: calc(1rem - 100%)
}
`;
.bar:before {
color: #mmm;
height: calc(1rem - 100%)
}
`;

describe('integration test expecting warnings', () => {
let result;

beforeEach(() => {
return postcss()
.use(stylelint(config))
.process(css, { from: undefined })
.then((data) => (result = data));
beforeEach(async () => {
result = await postcss().use(stylelint(config)).process(css, { from: undefined });
});

it('number and type', () => {
Expand Down Expand Up @@ -103,61 +104,62 @@ describe('integration test expecting warnings', () => {
});
});

it('Less integration test', () => {
const less = `.foo(@bar) {
color: @bar;
}
`;
it('Less integration test', async () => {
const less = stripIndent`
.foo(@bar) {
color: @bar;
}
`;

return postcss()
const result = await postcss()
.use(stylelint({ rules: {} }))
.process(less, { syntax: lessSyntax, from: undefined })
.then((result) => {
expect(result.messages).toHaveLength(0);
});
.process(less, { syntax: lessSyntax, from: undefined });

expect(result.messages).toHaveLength(0);
});

it('Sass integration test', () => {
const sass = `.foo-#{variable}
color: $color`;
it('Sass integration test', async () => {
const sass = stripIndent`
.foo-#{variable}
color: $color
`;

return postcss()
const result = await postcss()
.use(stylelint({ rules: {} }))
.process(sass, { syntax: sassSyntax, from: undefined })
.then((result) => {
expect(result.messages).toHaveLength(0);
});
.process(sass, { syntax: sassSyntax, from: undefined });

expect(result.messages).toHaveLength(0);
});

it('Scss integration test', () => {
const scss = `.foo-#{variable} {
color: $color;
}
`;
it('Scss integration test', async () => {
const scss = stripIndent`
.foo-#{variable} {
color: $color;
}
`;

return postcss()
const result = await postcss()
.use(stylelint({ rules: {} }))
.process(scss, { syntax: scssSyntax, from: undefined })
.then((result) => {
expect(result.messages).toHaveLength(0);
});
.process(scss, { syntax: scssSyntax, from: undefined });

expect(result.messages).toHaveLength(0);
});

describe('integration test null option', () => {
let results;

beforeEach(() => {
return stylelint
.lint({
beforeEach(async () => {
results = (
await stylelint.lint({
config: {
extends: [path.join(__dirname, 'fixtures/config-no-pixels')],
extends: [fixtures('config-no-pixels')],
rules: {
'unit-disallowed-list': null,
},
},
code: 'a { top: 10px; }',
})
.then((data) => (results = data.results));
).results;
});

it('no invalid option warnings', () => {
Expand All @@ -172,18 +174,18 @@ describe('integration test null option', () => {
describe('integration test [null] option', () => {
let results;

beforeEach(() => {
return stylelint
.lint({
beforeEach(async () => {
results = (
await stylelint.lint({
config: {
extends: [path.join(__dirname, 'fixtures/config-no-pixels')],
extends: [fixtures('config-no-pixels')],
rules: {
'unit-disallowed-list': [null],
},
},
code: 'a { top: 10px; }',
})
.then((data) => (results = data.results));
).results;
});

it('no invalid option warnings', () => {
Expand Down

0 comments on commit 5434bd1

Please sign in to comment.