Skip to content

Commit

Permalink
test: Add tests for updated push-manifest building
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Mar 24, 2022
1 parent 190e291 commit 22a52fd
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 8 deletions.
71 changes: 63 additions & 8 deletions packages/cli/tests/build.test.js
@@ -1,5 +1,5 @@
const { join } = require('path');
const { access, readdir, readFile } = require('fs').promises;
const { access, readdir, readFile, writeFile } = require('fs').promises;
const looksLike = require('html-looks-like');
const { create, build } = require('./lib/cli');
const { snapshot } = require('./lib/utils');
Expand Down Expand Up @@ -245,13 +245,68 @@ describe('preact build', () => {
mockExit.mockRestore();
});

it('should produce correct push-manifest', async () => {
let dir = await create('default');
describe('Push manifest plugin', () => {
it('should produce correct default `push-manifest.json`', async () => {
let dir = await create('default');

await build(dir);
const manifest = await readFile(`${dir}/build/push-manifest.json`, 'utf8');
expect(manifest).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.pushManifest))
);
await build(dir);
const manifest = await readFile(
`${dir}/build/push-manifest.json`,
'utf8'
);
expect(manifest).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.pushManifest))
);
});

it('should produce correct default `push-manifest.json` with esm', async () => {
let dir = await create('default');

await build(dir, { esm: true });
const manifest = await readFile(
`${dir}/build/push-manifest.json`,
'utf8'
);
expect(manifest).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.pushManifestEsm))
);
});

it('should produce correct `push-manifest.json` when expected values are missing', async () => {
// In this subject, there is no source CSS which means no CSS asset is output.
// In the past, this would result in `"undefined": { type: "style" ... }` being added to the manifest.
let dir = await subject('custom-webpack');
await build(dir);
const manifest = await readFile(
`${dir}/build/push-manifest.json`,
'utf8'
);
expect(manifest).not.toMatch(/"undefined"/);
});

// Issue #1675
it('should produce correct `push-manifest.json` when user configures output filenames', async () => {
let dir = await subject('custom-webpack');

const config = await readFile(`${dir}/preact.config.js`, 'utf8');
await writeFile(
`${dir}/preact.config.js`,
config.replace(
"config.output.filename = '[name].js'",
"config.output.filename = 'scripts/[name].js'"
)
);

await build(dir, { prerender: false });
const manifest = await readFile(
`${dir}/build/push-manifest.json`,
'utf8'
);
expect(manifest).toEqual(
expect.stringMatching(
getRegExpFromMarkup(images.pushManifestAlteredFilenames)
)
);
});
});
});
55 changes: 55 additions & 0 deletions packages/cli/tests/images/build.js
Expand Up @@ -15,6 +15,7 @@ exports.default = Object.assign({}, common, {
'ssr-build/ssr-bundle.aaacf.css.map': 2070,
'ssr-build/ssr-bundle.js': 11937,
'ssr-build/ssr-bundle.js.map': 32557,
'ssr-build/asset-manifest.json': 178,
'bundle.2da73.css': 901,
'bundle.44866.js': 21429,
'bundle.44866.js.map': 111801,
Expand All @@ -23,6 +24,7 @@ exports.default = Object.assign({}, common, {
'manifest.json': 455,
'preact_prerender_data.json': 11,
'push-manifest.json': 450,
'asset-manifest.json': 1074,
'route-home.chunk.bcb8a.css': 58,
'route-home.chunk.3cec8.js': 327,
'route-home.chunk.3cec8.js.map': 483,
Expand All @@ -42,6 +44,7 @@ exports['default-esm'] = Object.assign({}, exports.default, {
'route-profile.chunk.*.esm.js.map': 15392,
'index.html': 2193,
'push-manifest.json': 466,
'asset-manifest.json': 1106,
});

exports.sass = `
Expand Down Expand Up @@ -253,3 +256,55 @@ exports.pushManifest = `
}
}
`;

exports.pushManifestEsm = `
{
"/":{
"bundle.\\w{5}.css":{
"type":"style",
"weight":1
},
"bundle.\\w{5}.esm.js":{
"type":"script",
"weight":1
},
"route-home.chunk.\\w{5}.esm.js":{
"type":"script",
"weight":0.9
},
"route-home.chunk.\\w{5}.css":{
"type":"style",
"weight":0.9
}
},
"/profile":{
"bundle.\\w{5}.css":{
"type":"style",
"weight":1
},
"bundle.\\w{5}.esm.js":{
"type":"script",
"weight":1
},
"route-profile.chunk.\\w{5}.esm.js":{
"type":"script",
"weight":0.9
},
"route-profile.chunk.\\w{5}.css":{
"type":"style",
"weight":0.9
}
}
}
`;

exports.pushManifestAlteredFilenames = `
{
"/":{
"scripts/bundle.js":{
"type":"script",
"weight":1
}
}
}
`;

0 comments on commit 22a52fd

Please sign in to comment.