Skip to content

Commit

Permalink
Improved test organization, finished unit merge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenewald committed Apr 27, 2023
1 parent a6bcdbd commit 94d298c
Show file tree
Hide file tree
Showing 24 changed files with 355 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test/unit/merging-tests/autoprefixer-options/autoprefixer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const cracoConfig = require('./craco.config');
const autoprefixer = require('autoprefixer');

describe('CRACO autoprefixer configuration', () => {
it('correctly applies custom autoprefixer options', () => {
const postcssPlugins = cracoConfig.style.postcss.plugins;
const autoprefixerPluginEntry = postcssPlugins.find(
(pluginEntry) => pluginEntry.plugin === autoprefixer
);

expect(autoprefixerPluginEntry).toBeDefined();
expect(autoprefixerPluginEntry.options.grid).toEqual('autoplace');
expect(autoprefixerPluginEntry.options.overrideBrowserslist).toEqual([
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 11',
]);
});

it('does not remove other PostCSS plugins', () => {
const postcssPlugins = cracoConfig.style.postcss.plugins;
const autoprefixerPluginEntry = postcssPlugins.find(
(pluginEntry) => pluginEntry.plugin === autoprefixer
);

const pluginCountWithoutAutoprefixer = postcssPlugins.length - (autoprefixerPluginEntry ? 1 : 0);
expect(pluginCountWithoutAutoprefixer).toBeGreaterThanOrEqual(0);
});
});
15 changes: 15 additions & 0 deletions test/unit/merging-tests/autoprefixer-options/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
style: {
postcss: {
plugins: [
{
plugin: require('autoprefixer'),
options: {
grid: 'autoplace',
overrideBrowserslist: ['>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 11'],
},
},
],
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const babelConfigMock = {
presets: ['@babel/preset-env', '@babel/preset-react'],
};

module.exports = babelConfigMock;

23 changes: 23 additions & 0 deletions test/unit/merging-tests/custom-babel-config/babel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const cracoConfig = require('./craco.config');
const babelConfigMock = require('./babel.config.mock');

describe('CRACO Babel configuration', () => {
it('correctly applies custom Babel presets', () => {
const babelConfig = cracoConfig.babel.loaderOptions;

// Test if the original Babel presets are present
expect(babelConfig.presets).toContain('@babel/preset-env');
expect(babelConfig.presets).toContain('@babel/preset-react');

// Test if the custom Babel preset is added
expect(babelConfig.presets).toContain('@babel/preset-typescript');
});

it('does not remove existing Babel presets', () => {
const babelConfig = cracoConfig.babel.loaderOptions;

// Test if the number of presets in the custom configuration is greater than or equal to the original configuration
expect(babelConfig.presets.length).toBeGreaterThanOrEqual(babelConfigMock.presets.length);
});
});

8 changes: 8 additions & 0 deletions test/unit/merging-tests/custom-babel-config/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
babel: {
loaderOptions: {
presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'],
},
},
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs');
const path = require('path');

function onPostBuild({ paths }) {
const pluginLogPath = path.join(__dirname, '..', 'plugin.log');
fs.writeFileSync(pluginLogPath, 'Plugin executed successfully');
}

module.exports = {
onPostBuild,
};
10 changes: 10 additions & 0 deletions test/unit/merging-tests/custom-craco-plugin/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const CracoPluginMock = require('./craco-plugin-mock');

module.exports = {
plugins: [
{
plugin: CracoPluginMock,
},
],
};

28 changes: 28 additions & 0 deletions test/unit/merging-tests/custom-craco-plugin/plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs');
const path = require('path');
const cracoConfig = require('./craco.config');
const CracoPluginMock = require('./craco-plugin-mock');

describe('CRACO custom plugin', () => {
const pluginLogPath = path.join(__dirname, 'plugin.log');

afterEach(() => {
if (fs.existsSync(pluginLogPath)) {
fs.unlinkSync(pluginLogPath);
}
});

it('correctly includes the custom plugin', () => {
const pluginEntry = cracoConfig.plugins.find(pluginEntry => pluginEntry.plugin === CracoPluginMock);
expect(pluginEntry).toBeDefined();
});

it('correctly executes the onPostBuild function of the custom plugin', () => {
const pluginEntry = cracoConfig.plugins.find(pluginEntry => pluginEntry.plugin === CracoPluginMock);

pluginEntry.plugin.onPostBuild({});

const logContent = fs.readFileSync(pluginLogPath, 'utf-8');
expect(logContent).toEqual('Plugin executed successfully');
});
});
6 changes: 6 additions & 0 deletions test/unit/merging-tests/custom-env-variables/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
env: {
MY_CUSTOM_ENV_VAR: 'custom-env-value',
},
};

39 changes: 39 additions & 0 deletions test/unit/merging-tests/custom-env-variables/env.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const cracoConfig = require('./craco.config');

describe('CRACO environment variables', () => {
it('correctly sets custom environment variables', () => {
const originalProcessEnv = { ...process.env };

// Apply the custom environment variables from the CRACO configuration
process.env = {
...process.env,
...cracoConfig.env,
};

// Test if the custom environment variable is set
expect(process.env.MY_CUSTOM_ENV_VAR).toEqual('custom-env-value');

// Restore the original process.env to avoid side effects
process.env = originalProcessEnv;
});

it('does not remove existing environment variables', () => {
const originalProcessEnv = { ...process.env };

// Apply the custom environment variables from the CRACO configuration
process.env = {
...process.env,
...cracoConfig.env,
};

// Test if the existing environment variables are not removed
const originalEnvVarCount = Object.keys(originalProcessEnv).length;
const newEnvVarCount = Object.keys(process.env).length;

expect(newEnvVarCount).toBeGreaterThanOrEqual(originalEnvVarCount);

// Restore the original process.env to avoid side effects
process.env = originalProcessEnv;
});
});

12 changes: 12 additions & 0 deletions test/unit/merging-tests/custom-eslint-config/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
eslint: {
configure: {
extends: ['react-app', 'plugin:prettier/recommended'],
rules: {
'no-console': 'error',
'no-debugger': 'error',
},
},
},
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const eslintConfigMock = {
extends: ['react-app'],
rules: {
'no-console': 'warn',
},
};

module.exports = eslintConfigMock;

25 changes: 25 additions & 0 deletions test/unit/merging-tests/custom-eslint-config/eslint.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const cracoConfig = require('./craco.config');
const eslintConfigMock = require('./eslint.config.mock');

describe('CRACO ESLint configuration', () => {
it('correctly applies custom ESLint configuration', () => {
const eslintConfig = cracoConfig.eslint.configure;

// Test if the original ESLint extends are present
expect(eslintConfig.extends).toContain('react-app');

// Test if the custom ESLint extends are added
expect(eslintConfig.extends).toContain('plugin:prettier/recommended');

// Test if the custom ESLint rules are applied
expect(eslintConfig.rules['no-console']).toEqual('error');
expect(eslintConfig.rules['no-debugger']).toEqual('error');
});

it('does not remove existing ESLint rules', () => {
const eslintConfig = cracoConfig.eslint.configure;

// Test if the number of rules in the custom configuration is greater than or equal to the original configuration
expect(Object.keys(eslintConfig.rules).length).toBeGreaterThanOrEqual(Object.keys(eslintConfigMock.rules).length);
});
});
13 changes: 13 additions & 0 deletions test/unit/merging-tests/custom-jest-config/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
jest: {
configure: {
transform: {
'^.+\\.[t|j]sx?$': 'babel-jest',
},
moduleNameMapper: {
'^@components/(.*)$': '<rootDir>/src/components/$1',
},
},
},
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const jestConfigMock = {
transform: {
'^.+\\.[t|j]sx?$': 'babel-jest',
},
};

module.exports = jestConfigMock;

22 changes: 22 additions & 0 deletions test/unit/merging-tests/custom-jest-config/jest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const cracoConfig = require('./craco.config');
const jestConfigMock = require('./jest.config.mock');

describe('CRACO Jest configuration', () => {
it('correctly applies custom Jest configuration', () => {
const jestConfig = cracoConfig.jest.configure;

// Test if the original Jest transform configuration is present
expect(jestConfig.transform['^.+\\.[t|j]sx?$']).toEqual('babel-jest');

// Test if the custom Jest moduleNameMapper configuration is added
expect(jestConfig.moduleNameMapper['^@components/(.*)$']).toEqual('<rootDir>/src/components/$1');
});

it('does not remove existing Jest configurations', () => {
const jestConfig = cracoConfig.jest.configure;

// Test if the number of configurations in the custom configuration is greater than or equal to the original configuration
expect(Object.keys(jestConfig).length).toBeGreaterThanOrEqual(Object.keys(jestConfigMock).length);
});
});

11 changes: 11 additions & 0 deletions test/unit/merging-tests/custom-postcss-config/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
style: {
postcss: {
plugins: [
require('autoprefixer'),
require('postcss-nested'),
],
},
},
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const postcssConfigMock = {
plugins: [
require('autoprefixer'),
],
};

module.exports = postcssConfigMock;

23 changes: 23 additions & 0 deletions test/unit/merging-tests/custom-postcss-config/postcss.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const cracoConfig = require('./craco.config');
const postcssConfigMock = require('./postcss.config.mock');


//Checks if a custom PostCSS configuration is correctly applied
describe('CRACO PostCSS configuration', () => {
it('correctly applies custom PostCSS plugins', () => {
const postcssConfig = cracoConfig.style.postcss;

// Test if the original PostCSS plugins are present
expect(postcssConfig.plugins).toContainEqual(require('autoprefixer'));

// Test if the custom PostCSS plugin is added
expect(postcssConfig.plugins).toContainEqual(require('postcss-nested'));
});

it('does not remove existing PostCSS plugins', () => {
const postcssConfig = cracoConfig.style.postcss;

// Test if the number of plugins in the custom configuration is greater than or equal to the original configuration
expect(postcssConfig.plugins.length).toBeGreaterThanOrEqual(postcssConfigMock.plugins.length);
});
});
25 changes: 25 additions & 0 deletions test/unit/merging-tests/html-webpack-plugin/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
webpack: {
configure: (webpackConfig, { env, paths }) => {
const HtmlWebpackPlugin = require('html-webpack-plugin');

// Find the HtmlWebpackPlugin in the plugins array
const htmlWebpackPluginIndex = webpackConfig.plugins.findIndex(
(plugin) => plugin instanceof HtmlWebpackPlugin
);

if (htmlWebpackPluginIndex >= 0) {
// Create a new HtmlWebpackPlugin instance with the custom title
const updatedHtmlWebpackPlugin = new HtmlWebpackPlugin({
...webpackConfig.plugins[htmlWebpackPluginIndex].userOptions,
title: 'My Custom Title',
});

// Replace the original HtmlWebpackPlugin instance with the updated one
webpackConfig.plugins.splice(htmlWebpackPluginIndex, 1, updatedHtmlWebpackPlugin);
}

return webpackConfig;
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const cracoConfig = require('./craco.config');

describe('CRACO HtmlWebpackPlugin configuration', () => {
it('correctly applies custom HtmlWebpackPlugin configuration', async () => {
const webpackConfig = {
mode: 'development',
plugins: [new HtmlWebpackPlugin()],
};

// Apply custom configuration to the webpack config
const newWebpackConfig = cracoConfig.webpack.configure(webpackConfig, {});

// Find the HtmlWebpackPlugin in the newWebpackConfig
const htmlWebpackPlugin = newWebpackConfig.plugins.find(
(plugin) => plugin instanceof HtmlWebpackPlugin
);

// Test if the custom title is set
expect(htmlWebpackPlugin.userOptions.title).toEqual('My Custom Title');
});
});

0 comments on commit 94d298c

Please sign in to comment.