webpack-integration.test.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* eslint-disable import/no-dynamic-require, global-require */
  2. import fs from 'fs';
  3. import path from 'path';
  4. import webpack from 'webpack';
  5. import ExtractTextPlugin from 'extract-text-webpack-plugin';
  6. import OptimizeCssAssetsPlugin from '../src/';
  7. import { readFileOrEmpty, defaultConfig, checkForWebpackErrors } from './util/helpers';
  8. const cases = process.env.CASES ? process.env.CASES.split(',') : fs.readdirSync(path.join(__dirname, 'cases'));
  9. describe('Webpack Integration Tests', () => {
  10. cases.forEach((testCase) => {
  11. if (/^_skip_/.test(testCase)) return;
  12. it(testCase, (done) => {
  13. const testDirectory = path.join(__dirname, 'cases', testCase);
  14. const outputDirectory = path.join(__dirname, 'js', testCase);
  15. const expectedDirectory = path.join(testDirectory, 'expected');
  16. const configFile = path.join(testDirectory, 'webpack.config.js');
  17. const config = Object.assign(
  18. fs.existsSync(configFile) ? require(configFile) : { entry: { test: './index.js' } },
  19. {
  20. context: testDirectory,
  21. output: {
  22. filename: '[name].js',
  23. path: outputDirectory
  24. }
  25. }
  26. );
  27. webpack(config, (err, stats) => {
  28. checkForWebpackErrors({ err, stats, done });
  29. fs.readdirSync(expectedDirectory).forEach((file) => {
  30. const expectedFile = readFileOrEmpty(path.join(expectedDirectory, file));
  31. const actualFile = readFileOrEmpty(path.join(outputDirectory, file));
  32. expect(actualFile).toEqual(expectedFile);
  33. expect(actualFile).toMatchSnapshot();
  34. });
  35. done();
  36. });
  37. });
  38. });
  39. it('calls cssProcessor with correct arguments', (done) => {
  40. const destination = 'destination.css';
  41. const expectedCss = readFileOrEmpty(__dirname + '/util/default.css');
  42. const cssProcessorOptions = { discardComments: { removeAll: true } };
  43. const cssProcessor = {
  44. process: (actualCss, options) => {
  45. expect(options).toEqual(expect.objectContaining(cssProcessorOptions));
  46. expect(actualCss).toEqual(expectedCss);
  47. return Promise.resolve({ css: actualCss });
  48. }
  49. };
  50. const plugin = new OptimizeCssAssetsPlugin({ cssProcessor, cssProcessorOptions });
  51. const config = Object.assign(defaultConfig, {plugins: [plugin, new ExtractTextPlugin(destination)]});
  52. webpack(config, (err, stats) => {
  53. checkForWebpackErrors({ err, stats, done });
  54. done();
  55. });
  56. });
  57. it('writes processed css to destination', (done) => {
  58. const destination = 'destination.css';
  59. const expectedCss = '.inifinity-pool{overflow:hidden;}';
  60. const fakeCssProcessor = {
  61. process: jest.fn().mockReturnValue(Promise.resolve({ css: expectedCss }))
  62. };
  63. const plugin = new OptimizeCssAssetsPlugin({ cssProcessor: fakeCssProcessor });
  64. const config = Object.assign(defaultConfig, {plugins: [plugin, new ExtractTextPlugin(destination)]});
  65. webpack(config, (err, stats) => {
  66. checkForWebpackErrors({ err, stats, done });
  67. const actualCss = readFileOrEmpty(__dirname + '/js/default-exports/destination.css');
  68. expect(fakeCssProcessor.process).toHaveBeenCalled();
  69. expect(actualCss).toEqual(expectedCss);
  70. done();
  71. });
  72. });
  73. });