docsify/test/config/server.js
John Hildenbiddle c49c39a4a2
refactor: Update test environments and lint configuration (#1736)
* Update test environments and lint configuration

Update Jest (unit + integration) and Playwright (e2e) test environments. Includes stability improvements for e2e tests using newer, more stable methods per the Playwright docs.

- Update Jest 26 => 27
- Update Jest-related libs (babel parser)
- Update Playwright 1.8 => Playwright Test 1.18
- Update GitHub CI (action versions, job parallelization, and matrices)
- Update ESLint 5 => 8
- Update ESLint-related libs (parser, prettier, Jest, Playwright)
- Fix test failures on M1-based Macs
- Fix e2e stability issues by replacing PW $ method calls
- Fix ESLint errors
- Fix incorrect CI flag on Jest runs (-ci => --ci)
- Refactor e2e test runner from Jest to Playwright Test
- Refactor e2e test files for Playwright Test
- Refactor fix-lint script name to lint:fix for consistency
- Refactor npm scripts order for readability
- Remove unnecessary configs and libs
- Remove example image snapshots
2022-01-30 21:40:21 -06:00

116 lines
2.8 KiB
JavaScript

const browserSync = require('browser-sync').create();
const path = require('path');
const hasStartArg = process.argv.includes('--start');
const serverConfig = {
hostname: '127.0.0.1',
port: hasStartArg ? 3002 : 3001,
};
function startServer(options = {}, cb = Function.prototype) {
const defaults = {
...serverConfig,
middleware: [
{
route: '/_blank.html',
handle: function (req, res, next) {
res.setHeader('Content-Type', 'text/html');
res.end('');
next();
},
},
],
notify: false,
open: false,
rewriteRules: [
// Replace docsify-related CDN URLs with local paths
{
match:
/(https?:)?\/\/cdn\.jsdelivr\.net\/npm\/docsify(@\d?\.?\d?\.?\d)?\/lib\//g,
replace: '/lib/',
},
],
server: {
baseDir: path.resolve(__dirname, '../'),
routes: {
'/docs': path.resolve(__dirname, '../../docs'),
'/docs/changelog.md': './CHANGELOG.md',
'/lib': path.resolve(__dirname, '../../lib'),
'/node_modules': path.resolve(__dirname, '../../node_modules'),
},
},
snippetOptions: {
rule: {
match: /<\/body>/i,
fn: function (snippet, match) {
// Override changelog alias to load local changelog (see routes)
const injectJS = `
<script>
// Fix /docs site configuration during tests
(function() {
const aliasConfig = (window && window.$docsify && window.$docsify.alias) || {};
aliasConfig['.*?/changelog'] = '/changelog.md';
})();
</script>
`;
return injectJS + snippet + match;
},
},
},
ui: false,
};
console.log('\n');
// Set TEST_HOST environment variable
process.env.TEST_HOST = `http://${serverConfig.hostname}:${serverConfig.port}`;
// Start server
browserSync.init(
// Config
{
...defaults,
...options,
},
// Callback
cb
);
}
async function startServerAsync() {
await new Promise((resolve, reject) => {
startServer({}, () => {
console.log('\n');
resolve();
});
});
}
function stopServer() {
browserSync.exit();
}
// Allow starting the test server from the CLI. Useful for viewing test content
// like fixtures (/index.html)) and local docs site (/docs) used for testing.
if (hasStartArg) {
startServer({
open: true,
port: serverConfig.port,
directory: true,
startPath: '/docs',
});
}
// Display friendly message about manually starting a server instance
else if (require.main === module) {
console.info('Use --start argument to manually start server instance');
}
module.exports = {
start: startServer,
startAsync: startServerAsync,
stop: stopServer,
TEST_HOST: `http://${serverConfig.hostname}:${serverConfig.port}`,
};