From 2a604d102da514333a666fedb300366b7672e96d Mon Sep 17 00:00:00 2001 From: Sigve Hoel <6737410+sigveio@users.noreply.github.com> Date: Wed, 7 Jul 2021 21:06:46 +0200 Subject: [PATCH 1/4] test: fix misc. test warnings - Replace deprecated postcss.plugin syntax - Add { from: undefined } option to prevent warnings - Dummy workaround for calling postcss w/o plugins --- .../src/__tests__/sameParent.test.js | 27 ++++++++++++------- packages/cssnano/src/__tests__/api.js | 2 +- packages/cssnano/src/__tests__/issue26.js | 21 ++++++++------- packages/cssnano/src/__tests__/issue927.js | 21 ++++++++------- .../src/__tests__/postcss-normalize-url.js | 2 +- .../src/__tests__/index.js | 22 ++++++++------- packages/stylehacks/src/__tests__/api.js | 24 ++++++++++------- packages/stylehacks/src/__tests__/issue8.js | 14 +++++++--- 8 files changed, 80 insertions(+), 53 deletions(-) diff --git a/packages/cssnano-utils/src/__tests__/sameParent.test.js b/packages/cssnano-utils/src/__tests__/sameParent.test.js index dfe421430..7e6cbd8e4 100644 --- a/packages/cssnano-utils/src/__tests__/sameParent.test.js +++ b/packages/cssnano-utils/src/__tests__/sameParent.test.js @@ -1,8 +1,15 @@ import postcss from 'postcss'; import sameParent from '../sameParent'; +const plugin = () => { + return { + postcssPlugin: 'dummy', + }; +}; +plugin.postcss = true; + test('should calculate same parent', () => { - return postcss() + return postcss(plugin) .process('h1 {} h2 {}', { from: undefined }) .then((result) => { const h1 = result.root.nodes[0]; @@ -13,7 +20,7 @@ test('should calculate same parent', () => { }); test('should calculate same parent (detached nodes)', () => { - return postcss() + return postcss(plugin) .process('h1 {} h2 {}', { from: undefined }) .then((result) => { const h1 = result.root.nodes[0]; @@ -27,7 +34,7 @@ test('should calculate same parent (detached nodes)', () => { }); test('should calculate same parent (at rules)', () => { - return postcss() + return postcss(plugin) .process('@media screen{h1 {} h2 {}}', { from: undefined }) .then((result) => { const h1 = result.root.nodes[0].nodes[0]; @@ -38,7 +45,7 @@ test('should calculate same parent (at rules)', () => { }); test('should calculate same parent (multiple at rules)', () => { - return postcss() + return postcss(plugin) .process('@media screen{h1 {}} @media screen{h2 {}}', { from: undefined }) .then((result) => { const h1 = result.root.nodes[0].nodes[0]; @@ -49,7 +56,7 @@ test('should calculate same parent (multiple at rules)', () => { }); test('should calculate same parent (multiple at rules (uppercase))', () => { - return postcss() + return postcss(plugin) .process('@media screen{h1 {}} @MEDIA screen{h2 {}}', { from: undefined }) .then((result) => { const h1 = result.root.nodes[0].nodes[0]; @@ -60,7 +67,7 @@ test('should calculate same parent (multiple at rules (uppercase))', () => { }); test('should calculate same parent (nested at rules)', () => { - return postcss() + return postcss(plugin) .process( ` @media screen { @@ -85,7 +92,7 @@ test('should calculate same parent (nested at rules)', () => { }); test('should calculate not same parent (nested at rules)', () => { - return postcss() + return postcss(plugin) .process( ` @media screen { @@ -110,7 +117,7 @@ test('should calculate not same parent (nested at rules)', () => { }); test('should calculate not same parent (nested at rules) (2)', () => { - return postcss() + return postcss(plugin) .process( ` @media print { @@ -135,7 +142,7 @@ test('should calculate not same parent (nested at rules) (2)', () => { }); test('should calculate not same parent (nested at rules) (3)', () => { - return postcss() + return postcss(plugin) .process( ` @supports(pointer: course) { @@ -158,7 +165,7 @@ test('should calculate not same parent (nested at rules) (3)', () => { }); test('should calculate not same parent (nested at rules) (4)', () => { - return postcss() + return postcss(plugin) .process( ` @media screen { diff --git a/packages/cssnano/src/__tests__/api.js b/packages/cssnano/src/__tests__/api.js index 80c73c4c4..6fa4086a0 100644 --- a/packages/cssnano/src/__tests__/api.js +++ b/packages/cssnano/src/__tests__/api.js @@ -6,7 +6,7 @@ function pluginMacro(instance) { const min = 'h1{color:#fff}'; return () => - instance.process(css).then((result) => { + instance.process(css, { from: undefined }).then((result) => { expect(result.css).toBe(min); }); } diff --git a/packages/cssnano/src/__tests__/issue26.js b/packages/cssnano/src/__tests__/issue26.js index 62956c353..18cdc6dd7 100644 --- a/packages/cssnano/src/__tests__/issue26.js +++ b/packages/cssnano/src/__tests__/issue26.js @@ -24,17 +24,20 @@ const fixture = ` const expected = `@media print{.test{-webkit-border-radius:0;border-radius:0;-webkit-box-shadow:none;box-shadow:none}}.test{width:500px}`; test('it should compress whitespace after node.clone()', () => { - const processor = postcss([ - postcss.plugin('cloner', () => { - return (css) => { - css.walkAtRules((rule) => { - css.prepend(rule.clone()); + const plugin = () => { + return { + postcssPlugin: 'cloner', + Once(root) { + root.walkAtRules((rule) => { + root.prepend(rule.clone()); rule.remove(); }); - }; - }), - nano(), - ]); + }, + }; + }; + plugin.postcss = true; + + const processor = postcss([plugin, nano()]); return processor .process(fixture, { from: undefined }) diff --git a/packages/cssnano/src/__tests__/issue927.js b/packages/cssnano/src/__tests__/issue927.js index 002aaa68f..7ee7dc1c8 100644 --- a/packages/cssnano/src/__tests__/issue927.js +++ b/packages/cssnano/src/__tests__/issue927.js @@ -13,17 +13,20 @@ p{ const expected = `div{grid-column:span 2}p{column-count:2}`; test('it should compress the columns', () => { - const processor = postcss([ - postcss.plugin('cloner', () => { - return (css) => { - css.walkAtRules((rule) => { - css.prepend(rule.clone()); + const plugin = () => { + return { + postcssPlugin: 'cloner', + Once(root) { + root.walkAtRules((rule) => { + root.prepend(rule.clone()); rule.remove(); }); - }; - }), - nano(), - ]); + }, + }; + }; + plugin.postcss = true; + + const processor = postcss([plugin, nano()]); return processor .process(fixture, { from: undefined }) diff --git a/packages/cssnano/src/__tests__/postcss-normalize-url.js b/packages/cssnano/src/__tests__/postcss-normalize-url.js index 0b2c79fbd..a48f542c1 100644 --- a/packages/cssnano/src/__tests__/postcss-normalize-url.js +++ b/packages/cssnano/src/__tests__/postcss-normalize-url.js @@ -151,7 +151,7 @@ test( processCss( '@namespace islands url("http://bar.yandex.ru/ui/islands");', '@namespace islands "http://bar.yandex.ru/ui/islands";', - { discardUnused: { namespace: false } } + { discardUnused: { namespace: false }, from: undefined } ) ); diff --git a/packages/postcss-minify-selectors/src/__tests__/index.js b/packages/postcss-minify-selectors/src/__tests__/index.js index c8b3b678d..bedf54f0c 100644 --- a/packages/postcss-minify-selectors/src/__tests__/index.js +++ b/packages/postcss-minify-selectors/src/__tests__/index.js @@ -480,17 +480,21 @@ test('should handle selectors from other plugins', () => { return result; } - const toModules = postcss.plugin('toModules', () => { - return (css) => { - css.walkRules((rule) => { - rule.selectors = rule.selectors.map((selector) => { - const slice = selector.slice(1); - - return `.${encode(slice).slice(0, 7)}__${slice}`; + const toModules = () => { + return { + postcssPlugin: 'toModules', + Once(root) { + root.walkRules((rule) => { + rule.selectors = rule.selectors.map((selector) => { + const slice = selector.slice(1); + + return `.${encode(slice).slice(0, 7)}__${slice}`; + }); }); - }); + }, }; - }); + }; + toModules.postcss = true; const css = `.test, /* comment #1 - this comment breaks stuff */ .test:hover { /* comment #2 - ...but this comment is fine */ diff --git a/packages/stylehacks/src/__tests__/api.js b/packages/stylehacks/src/__tests__/api.js index 87a8f21c0..286c10d6d 100644 --- a/packages/stylehacks/src/__tests__/api.js +++ b/packages/stylehacks/src/__tests__/api.js @@ -49,15 +49,17 @@ test('should use the postcss plugin api', () => { test('should have a separate detect method', () => { let counter = 0; - let plugin = postcss.plugin('test', () => { - return (css) => { - css.walkDecls((decl) => { + const plugin = () => { + return { + postcssPlugin: 'test', + Declaration(decl) { if (stylehacks.detect(decl)) { counter++; } - }); + }, }; - }); + }; + plugin.postcss = true; return postcss(plugin) .process('h1 { _color: red; =color: black }', { from: undefined }) @@ -67,15 +69,17 @@ test('should have a separate detect method', () => { test('should have a separate detect method (2)', () => { let counter = 0; - let plugin = postcss.plugin('test', () => { - return (css) => { - css.walkRules((rule) => { + const plugin = () => { + return { + postcssPlugin: 'test', + Rule(rule) { if (stylehacks.detect(rule)) { counter++; } - }); + }, }; - }); + }; + plugin.postcss = true; return postcss(plugin) .process('h1 { _color: red; =color: black }', { from: undefined }) diff --git a/packages/stylehacks/src/__tests__/issue8.js b/packages/stylehacks/src/__tests__/issue8.js index d61b3716b..f9a834757 100644 --- a/packages/stylehacks/src/__tests__/issue8.js +++ b/packages/stylehacks/src/__tests__/issue8.js @@ -1,9 +1,15 @@ -import postcss, { plugin, decl } from 'postcss'; +import postcss, { decl } from 'postcss'; import stylehacks from '..'; -const insertZoom = plugin('insertZoom', () => { - return (css) => css.first.append(decl({ prop: '*zoom', value: '1' })); -}); +const insertZoom = () => { + return { + postcssPlugin: 'insertZoom', + Once(root) { + root.first.append(decl({ prop: '*zoom', value: '1' })); + }, + }; +}; +insertZoom.postcss = true; test('should remove star hack from plugins like lost', () => { return postcss([insertZoom(), stylehacks()]) From 28e2b4b836529f4d35beebf61c058ae2463c5317 Mon Sep 17 00:00:00 2001 From: Sigve Hoel <6737410+sigveio@users.noreply.github.com> Date: Wed, 7 Jul 2021 21:20:47 +0200 Subject: [PATCH 2/4] test(cssnano): ignore test helpers in coverage rep --- jest.config.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jest.config.js b/jest.config.js index dd0cbb03c..47ff0c8bf 100644 --- a/jest.config.js +++ b/jest.config.js @@ -4,6 +4,8 @@ module.exports = { 'packages/*/src/**/*.js', '!packages/postcss-colormin/src/generate.js', '!packages/postcss-reduce-initial/src/acquire.js', + '!packages/cssnano/src/__tests__/_processCss.js', + '!packages/cssnano/src/__tests__/_webpack.config.js', ], testPathIgnorePatterns: [ '/node_modules/', From 8d6e573e7c2327d5f1a82cb98842e73b371043b8 Mon Sep 17 00:00:00 2001 From: Sigve Hoel <6737410+sigveio@users.noreply.github.com> Date: Thu, 8 Jul 2021 13:32:16 +0200 Subject: [PATCH 3/4] test(cssnano-utils): suppress plugin warning --- .../src/__tests__/sameParent.test.js | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/packages/cssnano-utils/src/__tests__/sameParent.test.js b/packages/cssnano-utils/src/__tests__/sameParent.test.js index 7e6cbd8e4..e5c42a17b 100644 --- a/packages/cssnano-utils/src/__tests__/sameParent.test.js +++ b/packages/cssnano-utils/src/__tests__/sameParent.test.js @@ -1,16 +1,9 @@ import postcss from 'postcss'; import sameParent from '../sameParent'; -const plugin = () => { - return { - postcssPlugin: 'dummy', - }; -}; -plugin.postcss = true; - test('should calculate same parent', () => { - return postcss(plugin) - .process('h1 {} h2 {}', { from: undefined }) + return postcss() + .process('h1 {} h2 {}', { from: undefined, hideNothingWarning: true }) .then((result) => { const h1 = result.root.nodes[0]; const h2 = result.root.nodes[1]; @@ -20,8 +13,8 @@ test('should calculate same parent', () => { }); test('should calculate same parent (detached nodes)', () => { - return postcss(plugin) - .process('h1 {} h2 {}', { from: undefined }) + return postcss() + .process('h1 {} h2 {}', { from: undefined, hideNothingWarning: true }) .then((result) => { const h1 = result.root.nodes[0]; const h2 = result.root.nodes[1]; @@ -34,8 +27,11 @@ test('should calculate same parent (detached nodes)', () => { }); test('should calculate same parent (at rules)', () => { - return postcss(plugin) - .process('@media screen{h1 {} h2 {}}', { from: undefined }) + return postcss() + .process('@media screen{h1 {} h2 {}}', { + from: undefined, + hideNothingWarning: true, + }) .then((result) => { const h1 = result.root.nodes[0].nodes[0]; const h2 = result.root.nodes[0].nodes[1]; @@ -45,8 +41,11 @@ test('should calculate same parent (at rules)', () => { }); test('should calculate same parent (multiple at rules)', () => { - return postcss(plugin) - .process('@media screen{h1 {}} @media screen{h2 {}}', { from: undefined }) + return postcss() + .process('@media screen{h1 {}} @media screen{h2 {}}', { + from: undefined, + hideNothingWarning: true, + }) .then((result) => { const h1 = result.root.nodes[0].nodes[0]; const h2 = result.root.nodes[1].nodes[0]; @@ -56,8 +55,11 @@ test('should calculate same parent (multiple at rules)', () => { }); test('should calculate same parent (multiple at rules (uppercase))', () => { - return postcss(plugin) - .process('@media screen{h1 {}} @MEDIA screen{h2 {}}', { from: undefined }) + return postcss() + .process('@media screen{h1 {}} @MEDIA screen{h2 {}}', { + from: undefined, + hideNothingWarning: true, + }) .then((result) => { const h1 = result.root.nodes[0].nodes[0]; const h2 = result.root.nodes[1].nodes[0]; @@ -67,7 +69,7 @@ test('should calculate same parent (multiple at rules (uppercase))', () => { }); test('should calculate same parent (nested at rules)', () => { - return postcss(plugin) + return postcss() .process( ` @media screen { @@ -81,7 +83,7 @@ test('should calculate same parent (nested at rules)', () => { } } `, - { from: undefined } + { from: undefined, hideNothingWarning: true } ) .then((result) => { const h1 = result.root.nodes[0].nodes[0].nodes[0]; @@ -92,7 +94,7 @@ test('should calculate same parent (nested at rules)', () => { }); test('should calculate not same parent (nested at rules)', () => { - return postcss(plugin) + return postcss() .process( ` @media screen { @@ -106,7 +108,7 @@ test('should calculate not same parent (nested at rules)', () => { } } `, - { from: undefined } + { from: undefined, hideNothingWarning: true } ) .then((result) => { const h1 = result.root.nodes[0].nodes[0].nodes[0]; @@ -117,7 +119,7 @@ test('should calculate not same parent (nested at rules)', () => { }); test('should calculate not same parent (nested at rules) (2)', () => { - return postcss(plugin) + return postcss() .process( ` @media print { @@ -131,7 +133,7 @@ test('should calculate not same parent (nested at rules) (2)', () => { } } `, - { from: undefined } + { from: undefined, hideNothingWarning: true } ) .then((result) => { const h1 = result.root.nodes[0].nodes[0].nodes[0]; @@ -142,7 +144,7 @@ test('should calculate not same parent (nested at rules) (2)', () => { }); test('should calculate not same parent (nested at rules) (3)', () => { - return postcss(plugin) + return postcss() .process( ` @supports(pointer: course) { @@ -154,7 +156,7 @@ test('should calculate not same parent (nested at rules) (3)', () => { } } `, - { from: undefined } + { from: undefined, hideNothingWarning: true } ) .then((result) => { const h1 = result.root.nodes[0].nodes[0]; @@ -165,7 +167,7 @@ test('should calculate not same parent (nested at rules) (3)', () => { }); test('should calculate not same parent (nested at rules) (4)', () => { - return postcss(plugin) + return postcss() .process( ` @media screen { @@ -177,7 +179,7 @@ test('should calculate not same parent (nested at rules) (4)', () => { } } `, - { from: undefined } + { from: undefined, hideNothingWarning: true } ) .then((result) => { const h1 = result.root.nodes[0].nodes[0]; From 2c9e251f0b97c96990e994bd30facbdc61b9c394 Mon Sep 17 00:00:00 2001 From: Sigve Hoel <6737410+sigveio@users.noreply.github.com> Date: Thu, 8 Jul 2021 14:08:44 +0200 Subject: [PATCH 4/4] test(cssnano): leave a test for old plugin syntax --- packages/cssnano/src/__tests__/issue927.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/cssnano/src/__tests__/issue927.js b/packages/cssnano/src/__tests__/issue927.js index 7ee7dc1c8..1d187f9aa 100644 --- a/packages/cssnano/src/__tests__/issue927.js +++ b/packages/cssnano/src/__tests__/issue927.js @@ -12,7 +12,25 @@ p{ const expected = `div{grid-column:span 2}p{column-count:2}`; -test('it should compress the columns', () => { +test('it should compress the columns (old plugin syntax)', () => { + const processor = postcss([ + postcss.plugin('cloner', () => { + return (css) => { + css.walkAtRules((rule) => { + css.prepend(rule.clone()); + rule.remove(); + }); + }; + }), + nano(), + ]); + + return processor + .process(fixture, { from: undefined }) + .then((r) => expect(r.css).toBe(expected)); +}); + +test('it should compress the columns (new plugin syntax)', () => { const plugin = () => { return { postcssPlugin: 'cloner',