From b01ed25dfa3e8231a976eef76bd5e81fb535b1e9 Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Fri, 14 May 2021 04:15:49 +0900 Subject: [PATCH] Refactor `lib/__tests__/plugins.test.js` (#5300) Reduce callbacks as much as possible via `async/await` syntax. (see also ) --- lib/__tests__/plugins.test.js | 216 +++++++++++++++------------------- 1 file changed, 93 insertions(+), 123 deletions(-) diff --git a/lib/__tests__/plugins.test.js b/lib/__tests__/plugins.test.js index a63f41b890..b16a3f95b4 100644 --- a/lib/__tests__/plugins.test.js +++ b/lib/__tests__/plugins.test.js @@ -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', () => { @@ -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); }); }); @@ -174,28 +168,24 @@ 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: { @@ -203,20 +193,12 @@ it('slashless plugin causes configuration error', () => { }, }; - 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: { @@ -224,15 +206,12 @@ it('plugin with primary option array', () => { }, }; - 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: { @@ -240,15 +219,12 @@ it('plugin with primary option array within options array', () => { }, }; - 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: { @@ -256,12 +232,9 @@ it('plugin with async rule', () => { }, }; - 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', () => { @@ -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', () => {