Skip to content

Commit

Permalink
test: update inlineRuntimeChunk test case (#4564)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Sep 1, 2023
1 parent e3d08fb commit 48fca89
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 95 deletions.
2 changes: 2 additions & 0 deletions tests/e2e/builder/cases/css/less-exclude/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { expect } from '@modern-js/e2e/playwright';
import { build } from '@scripts/shared';
import { webpackOnlyTest } from '@scripts/helper';

// 'error[javascript]: JavaScript parsing error' in rspack
// The rspack default rule type seems to be inconsistent with webpack. Not sure if that will fix when disableTransformByDefault supported.
webpackOnlyTest('should exclude specified less file', async () => {
const builder = await build({
cwd: __dirname,
Expand Down
124 changes: 50 additions & 74 deletions tests/e2e/builder/cases/inline-chunk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { webpackOnlyTest } from '@scripts/helper';
import { build, getHrefByEntryName } from '@scripts/shared';
import { BundlerChain, RUNTIME_CHUNK_NAME } from '@modern-js/builder-shared';

// todo: Rspack not output RUNTIME_CHUNK_NAME.js.map
// Rspack will not output builder runtime source map, but it not necessary
// Identify whether the builder runtime chunk is included through some specific code snippets
const isRuntimeChunkInHtml = (html: string): boolean =>
Boolean(
html.match(new RegExp(`static/js/${RUNTIME_CHUNK_NAME}\\.(.+)\\.js\\.map`)),
);
Boolean(html.includes('builder-runtime') && html.includes('Loading chunk'));

// use source-map for easy to test. By default, builder use hidden-source-map
const toolsConfig = {
Expand Down Expand Up @@ -62,18 +61,9 @@ test.describe('disableInlineRuntimeChunk', () => {
),
).toBe(true);
});

webpackOnlyTest('should emit source map of builder runtime', async () => {
expect(
Object.keys(files).some(
fileName =>
fileName.includes(RUNTIME_CHUNK_NAME) && fileName.endsWith('.js.map'),
),
).toBe(true);
});
});

webpackOnlyTest('inline runtime chunk by default', async ({ page }) => {
test('inline runtime chunk by default', async ({ page }) => {
const builder = await build({
cwd: __dirname,
entry: { index: path.resolve(__dirname, './src/index.js') },
Expand All @@ -98,14 +88,6 @@ webpackOnlyTest('inline runtime chunk by default', async ({ page }) => {
),
).toBe(false);

// should emit source map of builder runtime
expect(
Object.keys(files).some(
fileName =>
fileName.includes(RUNTIME_CHUNK_NAME) && fileName.endsWith('.js.map'),
),
).toBe(true);

// found builder-runtime file in html
const indexHtml =
files[path.resolve(__dirname, './dist/html/index/index.html')];
Expand All @@ -115,66 +97,60 @@ webpackOnlyTest('inline runtime chunk by default', async ({ page }) => {
builder.close();
});

webpackOnlyTest(
'inline runtime chunk and remove source map when devtool is "hidden-source-map"',
async () => {
const builder = await build({
cwd: __dirname,
entry: { index: path.resolve(__dirname, './src/index.js') },
builderConfig: {
tools: {
bundlerChain(chain) {
chain.devtool('hidden-source-map');
},
test('inline runtime chunk and remove source map when devtool is "hidden-source-map"', async () => {
const builder = await build({
cwd: __dirname,
entry: { index: path.resolve(__dirname, './src/index.js') },
builderConfig: {
tools: {
bundlerChain(chain) {
chain.devtool('hidden-source-map');
},
},
});
},
});

const files = await builder.unwrapOutputJSON(false);
const files = await builder.unwrapOutputJSON(false);

// should not emit source map of builder runtime
expect(
Object.keys(files).some(
fileName =>
fileName.includes(RUNTIME_CHUNK_NAME) && fileName.endsWith('.js.map'),
),
).toBe(false);
},
);
// should not emit source map of builder runtime
expect(
Object.keys(files).some(
fileName =>
fileName.includes(RUNTIME_CHUNK_NAME) && fileName.endsWith('.js.map'),
),
).toBe(false);
});

webpackOnlyTest(
'inline runtime chunk by default with multiple entries',
async () => {
const builder = await build({
cwd: __dirname,
entry: {
index: path.resolve(__dirname, './src/index.js'),
another: path.resolve(__dirname, './src/another.js'),
},
builderConfig: {
tools: toolsConfig,
},
});
const files = await builder.unwrapOutputJSON(false);
test('inline runtime chunk by default with multiple entries', async () => {
const builder = await build({
cwd: __dirname,
entry: {
index: path.resolve(__dirname, './src/index.js'),
another: path.resolve(__dirname, './src/another.js'),
},
builderConfig: {
tools: toolsConfig,
},
});
const files = await builder.unwrapOutputJSON(false);

// no builder-runtime file in output
expect(
Object.keys(files).some(
fileName =>
fileName.includes(RUNTIME_CHUNK_NAME) && fileName.endsWith('.js'),
),
).toBe(false);
// no builder-runtime file in output
expect(
Object.keys(files).some(
fileName =>
fileName.includes(RUNTIME_CHUNK_NAME) && fileName.endsWith('.js'),
),
).toBe(false);

// found builder-runtime file in html
const indexHtml =
files[path.resolve(__dirname, './dist/html/index/index.html')];
const anotherHtml =
files[path.resolve(__dirname, './dist/html/another/index.html')];
// found builder-runtime file in html
const indexHtml =
files[path.resolve(__dirname, './dist/html/index/index.html')];
const anotherHtml =
files[path.resolve(__dirname, './dist/html/another/index.html')];

expect(isRuntimeChunkInHtml(indexHtml)).toBeTruthy();
expect(isRuntimeChunkInHtml(anotherHtml)).toBeTruthy();
},
);
expect(isRuntimeChunkInHtml(indexHtml)).toBeTruthy();
expect(isRuntimeChunkInHtml(anotherHtml)).toBeTruthy();
});

webpackOnlyTest(
'inline all scripts and emit all source maps',
Expand Down
23 changes: 23 additions & 0 deletions tests/e2e/builder/cases/top-level-await/index.swc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as path from 'path';
import { expect, test } from '@modern-js/e2e/playwright';
import { build, getHrefByEntryName } from '@scripts/shared';
import { builderPluginSwc } from '@modern-js/builder-plugin-swc';

test('should run top level await correctly when using SWC', async ({
page,
}) => {
const builder = await build({
cwd: __dirname,
entry: {
index: path.resolve(__dirname, './src/index.ts'),
},
plugins: [builderPluginSwc()],
runServer: true,
});

await page.goto(getHrefByEntryName('index', builder.port));

expect(await page.evaluate('window.foo')).toEqual('hello');

builder.close();
});
21 changes: 0 additions & 21 deletions tests/e2e/builder/cases/top-level-await/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as path from 'path';
import { expect } from '@modern-js/e2e/playwright';
import { build, getHrefByEntryName } from '@scripts/shared';
import { builderPluginSwc } from '@modern-js/builder-plugin-swc';
import { webpackOnlyTest } from '../../scripts/helper';

webpackOnlyTest('should run top level await correctly', async ({ page }) => {
Expand All @@ -19,23 +18,3 @@ webpackOnlyTest('should run top level await correctly', async ({ page }) => {

builder.close();
});

webpackOnlyTest(
'should run top level await correctly when using SWC',
async ({ page }) => {
const builder = await build({
cwd: __dirname,
entry: {
index: path.resolve(__dirname, './src/index.ts'),
},
plugins: [builderPluginSwc()],
runServer: true,
});

await page.goto(getHrefByEntryName('index', builder.port));

expect(await page.evaluate('window.foo')).toEqual('hello');

builder.close();
},
);

0 comments on commit 48fca89

Please sign in to comment.