forked from dilanx/craco
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved testing structure, fixed integrations
- Loading branch information
1 parent
94d298c
commit 820cd93
Showing
11 changed files
with
15,587 additions
and
12,542 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
test/integration/fixtures/custom-postcss-config/craco.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const webpack = require('webpack'); | ||
const isDevelopment = false; | ||
module.exports = { | ||
webpack: { | ||
configure: (webpackConfig) => { | ||
if (!isDevelopment) { | ||
const reactRefreshPluginIndex = webpackConfig.plugins.findIndex( | ||
(plugin) => plugin.constructor.name === 'ReactRefreshPlugin' | ||
); | ||
|
||
if (reactRefreshPluginIndex !== -1) { | ||
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 reactRefreshBabelIndex = babelLoader.options.plugins.findIndex( | ||
(plugin) => plugin && plugin.includes && plugin.includes('react-refresh/babel') | ||
); | ||
|
||
if (reactRefreshBabelIndex !== -1) { | ||
babelLoader.options.plugins.splice(reactRefreshBabelIndex, 1); | ||
} | ||
} | ||
|
||
webpackConfig.plugins.push( | ||
new webpack.DefinePlugin({ | ||
__CUSTOM_GLOBAL_CONSTANT__: JSON.stringify("CRACO is working!"), | ||
}) | ||
); | ||
|
||
return webpackConfig; | ||
}, | ||
}, | ||
}; | ||
|
13 changes: 13 additions & 0 deletions
13
test/integration/fixtures/custom-postcss-config/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>React App</title> | ||
<link rel="stylesheet" href="index.css" /> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="index.js"></script> | ||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* global __CUSTOM_GLOBAL_CONSTANT__ */ | ||
import React from "react"; | ||
import ReactDOM from 'react-dom'; | ||
import './index.css'; | ||
|
||
ReactDOM.render( | ||
<React.StrictMode> | ||
<div className="container"> | ||
<h1>Testing CRACO</h1> | ||
<p id="craco-test">{__CUSTOM_GLOBAL_CONSTANT__}</p> | ||
</div> | ||
</React.StrictMode>, | ||
document.getElementById('root') | ||
); |
68 changes: 68 additions & 0 deletions
68
test/integration/fixtures/custom-postcss-config/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
const { join } = require('path'); | ||
const { readdirSync, readFileSync } = require('fs'); | ||
const { execSync, spawn } = require('child_process'); | ||
|
||
const PORT = 3009; | ||
const URL = `http://localhost:${PORT}/`; | ||
|
||
beforeAll(async () => { | ||
process.env.NODE_ENV = 'production'; | ||
// Create a test project using Create React App and CRACO | ||
execSync('npx create-react-app test-project', { | ||
cwd: __dirname, | ||
}); | ||
execSync('cd test-project && npm install', { cwd: __dirname }); | ||
// Copy the necessary files to the test project directory | ||
const cracoConfigPath = join(__dirname, 'craco.config.js'); | ||
const indexPath = join(__dirname, 'index.js'); | ||
const htmlPath = join(__dirname, 'index.html'); | ||
const packageJsonPath = join(__dirname, 'package.json'); | ||
const cracoConfigDestPath = join(__dirname, 'test-project', 'craco.config.js'); | ||
const indexDestPath = join(__dirname, 'test-project', 'src', 'index.js'); | ||
const htmlDestPath = join(__dirname, 'test-project', 'public', 'index.html'); | ||
const packageJsonDestPath = join(__dirname, 'test-project', 'package.json'); | ||
execSync(`cp ${cracoConfigPath} ${cracoConfigDestPath}`, { cwd: __dirname }); | ||
execSync(`cp ${indexPath} ${indexDestPath}`, { cwd: __dirname }); | ||
execSync(`cp ${htmlPath} ${htmlDestPath}`, { cwd: __dirname }); | ||
execSync(`cp ${packageJsonPath} ${packageJsonDestPath}`, { cwd: __dirname }); | ||
|
||
await new Promise(resolve => {setTimeout(resolve, 1000)}); | ||
|
||
execSync('npm install ../../../../../packages/craco', { cwd: join(__dirname, 'test-project') }); | ||
|
||
// Build the test project | ||
execSync('npm run build', { cwd: join(__dirname, 'test-project'), stdio: 'inherit' }); | ||
|
||
// Start a local server to serve the test project | ||
const server = spawn('npx', ['serve', '-s', 'build', '-l', PORT], { | ||
cwd: join(__dirname, 'test-project'), | ||
}); | ||
|
||
// Log any server errors to the console | ||
server.stderr.on('data', (data) => { | ||
console.error(`stderr: ${data}`); | ||
}); | ||
|
||
// Leave time for the server to initialize | ||
await new Promise(resolve => {setTimeout(resolve, 3000)}); | ||
|
||
// Go to the locally hosted site | ||
await page.goto(URL); | ||
}, 60000); | ||
|
||
afterAll(() => { | ||
// Stop the local server | ||
execSync(`kill $(lsof -t -i:${PORT})`, { cwd: __dirname, stdio: 'ignore' }); | ||
|
||
// Delete the test project | ||
execSync('rm -rf test-project', { cwd: __dirname }); | ||
}); | ||
|
||
test("Should have the expected styles", async () => { | ||
await page.goto(URL, {'waitUntil':'domcontentloaded'}); | ||
|
||
const cracoTestText = await page.$eval('#craco-test', (element) => element.textContent); | ||
expect(cracoTestText).toBe("CRACO is working!"); | ||
|
||
}); |
Oops, something went wrong.