Skip to content

Commit

Permalink
Refactor lib/__tests__/plugins.test.js (#5300)
Browse files Browse the repository at this point in the history
Reduce callbacks as much as possible via `async/await` syntax.
(see also <https://jestjs.io/docs/asynchronous>)
  • Loading branch information
ybiquitous committed May 13, 2021
1 parent db56ce7 commit b01ed25
Showing 1 changed file with 93 additions and 123 deletions.
216 changes: 93 additions & 123 deletions lib/__tests__/plugins.test.js
Expand Up @@ -52,46 +52,46 @@ const processorRelativeAndExtendRelative = postcss().use(
}),
);

it('one plugin runs', () => {
return processorRelative.process(cssWithFoo, { from: undefined }).then((result) => {
expect(result.warnings()).toHaveLength(2);
expect(result.warnings()[0].text).toBe('found .foo (plugin/warn-about-foo)');
expect(result.warnings()[0].node).toBeTruthy();
});
it('one plugin runs', async () => {
const result = await processorRelative.process(cssWithFoo, { from: undefined });

expect(result.warnings()).toHaveLength(2);
expect(result.warnings()[0].text).toBe('found .foo (plugin/warn-about-foo)');
expect(result.warnings()[0].node).toBeTruthy();
});

it('another plugin runs', () => {
return processorRelative.process(cssWithoutFoo, { from: undefined }).then((result) => {
expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('Unexpected empty block (block-no-empty)');
});
it('another plugin runs', async () => {
const result = await processorRelative.process(cssWithoutFoo, { from: undefined });

expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('Unexpected empty block (block-no-empty)');
});

it('plugin with absolute path and no configBasedir', () => {
return processorAbsolute.process(cssWithFoo, { from: undefined }).then((result) => {
expect(result.warnings()).toHaveLength(2);
expect(result.warnings()[0].text).toBe('found .foo (plugin/warn-about-foo)');
expect(result.warnings()[0].node).toBeTruthy();
});
it('plugin with absolute path and no configBasedir', async () => {
const result = await processorAbsolute.process(cssWithFoo, { from: undefined });

expect(result.warnings()).toHaveLength(2);
expect(result.warnings()[0].text).toBe('found .foo (plugin/warn-about-foo)');
expect(result.warnings()[0].node).toBeTruthy();
});

it('config extending another config that invokes a plugin with a relative path', () => {
return processorExtendRelative.process(cssWithFoo, { from: undefined }).then((result) => {
expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('found .foo (plugin/warn-about-foo)');
expect(result.warnings()[0].node).toBeTruthy();
});
it('config extending another config that invokes a plugin with a relative path', async () => {
const result = await processorExtendRelative.process(cssWithFoo, { from: undefined });

expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('found .foo (plugin/warn-about-foo)');
expect(result.warnings()[0].node).toBeTruthy();
});

it('config with its own plugins extending another config that invokes a plugin with a relative path', () => {
return processorRelativeAndExtendRelative
.process(cssWithFooAndBar, { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(2);
expect(result.warnings()[0].text).toBe('found .bar (plugin/warn-about-bar)');
expect(result.warnings()[1].text).toBe('found .foo (plugin/warn-about-foo)');
expect(result.warnings()[0].node).toBeTruthy();
});
it('config with its own plugins extending another config that invokes a plugin with a relative path', async () => {
const result = await processorRelativeAndExtendRelative.process(cssWithFooAndBar, {
from: undefined,
});

expect(result.warnings()).toHaveLength(2);
expect(result.warnings()[0].text).toBe('found .bar (plugin/warn-about-bar)');
expect(result.warnings()[1].text).toBe('found .foo (plugin/warn-about-foo)');
expect(result.warnings()[0].node).toBeTruthy();
});

describe('plugin using exposed rules via stylelint.rules', () => {
Expand All @@ -108,60 +108,54 @@ describe('plugin using exposed rules via stylelint.rules', () => {
},
});

it('with uppercase expectation and lowercase color', () => {
return postcss()
it('with uppercase expectation and lowercase color', async () => {
const result = await postcss()
.use(stylelint(config('upper')))
.process(cssWithDirectiveLower, { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('Expected "#eee" to be "#EEE" (color-hex-case)');
});
.process(cssWithDirectiveLower, { from: undefined });

expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('Expected "#eee" to be "#EEE" (color-hex-case)');
});

it('with uppercase expectation and uppercase color', () => {
return postcss()
it('with uppercase expectation and uppercase color', async () => {
const result = await postcss()
.use(stylelint(config('upper')))
.process(cssWithDirectiveUpper, { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(0);
});
.process(cssWithDirectiveUpper, { from: undefined });

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

it('with lowercase expectation and uppercase color', () => {
return postcss()
it('with lowercase expectation and uppercase color', async () => {
const result = await postcss()
.use(stylelint(config('lower')))
.process(cssWithDirectiveUpper, { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('Expected "#EEE" to be "#eee" (color-hex-case)');
});
.process(cssWithDirectiveUpper, { from: undefined });

expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('Expected "#EEE" to be "#eee" (color-hex-case)');
});

it('with lowercase expectation and lowercase color', () => {
return postcss()
it('with lowercase expectation and lowercase color', async () => {
const result = await postcss()
.use(stylelint(config('lower')))
.process(cssWithDirectiveLower, { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(0);
});
.process(cssWithDirectiveLower, { from: undefined });

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

it('with uppercase expectation and lowercase color without directive', () => {
return postcss()
it('with uppercase expectation and lowercase color without directive', async () => {
const result = await postcss()
.use(stylelint(config('upper')))
.process(cssWithoutDirectiveLower, { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(0);
});
.process(cssWithoutDirectiveLower, { from: undefined });

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

it('with uppercase expectation and uppercase color without directive', () => {
return postcss()
it('with uppercase expectation and uppercase color without directive', async () => {
const result = await postcss()
.use(stylelint(config('lower')))
.process(cssWithoutDirectiveUpper, { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(0);
});
.process(cssWithoutDirectiveUpper, { from: undefined });

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

Expand All @@ -174,94 +168,73 @@ describe('module providing an array of plugins', () => {
},
};

it('first plugin works', () => {
return postcss()
it('first plugin works', async () => {
const result = await postcss()
.use(stylelint(config))
.process('@@check-color-hex-case a { color: #eee; }', { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('Expected "#eee" to be "#EEE" (color-hex-case)');
});
.process('@@check-color-hex-case a { color: #eee; }', { from: undefined });

expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('Expected "#eee" to be "#EEE" (color-hex-case)');
});

it('second plugin works', () => {
return postcss()
.use(stylelint(config))
.process('.foo {}', { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('found .foo (plugin/warn-about-foo)');
});
it('second plugin works', async () => {
const result = await postcss().use(stylelint(config)).process('.foo {}', { from: undefined });

expect(result.warnings()).toHaveLength(1);
expect(result.warnings()[0].text).toBe('found .foo (plugin/warn-about-foo)');
});
});

it('slashless plugin causes configuration error', () => {
it('slashless plugin causes configuration error', async () => {
const config = {
plugins: [path.join(__dirname, 'fixtures/plugin-slashless-warn-about-foo')],
rules: {
'slashless-warn-about-foo': true,
},
};

return postcss()
.use(stylelint(config))
.process('.foo {}', { from: undefined })
.then(() => {
throw new Error('should not have succeeded');
})
.catch((err) => {
expect(err.message.startsWith('stylelint v7+ requires plugin rules to be namespaced')).toBe(
true,
);
});
await expect(
postcss().use(stylelint(config)).process('.foo {}', { from: undefined }),
).rejects.toThrow(/^stylelint v7\+ requires plugin rules to be namespaced/);
});

it('plugin with primary option array', () => {
it('plugin with primary option array', async () => {
const config = {
plugins: [path.join(__dirname, 'fixtures/plugin-primary-array')],
rules: {
'plugin/primary-array': ['foo', 'bar'],
},
};

return postcss()
.use(stylelint(config))
.process('a {}', { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(0);
});
const result = await postcss().use(stylelint(config)).process('a {}', { from: undefined });

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

it('plugin with primary option array within options array', () => {
it('plugin with primary option array within options array', async () => {
const config = {
plugins: [path.join(__dirname, 'fixtures/plugin-primary-array')],
rules: {
'plugin/primary-array': [['foo', 'bar'], { something: true }],
},
};

return postcss()
.use(stylelint(config))
.process('a {}', { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(0);
});
const result = await postcss().use(stylelint(config)).process('a {}', { from: undefined });

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

it('plugin with async rule', () => {
it('plugin with async rule', async () => {
const config = {
plugins: [path.join(__dirname, 'fixtures/plugin-async')],
rules: {
'plugin/async': true,
},
};

return postcss()
.use(stylelint(config))
.process('a {}', { from: undefined })
.then((result) => {
expect(result.warnings()).toHaveLength(1);
});
const result = await postcss().use(stylelint(config)).process('a {}', { from: undefined });

expect(result.warnings()).toHaveLength(1);
});

describe('loading a plugin from process.cwd', () => {
Expand All @@ -284,11 +257,8 @@ describe('loading a plugin from process.cwd', () => {
process.chdir(actualCwd);
});

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

it('error is caught', () => {
Expand Down

0 comments on commit b01ed25

Please sign in to comment.