Skip to content

Commit

Permalink
test: Update test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Jan 19, 2024
1 parent 6c22450 commit 8f09b5b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 100 deletions.
56 changes: 27 additions & 29 deletions packages/cli/tests/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
writeFile,
} = require('fs/promises');
const looksLike = require('html-looks-like');
const { create, build, buildFast } = require('./lib/cli');
const { create, build } = require('./lib/cli');
const { snapshot } = require('./lib/utils');
const { subject } = require('./lib/output');
const images = require('./images/build');
Expand Down Expand Up @@ -80,25 +80,25 @@ describe('preact build', () => {
it('builds the `typescript` template', async () => {
let dir = await create('typescript');

await expect(buildFast(dir)).resolves.not.toThrow();
await expect(build(dir)).resolves.not.toThrow();
});

it('should patch global location object', async () => {
let dir = await subject('location-patch');

await expect(buildFast(dir)).resolves.not.toThrow();
await expect(build(dir)).resolves.not.toThrow();
});

it('should copy resources from static to build directory', async () => {
let dir = await subject('static-root');
await buildFast(dir);
await build(dir);
let file = join(dir, 'build', '.htaccess');
expect(await access(file)).toBeUndefined();
});

it('should use a custom `.env` with prefixed environment variables', async () => {
let dir = await subject('custom-dotenv');
await buildFast(dir);
await build(dir);

const bundleFile = (await readdir(`${dir}/build`)).find(file =>
/bundle\.\w{5}\.js$/.test(file)
Expand Down Expand Up @@ -131,15 +131,13 @@ describe('preact build', () => {
await rename(join(dir, 'index.js'), join(dir, 'renamed-src/index.js'));
await rename(join(dir, 'style.css'), join(dir, 'renamed-src/style.css'));

await expect(
buildFast(dir, { src: 'renamed-src' })
).resolves.not.toThrow();
await expect(build(dir, { src: 'renamed-src' })).resolves.not.toThrow();
});

it('--dest', async () => {
let dir = await subject('minimal');

await buildFast(dir, { dest: 'renamed-dest' });
await build(dir, { dest: 'renamed-dest' });
expect(await access(join(dir, 'renamed-dest'))).toBeUndefined();
});

Expand All @@ -148,13 +146,13 @@ describe('preact build', () => {

const logSpy = jest.spyOn(process.stdout, 'write');

await buildFast(dir, { sw: true });
await build(dir, { sw: true });
expect(await access(join(dir, 'build', 'sw.js'))).toBeUndefined();
expect(logSpy).toHaveBeenCalledWith(
expect.stringContaining('Could not find sw.js')
);

await buildFast(dir, { sw: false });
await build(dir, { sw: false });
await expect(access(join(dir, 'build', 'sw.js'))).rejects.toThrow(
'no such file or directory'
);
Expand All @@ -165,12 +163,12 @@ describe('preact build', () => {
// Prerendering is disabled to avoid (non-relevant) regenerator issues
let dir = await subject('custom-babelrc');

await buildFast(dir, { prerender: false });
await build(dir, { prerender: false });
let transpiledChunk = await getOutputFile(dir, /bundle\.\w{5}\.js$/);
expect(/=>\s?setTimeout/.test(transpiledChunk)).toBe(false);

await rename(join(dir, '.babelrc'), join(dir, 'babel.config.json'));
await buildFast(dir, {
await build(dir, {
babelConfig: 'babel.config.json',
prerender: false,
});
Expand All @@ -185,7 +183,7 @@ describe('preact build', () => {
join(dir, 'template.ejs'),
join(dir, 'renamed-template.ejs')
);
await buildFast(dir, { template: 'renamed-template.ejs' });
await build(dir, { template: 'renamed-template.ejs' });

const html = await getOutputFile(dir, 'index.html');
expect(html).toEqual(
Expand All @@ -196,19 +194,19 @@ describe('preact build', () => {
it('--prerender', async () => {
let dir = await subject('minimal');

await buildFast(dir, { prerender: true });
await build(dir, { prerender: true });
let html = await getOutputFile(dir, 'index.html');
expect(html).toMatch('<h1>Minimal App</h1>');

await buildFast(dir, { prerender: false });
await build(dir, { prerender: false });
html = await getOutputFile(dir, 'index.html');
expect(html).not.toMatch('<h1>Minimal App</h1>');
});

it('--prerenderUrls', async () => {
let dir = await subject('multiple-prerendering');

await buildFast(dir, { prerenderUrls: 'prerender-urls.json' });
await build(dir, { prerenderUrls: 'prerender-urls.json' });
expect(await access(join(dir, 'build/index.html'))).toBeUndefined();
expect(
await access(join(dir, 'build/route66/index.html'))
Expand All @@ -221,7 +219,7 @@ describe('preact build', () => {
join(dir, 'prerender-urls.json'),
join(dir, 'renamed-urls.json')
);
await buildFast(dir, { prerenderUrls: 'renamed-urls.json' });
await build(dir, { prerenderUrls: 'renamed-urls.json' });
expect(await access(join(dir, 'build/index.html'))).toBeUndefined();
expect(
await access(join(dir, 'build/route66/index.html'))
Expand All @@ -234,26 +232,26 @@ describe('preact build', () => {
it('--inlineCss', async () => {
let dir = await subject('minimal');

await buildFast(dir, { inlineCss: true });
await build(dir, { inlineCss: true });
let head = await getHead(dir);
expect(head).toMatch('<style>h1{color:red}</style>');

await buildFast(dir, { inlineCss: false });
await build(dir, { inlineCss: false });
head = await getOutputFile(dir, 'index.html');
expect(head).not.toMatch(/<style>[^<]*<\/style>/);
});

it('--config', async () => {
let dir = await subject('custom-webpack');

await buildFast(dir, { config: 'preact.config.js' });
await build(dir, { config: 'preact.config.js' });
expect(await access(join(dir, 'build/bundle.js'))).toBeUndefined();

await rename(
join(dir, 'preact.config.js'),
join(dir, 'renamed-config.js')
);
await buildFast(dir, { config: 'renamed-config.js' });
await build(dir, { config: 'renamed-config.js' });
expect(await access(join(dir, 'build/bundle.js'))).toBeUndefined();
});

Expand All @@ -278,7 +276,7 @@ describe('preact build', () => {
'h2{color:green}'
);

await buildFast(dir);
await build(dir);
const builtStylesheet = await getOutputFile(dir, /bundle\.\w{5}\.css$/);

expect(builtStylesheet).toMatch('h1{color:red}');
Expand All @@ -289,7 +287,7 @@ describe('preact build', () => {

it('should use plain CSS & CSS Modules together, determining loading method by filename', async () => {
let dir = await subject('css-modules');
await buildFast(dir);
await build(dir);
const builtStylesheet = await getOutputFile(dir, /bundle\.\w{5}\.css$/);

expect(builtStylesheet).toMatch('h1{color:red}');
Expand All @@ -298,7 +296,7 @@ describe('preact build', () => {

it('should inline critical CSS only', async () => {
let dir = await subject('css-inline');
await buildFast(dir);
await build(dir);
const builtStylesheet = await getOutputFile(dir, /bundle\.\w{5}\.css$/);
const html = await getOutputFile(dir, 'index.html');

Expand All @@ -309,15 +307,15 @@ describe('preact build', () => {
// Issue #1411
it('should preserve side-effectful CSS imports even if package.json claims no side effects', async () => {
let dir = await subject('css-side-effect');
await buildFast(dir);
await build(dir);

const builtStylesheet = await getOutputFile(dir, /bundle\.\w{5}\.css$/);
expect(builtStylesheet).toMatch('h1{background:#673ab8}');
});

it('should use SASS, SCSS, and CSS Modules for each', async () => {
let dir = await subject('css-sass');
await buildFast(dir);
await build(dir);
const builtStylesheet = await getOutputFile(dir, /bundle\.\w{5}\.css$/);

expect(builtStylesheet).toMatch('h1{background:blue;color:red}');
Expand All @@ -330,7 +328,7 @@ describe('preact build', () => {
prerenderUrlFiles.forEach(prerenderUrls => {
it(`should prerender the routes provided with '${prerenderUrls}'`, async () => {
let dir = await subject('multiple-prerendering');
await buildFast(dir, { prerenderUrls });
await build(dir, { prerenderUrls });

const body1 = await getBody(dir);
looksLike(body1, images.prerender.home);
Expand Down Expand Up @@ -367,7 +365,7 @@ describe('preact build', () => {
prerenderUrlFiles.forEach(prerenderUrls => {
it(`should prerender the routes with data provided with '${prerenderUrls}' via provider`, async () => {
let dir = await subject('multiple-prerendering-with-provider');
await buildFast(dir, { prerenderUrls });
await build(dir, { prerenderUrls });

const body1 = await getBody(dir);
looksLike(body1, images.prerender.home);
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/tests/config-formats.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { join } = require('path');
const { access } = require('fs/promises');
const { build, buildFast } = require('./lib/cli');
const { build } = require('./lib/cli');
const { subject } = require('./lib/output');

const formats = ['cjs', 'esm'];
Expand All @@ -19,7 +19,7 @@ describe('config files', () => {
it(`should load the 'prerender-urls.json' file`, async () => {
let dir = await subject('multiple-config-files');

await buildFast(dir);
await build(dir);

expect(await access(join(dir, 'build/index.html'))).toBeUndefined();
expect(
Expand All @@ -32,7 +32,7 @@ describe('config files', () => {
it(`should load the '${dataFormat}' file in ${moduleFormat}`, async () => {
let dir = await subject('multiple-config-files');

await buildFast(dir, {
await build(dir, {
prerenderUrls: `prerenderUrls/${moduleFormat}/${dataFormat}`,
});

Expand Down
31 changes: 11 additions & 20 deletions packages/cli/tests/images/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,26 @@ exports.default = {
'ssr-build/ssr-bundle.css': 2346,
'ssr-build/ssr-bundle.css.map': 3603,

'bundle.d55d3.js': 22978,
'bundle.d55d3.js.map': 92378,
'bundle.d55d3.legacy.js': 23646,
'bundle.d55d3.legacy.js.map': 92673,
'bundle.05a31.js': 22978,
'bundle.05a31.js.map': 125226,
'bundle.6329a.css': 1173,
'bundle.6329a.css.map': 2165,

'dom-polyfills.99150.js': 5221,
'dom-polyfills.99150.js.map': 18676,
'es-polyfills.js': 46419,
'dom-polyfills.f5813.js': 5221,
'dom-polyfills.f5813.js.map': 18676,

'favicon.ico': 15086,
'index.html': 3998,
'manifest.json': 455,
'preact_prerender_data.json': 11,

'route-home.chunk.ede4d.js': 1179,
'route-home.chunk.ede4d.js.map': 3814,
'route-home.chunk.ede4d.legacy.js': 1222,
'route-home.chunk.ede4d.legacy.js.map': 3964,
'route-home.chunk.d116e.css': 838,
'route-home.chunk.d116e.css.map': 1406,

'route-profile.chunk.6856a.js': 3165,
'route-profile.chunk.6856a.js.map': 13170,
'route-profile.chunk.6856a.legacy.js': 3302,
'route-profile.chunk.6856a.legacy.js.map': 13200,
'route-home.chunk.585d0.js': 1231,
'route-home.chunk.585d0.js.map': 2057,
'route-home.chunk.d116e.css': 825,
'route-home.chunk.d116e.css.map': 1378,

'route-profile.chunk.66aa5.js': 3300,
'route-profile.chunk.66aa5.js.map': 21931,
};

exports.prerender = {};
Expand Down Expand Up @@ -176,8 +169,6 @@ exports.publicPath = `
<script type="__PREACT_CLI_DATA__">%7B%22prerenderData%22:%7B%22url%22:%22/%22%7D%7D</script>
<script type="module" src="/example-path/bundle.\\w{5}.js"></script>
<script nomodule="" src="/example-path/dom-polyfills.\\w{5}.js"></script>
<script nomodule="" src="/example-path/es-polyfills.js"></script>
<script nomodule="" defer="defer" src="/example-path/bundle.\\w{5}.legacy.js"></script>
</body>
</html>
`;
9 changes: 2 additions & 7 deletions packages/cli/tests/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
create: createCmd,
} = require('../../../create-cli/src/commands/create');
const { tmpDir } = require('./output');
const { linkPackage, handleOptimize } = require('./utils');
const { linkPackage } = require('./utils');

exports.create = async function (template, options) {
let dest = await tmpDir();
Expand All @@ -17,7 +17,7 @@ exports.create = async function (template, options) {
return dest;
};

const build = (exports.build = async function (cwd, options) {
exports.build = async function (cwd, options) {
const argv = {
src: 'src',
dest: 'build',
Expand All @@ -34,11 +34,6 @@ const build = (exports.build = async function (cwd, options) {

let opts = Object.assign({ cwd }, argv, options);
return await buildCmd(opts);
});

exports.buildFast = async function (cwd, options) {
await handleOptimize(cwd, options && options.config);
return await build(cwd, options);
};

exports.watch = function (cwd, options) {
Expand Down
41 changes: 0 additions & 41 deletions packages/cli/tests/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,46 +71,6 @@ async function linkPackage(name, cwd) {
} catch {}
}

/**
* Injectable configuration to disable the optimize plugin
*/
const disableOptimizePluginConfig = `
const optimizePlugin = helpers.getPluginsByName(config, 'OptimizePlugin')[0];
if (optimizePlugin) {
const { plugin } = optimizePlugin;
plugin.options.downlevel = false;
plugin.options.minify = false;
}
`;

/**
* Full config file to disable the optimize plugin
*/
const disableOptimizePluginConfigFile = `module.exports = function (config, env, helpers) {
${disableOptimizePluginConfig}
}`;

/**
* Disables the slow portions of `optimize-plugin` for
* the tests that don't rely on its functionality.
*/
async function handleOptimize(cwd, config) {
const configFile = `${cwd}/${config || 'preact.config.js'}`;
try {
let config = await readFile(configFile, 'utf8');
// Don't alter config in subsequent runs of same subject
if (/optimizePlugin/.test(config)) return;
config = config.replace(
/}(?![\s\S]*})(?:;?)/m,
`${disableOptimizePluginConfig}};`
);
await writeFile(configFile, config);
} catch {
await writeFile(configFile, disableOptimizePluginConfigFile);
}
}

expect.extend({
toFindMatchingKey(key, matchingKey) {
if (matchingKey) {
Expand Down Expand Up @@ -160,5 +120,4 @@ module.exports = {
waitUntil,
sleep,
linkPackage,
handleOptimize,
};

0 comments on commit 8f09b5b

Please sign in to comment.