From 127ffd2a7467261d84f22cf74162e8942f9fdd6e Mon Sep 17 00:00:00 2001 From: Benjamin Kinga <58235869+thescripted@users.noreply.github.com> Date: Tue, 27 Oct 2020 13:33:28 -0500 Subject: [PATCH] Added Minimal Tests for plugin-postcss (#1357) * initial commit * removed unnecessary mocks * removed snapshots & mocked execa * removed console logs --- plugins/plugin-postcss/test/plugin.test.js | 46 +++++++++++++++++++ .../test/stubs/postcss.config.js | 7 +++ plugins/plugin-postcss/test/stubs/style.css | 16 +++++++ 3 files changed, 69 insertions(+) create mode 100644 plugins/plugin-postcss/test/plugin.test.js create mode 100644 plugins/plugin-postcss/test/stubs/postcss.config.js create mode 100644 plugins/plugin-postcss/test/stubs/style.css diff --git a/plugins/plugin-postcss/test/plugin.test.js b/plugins/plugin-postcss/test/plugin.test.js new file mode 100644 index 0000000000..160836aae9 --- /dev/null +++ b/plugins/plugin-postcss/test/plugin.test.js @@ -0,0 +1,46 @@ +const fs = require('fs'); +const path = require('path'); +const plugin = require("../plugin.js"); + +const cssPath = path.resolve(__dirname, './stubs/style.css'); +const cssContent = fs.readFileSync(cssPath, 'utf-8'); +const configFilePath = path.resolve(__dirname, './stubs/postcss.config.js'); + +jest.mock('execa') +const execa = require('execa'); + +describe('@snowpack/plugin-postcss', () => { + beforeEach(() => { + execa.mockClear(); + execaFn = jest.fn(() => { + return { + stdout: "stdout", + stderr: "stderr" + } + }); + execa.mockImplementation(execaFn) + }); + + test('with no options', async () => { + const pluginInstance = plugin({}, {}); + const transformCSSResults = await pluginInstance.transform({ + fileExt: path.extname(cssPath), + contents: cssContent + }); + expect(execa.mock.calls[0]).toContain('postcss') + expect(execa.mock.calls[0][1]).not.toEqual([`--config ${configFilePath}`]) + }); + + test('passing in a config file', async () => { + const options = { + config: path.resolve(configFilePath) + } + const pluginInstance = plugin({}, options) + const transformCSSResults = await pluginInstance.transform({ + fileExt: path.extname(cssPath), + contents: cssContent + }); + expect(execa.mock.calls[0]).toContain('postcss') + expect(execa.mock.calls[0][1]).toEqual([`--config ${configFilePath}`]) + }); +}); diff --git a/plugins/plugin-postcss/test/stubs/postcss.config.js b/plugins/plugin-postcss/test/stubs/postcss.config.js new file mode 100644 index 0000000000..ffb2269ac8 --- /dev/null +++ b/plugins/plugin-postcss/test/stubs/postcss.config.js @@ -0,0 +1,7 @@ +module.exports = { + plugins: [ + require('cssnano')({ + preset: 'default', + }), + ], +}; diff --git a/plugins/plugin-postcss/test/stubs/style.css b/plugins/plugin-postcss/test/stubs/style.css new file mode 100644 index 0000000000..3a7d04ef1b --- /dev/null +++ b/plugins/plugin-postcss/test/stubs/style.css @@ -0,0 +1,16 @@ +body { + background: #fff; +} + +#root { + width: 480px; + margin: auto; + padding: 16px; + box-sizing: border-box; +} + +.content { + color: blue; +} + +