Skip to content

Commit

Permalink
test(builder): ignore e2e tests that rspack does not need attention (w…
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Jul 3, 2023
1 parent 4a83fcd commit 36a070b
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 270 deletions.
29 changes: 29 additions & 0 deletions tests/e2e/builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,32 @@ pnpm run test:webpack
pnpm run test:webpack css
pnpm run test:rspack css
```

## Add Test Cases

### Add test cases for common capabilities

Test cases added using the `test` method will run in webpack and Rspack.

```ts
import { expect, test } from '@modern-js/e2e/playwright';
// will passed in webpack, and rspack
test('test 1 + 1', () => {
expect(1 + 1).toBe(2);
})
```

If the capability corresponding to the test case is not yet supported in other bundlers,
you can use the `webpackOnlyTest` or `rspackOnlyTest` methods for testing. Once other bundlers also support it, you can replace it with the `test` method.

```ts
import { webpackOnlyTest } from '@scripts/helper';
// will passed in webpack, and skipped in rspack
webpackOnlyTest('test 1 + 1', () => {
expect(1 + 1).toBe(2);
})
```

### Add test cases for a bundler's unique capability

If you want to add test cases for a bundler's unique capability and do not need to be supported by other bundlers, such as `tools.webpack` or `tools.webpackChain`, you can add the infix `.webpack` or `.rspack` to the file name, for example, `index.webpack.test.ts`.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import path from 'path';
import { execSync } from 'child_process';
import { expect } from '@modern-js/e2e/playwright';
import { expect, test } from '@modern-js/e2e/playwright';
import { globContentJSON } from '@modern-js/e2e';
import { rspackOnlyTest } from '../../../scripts/helper';

rspackOnlyTest('should run build command correctly', async () => {
test('should run build command correctly', async () => {
execSync('npm run build', {
cwd: __dirname,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import path from 'path';
import { execSync } from 'child_process';
import { expect } from '@modern-js/e2e/playwright';
import { expect, test } from '@modern-js/e2e/playwright';
import { globContentJSON } from '@modern-js/e2e';
import { webpackOnlyTest } from '../../../scripts/helper';

webpackOnlyTest('should run build command correctly', async () => {
test('should run build command correctly', async () => {
execSync('npm run build', {
cwd: __dirname,
});
Expand Down
93 changes: 93 additions & 0 deletions tests/e2e/builder/cases/source/plugin-import/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import path from 'path';
import { build } from '@scripts/shared';
import { expect, test } from '@modern-js/e2e/playwright';
import { ensureDirSync, copySync } from 'fs-extra';
import { SharedTransformImport } from '@modern-js/builder-shared';
import { BuilderConfig } from '@modern-js/builder-webpack-provider';

export const cases: Parameters<typeof shareTest>[] = [
[
`camelCase test`,
'./src/camel.js',
{
libraryName: 'foo',
libraryDirectory: 'lib',
camelToDashComponentName: false,
},
],
[
`kebab-case test`,
'./src/kebab.js',
{
libraryName: 'foo',
libraryDirectory: 'lib',
camelToDashComponentName: true,
},
],
[
'transform to named import',
'./src/named.js',
{
libraryName: 'foo',
libraryDirectory: 'lib',
camelToDashComponentName: true,
transformToDefaultImport: false,
},
],
];

export function findEntry(
files: Record<string, string>,
name = 'index',
): string {
for (const key of Reflect.ownKeys(files) as string[]) {
if (key.includes(`dist/static/js/${name}`) && key.endsWith('.js')) {
return key;
}
}

throw new Error('unreacheable');
}

export function copyPkgToNodeModules() {
const nodeModules = path.resolve(__dirname, 'node_modules');

ensureDirSync(nodeModules);
copySync(path.resolve(__dirname, 'foo'), path.resolve(nodeModules, 'foo'));
}

export function shareTest(
msg: string,
entry: string,
transformImport: SharedTransformImport,
otherConfigs: {
plugins?: any[];
} = {},
) {
const setupConfig = {
cwd: __dirname,
entry: {
index: entry,
},
};
const config: BuilderConfig = {
source: {
transformImport: [transformImport],
},
performance: {
chunkSplit: {
strategy: 'all-in-one',
},
},
};

test(msg, async () => {
const builder = await build({
...setupConfig,
...otherConfigs,
builderConfig: { ...config },
});
const files = await builder.unwrapOutputJSON(false);
expect(files[findEntry(files)]).toContain('transformImport test succeed');
});
}
36 changes: 36 additions & 0 deletions tests/e2e/builder/cases/source/plugin-import/index.rspack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'path';
import { build } from '@scripts/shared';
import { expect, test } from '@modern-js/e2e/playwright';
import { cases, shareTest, copyPkgToNodeModules, findEntry } from './helper';

test('should import with template config', async () => {
copyPkgToNodeModules();

const builder = await build({
cwd: __dirname,
entry: { index: path.resolve(__dirname, './src/index.js') },
builderConfig: {
source: {
transformImport: [
{
libraryName: 'foo',
customName: 'foo/lib/{{ member }}',
},
],
},
performance: {
chunkSplit: {
strategy: 'all-in-one',
},
},
},
});
const files = await builder.unwrapOutputJSON(false);
const entry = findEntry(files);
expect(files[entry]).toContain('transformImport test succeed');
});

cases.forEach(c => {
const [name, entry, config] = c;
shareTest(`${name}-rspack`, entry, config);
});
Loading

0 comments on commit 36a070b

Please sign in to comment.