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; +} + +