Skip to content

Commit

Permalink
test: add alias e2e test (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Jul 2, 2024
1 parent 1a8f78e commit a645e60
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ compiled/
coverage/
doc_build/
playwright-report/
tsconfig.tsbuildinfo

.vscode/**/*
!.vscode/settings.json
Expand Down
29 changes: 29 additions & 0 deletions e2e/cases/alias/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { join } from 'node:path';
import { build } from '@rslib/core';
import { expect, test } from 'vitest';
import { getEntryJsResults } from '#shared';
import { loadConfig } from '../../../packages/core/src/config';

test('alias in js', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname, 'js');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
await build(rslibConfig);
const results = await getEntryJsResults(rslibConfig);

expect(results.esm).toContain('hello world');
expect(results.cjs).toContain('hello world');
});

test('alias in ts', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname, 'ts');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
await build(rslibConfig);
const results = await getEntryJsResults(rslibConfig);

expect(results.esm).toContain('hello world');
expect(results.cjs).toContain('hello world');
});
15 changes: 15 additions & 0 deletions e2e/cases/alias/js/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: join(__dirname, 'src/index.js'),
},
alias: {
'@src': join(__dirname, 'src'),
},
},
});
1 change: 1 addition & 0 deletions e2e/cases/alias/js/src/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 'hello world';
3 changes: 3 additions & 0 deletions e2e/cases/alias/js/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { a } from '@src/a';

console.info(a);
15 changes: 15 additions & 0 deletions e2e/cases/alias/ts/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: join(__dirname, 'src/index.ts'),
},
alias: {
'@src': join(__dirname, 'src'),
},
},
});
2 changes: 2 additions & 0 deletions e2e/cases/alias/ts/src/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const a = 'hello world';
export type A = string;
5 changes: 5 additions & 0 deletions e2e/cases/alias/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { a } from '@src/a';

console.info(a);

export type { A } from './a';
10 changes: 10 additions & 0 deletions e2e/cases/alias/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@src/*": ["./src/*"]
}
},
"include": ["src"]
}
62 changes: 24 additions & 38 deletions e2e/cases/define/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
import { join } from 'node:path';
import { type RslibConfig, build } from '@rslib/core';
import { build } from '@rslib/core';
import { expect, test } from 'vitest';
import { globContentJSON } from '#helper';
import { getEntryJsResults } from '#shared';
import { loadConfig } from '../../../packages/core/src/config';

test('define', async () => {
test('define in js', async () => {
delete process.env.NODE_ENV;

const rslibConfig: RslibConfig = {
lib: [
{
format: 'esm',
output: {
distPath: {
root: join(__dirname, './dist/esm'),
},
},
},
{
format: 'cjs',
output: {
distPath: {
root: join(__dirname, './dist/cjs'),
},
},
},
],
source: {
entry: {
main: join(__dirname, './js/src/index.js'),
},
define: {
VERSION: JSON.stringify('1.0.0'),
},
},
};
const fixturePath = join(__dirname, 'js');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
await build(rslibConfig);
const results = await getEntryJsResults(rslibConfig);

const instance = await build(rslibConfig);
expect(results.esm).not.toContain('console.info(VERSION)');
expect(results.esm).toContain('1.0.0');
expect(results.cjs).not.toContain('console.info(VERSION)');
expect(results.cjs).toContain('1.0.0');
});

test('define in ts', async () => {
delete process.env.NODE_ENV;

const results = await globContentJSON(instance[0]!.context.distPath, {
absolute: true,
ignore: ['/**/*.map'],
});
const fixturePath = join(__dirname, 'ts');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
await build(rslibConfig);
const results = await getEntryJsResults(rslibConfig);

const entryJs = Object.keys(results).find((file) => file.endsWith('.js'));
expect(results[entryJs!]).not.toContain('console.info(VERSION)');
expect(results.esm).not.toContain('console.info(VERSION)');
expect(results.esm).toContain('1.0.0');
expect(results.cjs).not.toContain('console.info(VERSION)');
expect(results.cjs).toContain('1.0.0');
});
15 changes: 15 additions & 0 deletions e2e/cases/define/js/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: join(__dirname, 'src/index.js'),
},
define: {
VERSION: JSON.stringify('1.0.0'),
},
},
});
15 changes: 15 additions & 0 deletions e2e/cases/define/ts/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: join(__dirname, 'src/index.ts'),
},
define: {
VERSION: JSON.stringify('1.0.0'),
},
},
});
1 change: 1 addition & 0 deletions e2e/cases/define/ts/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare const VERSION: number;
1 change: 1 addition & 0 deletions e2e/cases/define/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.info(VERSION);
3 changes: 2 additions & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"private": true,
"type": "module",
"imports": {
"#helper": "./scripts/helper.ts"
"#helper": "./scripts/helper.ts",
"#shared": "./scripts/shared.ts"
},
"scripts": {
"test": "playwright test"
Expand Down
44 changes: 44 additions & 0 deletions e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { join } from 'node:path';
import type { LibConfig, RslibConfig } from '@rslib/core';
import { globContentJSON } from '#helper';

export function generateBundleEsmConfig(cwd: string): LibConfig {
return {
format: 'esm',
output: {
distPath: {
root: join(cwd, './dist/esm'),
},
},
};
}

export function generateBundleCjsConfig(cwd: string): LibConfig {
return {
format: 'cjs',
output: {
distPath: {
root: join(cwd, './dist/cjs'),
},
},
};
}

export async function getEntryJsResults(rslibConfig: RslibConfig) {
const results: Record<string, string> = {};

for (const libConfig of rslibConfig.lib) {
const result = await globContentJSON(libConfig?.output?.distPath?.root!, {
absolute: true,
ignore: ['/**/*.map'],
});

const entryJs = Object.keys(result).find((file) => file.endsWith('.js'));

if (entryJs) {
results[libConfig.format!] = result[entryJs]!;
}
}

return results;
}
2 changes: 1 addition & 1 deletion e2e/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"noEmit": true,
"composite": true
},
"include": ["cases", "playwright.config.ts", "cases/**/*.test.ts", "scripts"],
"include": ["cases/**/*.ts", "playwright.config.ts", "scripts"],
"exclude": ["**/node_modules", "**/.*/"],
"references": [
{
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"prebundle": "nx run-many -t prebundle",
"prepare": "pnpm run build && simple-git-hooks",
"sort-package-json": "npx sort-package-json \"packages/*/package.json\"",
"test:artifact": "vitest run --project artifact",
"test:artifact": "vitest run --project artifact",
"test:e2e": "cd e2e && pnpm run test",
"test:unit": "vitest run --project unit",
"test:unit": "vitest run --project unit",
"watch": "pnpm build --watch"
},
"simple-git-hooks": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"prebundle": "prebundle"
},
"dependencies": {
"@rsbuild/core": "0.7.9"
"@rsbuild/core": "0.7.10"
},
"devDependencies": {
"@rslib/tsconfig": "workspace:*",
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a645e60

Please sign in to comment.