Skip to content

Commit

Permalink
Cleaned up test directory with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenewald committed Apr 30, 2023
1 parent 4d81e73 commit a992bde
Show file tree
Hide file tree
Showing 28 changed files with 106 additions and 69 deletions.
2 changes: 1 addition & 1 deletion test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ This will run the `test` command and return the `stdout` and `stderr` of the pro
##### `serve`

This will run serve the application.
It will return the `port` and a `done` function to clean up the process.
It will return the `port` and a `done` function to clean up the process.
22 changes: 15 additions & 7 deletions test/integration/fixtures/basic-integration-test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ beforeAll(async () => {
});

// Leave time for the server to initialize
await new Promise(resolve => {setTimeout(resolve, 3000)});
await new Promise((resolve) => {
setTimeout(resolve, 3000);
});
});

test("Should have the expected styles", async () => {
await page.goto(global.URL, {'waitUntil':'domcontentloaded'}); //todo: make the url changeble
test('Should have the expected styles', async () => {
await page.goto(global.URL, { waitUntil: 'domcontentloaded' }); //todo: make the url changeble

const cracoTestText = await page.$eval('#craco-test', (element) => element.textContent);
expect(cracoTestText).toBe("CRACO is working!");
const cracoTestText = await page.$eval(
'#craco-test',
(element) => element.textContent
);
expect(cracoTestText).toBe('CRACO is working!');
});

afterAll(() => {
// Stop the local server
execSync(`kill $(lsof -t -i:${global.PORT})`, { cwd: __dirname, stdio: 'ignore' });
});
execSync(`kill $(lsof -t -i:${global.PORT})`, {
cwd: __dirname,
stdio: 'ignore',
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const webpack = require('webpack');
const isDevelopment = false;
const isDevelopment = false;
module.exports = {
webpack: {
configure: (webpackConfig) => {
Expand All @@ -12,12 +12,19 @@ module.exports = {
webpackConfig.plugins.splice(reactRefreshPluginIndex, 1);
}

const babelLoader = webpackConfig.module.rules.find(
(rule) => rule.oneOf && rule.oneOf.find((r) => r.loader && r.loader.includes('babel-loader'))
).oneOf.find((r) => r.loader && r.loader.includes('babel-loader'));
const babelLoader = webpackConfig.module.rules
.find(
(rule) =>
rule.oneOf &&
rule.oneOf.find(
(r) => r.loader && r.loader.includes('babel-loader')
)
)
.oneOf.find((r) => r.loader && r.loader.includes('babel-loader'));

const reactRefreshBabelIndex = babelLoader.options.plugins.findIndex(
(plugin) => plugin && plugin.includes && plugin.includes('react-refresh/babel')
(plugin) =>
plugin && plugin.includes && plugin.includes('react-refresh/babel')
);

if (reactRefreshBabelIndex !== -1) {
Expand All @@ -27,12 +34,11 @@ module.exports = {

webpackConfig.plugins.push(
new webpack.DefinePlugin({
__CUSTOM_GLOBAL_CONSTANT__: JSON.stringify("CRACO is working!"),
__CUSTOM_GLOBAL_CONSTANT__: JSON.stringify('CRACO is working!'),
})
);

return webpackConfig;
},
},
};

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@
<script src="index.js"></script>
</body>
</html>

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global __CUSTOM_GLOBAL_CONSTANT__ */
import React from "react";
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

Expand Down
6 changes: 3 additions & 3 deletions test/integration/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ module.exports = {
moduleNameMapper: {
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
},
setupFiles: ["./setup.js"],
globalTeardown: "./teardown.js",
setupFiles: ['./setup.js'],
globalTeardown: './teardown.js',
testEnvironmentOptions: {
'jest-playwright': {
browsers: ['firefox'],
launchOptions: {
headless: true
headless: true,
},
},
},
Expand Down
32 changes: 23 additions & 9 deletions test/integration/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,37 @@ module.exports = async function (globalConfig, projectConfig) {
console.error(`Error reading directory: ${err.message}`);
return;
}

const directoryNames = entries
.filter(entry => entry.isDirectory())
.map(entry => entry.name);
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);

directoryNames.forEach(directoryName => {
directoryNames.forEach((directoryName) => {
//copy files in directory/test-package-files to directory/test-project
const testPackageFilesPath = join(rootPath, directoryName, 'test-package-files');
const testPackageFilesPath = join(
rootPath,
directoryName,
'test-package-files'
);
const testProjectPath = join(rootPath, directoryName, 'test-project');
execSync(`cd ${join(rootPath, directoryName)}&& npx create-react-app test-project`, { cwd: cwd });
execSync(`cp -r ${testPackageFilesPath}/* ${testProjectPath}`, { cwd: cwd });
execSync(
`cd ${join(
rootPath,
directoryName
)}&& npx create-react-app test-project`,
{ cwd: cwd }
);
execSync(`cp -r ${testPackageFilesPath}/* ${testProjectPath}`, {
cwd: cwd,
});
//install craco
execSync(`npm install ../../../../../packages/craco`, { cwd: testProjectPath });
execSync(`npm install ../../../../../packages/craco`, {
cwd: testProjectPath,
});
//install other necessary files
execSync(`npm install`, { cwd: testProjectPath });
//build project
execSync('npm run build', { cwd: testProjectPath, stdio: 'inherit' });
});
});
}
};
15 changes: 8 additions & 7 deletions test/integration/teardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ const rootPath = 'test/integration/fixtures';
const cwd = process.cwd();

module.exports = async () => {
console.log('Running teardown script to clean up test environment...');
fs.readdir(rootPath, { withFileTypes: true }, (err, entries) => {
if (err) {
console.error(`Error reading directory: ${err.message}`);
return;
}

const directoryNames = entries
.filter(entry => entry.isDirectory())
.map(entry => entry.name);
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);

directoryNames.forEach(directoryName => {
directoryNames.forEach((directoryName) => {
//clean up test-project
execSync(`rm -rf ${join(rootPath, directoryName, 'test-project')}`, { cwd: cwd });
execSync(`rm -rf ${join(rootPath, directoryName, 'test-project')}`, {
cwd: cwd,
});
});
});
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe('CRACO autoprefixer configuration', () => {
(pluginEntry) => pluginEntry.plugin === autoprefixer
);

const pluginCountWithoutAutoprefixer = postcssPlugins.length - (autoprefixerPluginEntry ? 1 : 0);
const pluginCountWithoutAutoprefixer =
postcssPlugins.length - (autoprefixerPluginEntry ? 1 : 0);
expect(pluginCountWithoutAutoprefixer).toBeGreaterThanOrEqual(0);
});
});
7 changes: 6 additions & 1 deletion test/unit/merging-tests/autoprefixer-options/craco.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ module.exports = {
plugin: require('autoprefixer'),
options: {
grid: 'autoplace',
overrideBrowserslist: ['>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 11'],
overrideBrowserslist: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 11',
],
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ const craConfigMock = {
},
};

module.exports = craConfigMock;
module.exports = craConfigMock;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ const babelConfigMock = {
};

module.exports = babelConfigMock;

5 changes: 3 additions & 2 deletions test/unit/merging-tests/custom-babel-config/babel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('CRACO Babel configuration', () => {
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);
expect(babelConfig.presets.length).toBeGreaterThanOrEqual(
babelConfigMock.presets.length
);
});
});

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

Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ module.exports = {
},
],
};

8 changes: 6 additions & 2 deletions test/unit/merging-tests/custom-craco-plugin/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ describe('CRACO custom plugin', () => {
});

it('correctly includes the custom plugin', () => {
const pluginEntry = cracoConfig.plugins.find(pluginEntry => pluginEntry.plugin === CracoPluginMock);
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);
const pluginEntry = cracoConfig.plugins.find(
(pluginEntry) => pluginEntry.plugin === CracoPluginMock
);

pluginEntry.plugin.onPostBuild({});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ module.exports = {
MY_CUSTOM_ENV_VAR: 'custom-env-value',
},
};

1 change: 0 additions & 1 deletion test/unit/merging-tests/custom-env-variables/env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,3 @@ describe('CRACO environment variables', () => {
process.env = originalProcessEnv;
});
});

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ module.exports = {
},
},
};

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ const eslintConfigMock = {
};

module.exports = eslintConfigMock;

4 changes: 3 additions & 1 deletion test/unit/merging-tests/custom-eslint-config/eslint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ describe('CRACO ESLint configuration', () => {
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);
expect(Object.keys(eslintConfig.rules).length).toBeGreaterThanOrEqual(
Object.keys(eslintConfigMock.rules).length
);
});
});
1 change: 0 additions & 1 deletion test/unit/merging-tests/custom-jest-config/craco.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ module.exports = {
},
},
};

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ const jestConfigMock = {
};

module.exports = jestConfigMock;

9 changes: 6 additions & 3 deletions test/unit/merging-tests/custom-jest-config/jest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ describe('CRACO Jest configuration', () => {
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');
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);
expect(Object.keys(jestConfig).length).toBeGreaterThanOrEqual(
Object.keys(jestConfigMock).length
);
});
});

Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
module.exports = {
style: {
postcss: {
plugins: [
require('autoprefixer'),
require('postcss-nested'),
],
plugins: [require('autoprefixer'), require('postcss-nested')],
},
},
};

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

module.exports = postcssConfigMock;

5 changes: 3 additions & 2 deletions test/unit/merging-tests/custom-postcss-config/postcss.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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', () => {
Expand All @@ -18,6 +17,8 @@ describe('CRACO PostCSS configuration', () => {
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);
expect(postcssConfig.plugins.length).toBeGreaterThanOrEqual(
postcssConfigMock.plugins.length
);
});
});
6 changes: 5 additions & 1 deletion test/unit/merging-tests/html-webpack-plugin/craco.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ module.exports = {
});

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

return webpackConfig;
Expand Down

0 comments on commit a992bde

Please sign in to comment.