Skip to content

Commit

Permalink
test: add tests of dts: false and dts.distPath (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Aug 6, 2024
1 parent 140f955 commit 8cd057d
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 15 deletions.
4 changes: 2 additions & 2 deletions e2e/cases/dts/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`dts when bundle: false 1`] = `
exports[`dts when bundle: false > basic - bundleless dts 1`] = `
{
"./dist/esm/index.d.ts": "export * from './utils/numbers';
export * from './utils/strings';
Expand All @@ -20,7 +20,7 @@ export declare const str3 = "str3";
}
`;

exports[`dts when bundle: true 1`] = `
exports[`dts when bundle: true > basic - bundle dts 1`] = `
{
"esm": "export declare const num1 = 1;
Expand Down
22 changes: 22 additions & 0 deletions e2e/cases/dts/bundle-false/rslib.distpath.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
bundle: false,
dts: {
bundle: false,
distPath: './dist/custom',
},
}),
generateBundleCjsConfig(__dirname, {
bundle: false,
}),
],
source: {
entry: {
main: ['./src/**'],
},
},
});
19 changes: 19 additions & 0 deletions e2e/cases/dts/bundle-false/rslib.false.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
bundle: false,
dts: false,
}),
generateBundleCjsConfig(__dirname, {
bundle: false,
}),
],
source: {
entry: {
main: ['./src/**'],
},
},
});
19 changes: 19 additions & 0 deletions e2e/cases/dts/bundle/rslib.distpath.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
dts: {
bundle: true,
distPath: './dist/custom',
},
}),
generateBundleCjsConfig(__dirname),
],
source: {
entry: {
main: './src/index.ts',
},
},
});
16 changes: 16 additions & 0 deletions e2e/cases/dts/bundle/rslib.false.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
dts: false,
}),
generateBundleCjsConfig(__dirname),
],
source: {
entry: {
main: './src/index.ts',
},
},
});
80 changes: 68 additions & 12 deletions e2e/cases/dts/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,76 @@
import { join } from 'node:path';
import { buildAndGetResults } from '@e2e/helper';
import { expect, test } from 'vitest';
import { describe, expect, test } from 'vitest';

test('dts when bundle: false', async () => {
const fixturePath = join(__dirname, 'bundle-false');
const { files, contents } = await buildAndGetResults(fixturePath, 'dts');
describe('dts when bundle: false', () => {
test('basic - bundleless dts', async () => {
const fixturePath = join(__dirname, 'bundle-false');
const { files, contents } = await buildAndGetResults(
fixturePath,
'rslib.config.ts',
'dts',
);

expect(files.esm?.length).toBe(4);
expect(files.esm?.[0]!.endsWith('.d.ts')).toEqual(true);
expect(contents.esm).toMatchSnapshot();
expect(files.esm?.length).toBe(4);
expect(files.esm?.[0]!.endsWith('.d.ts')).toEqual(true);
expect(contents.esm).toMatchSnapshot();
});

test('dts false - bundleless dts', async () => {
const fixturePath = join(__dirname, 'bundle-false');
const { files } = await buildAndGetResults(
fixturePath,
'rslib.false.config.ts',
'dts',
);

expect(files.esm).toBe(undefined);
});

test('distPath - bundleless dts', async () => {
const fixturePath = join(__dirname, 'bundle-false');
const { files } = await buildAndGetResults(
fixturePath,
'rslib.distpath.config.ts',
'dts',
);
expect(files.esm?.length).toBe(4);
expect(files.esm?.[0]!.startsWith('./dist/custom')).toEqual(true);
});
});

test('dts when bundle: true', async () => {
const fixturePath = join(__dirname, 'bundle');
const { entryFiles, entries } = await buildAndGetResults(fixturePath, 'dts');
describe('dts when bundle: true', () => {
test('basic - bundle dts', async () => {
const fixturePath = join(__dirname, 'bundle');
const { entryFiles, entries } = await buildAndGetResults(
fixturePath,
'rslib.config.ts',
'dts',
);

expect(entryFiles.esm!.endsWith('index.d.ts')).toEqual(true);
expect(entries).toMatchSnapshot();
});

test('dts false - bundle dts', async () => {
const fixturePath = join(__dirname, 'bundle');
const { entryFiles } = await buildAndGetResults(
fixturePath,
'rslib.false.config.ts',
'dts',
);

expect(entryFiles.esm).toEqual(undefined);
});

test('distPath - bundle dts', async () => {
const fixturePath = join(__dirname, 'bundle');
const { entryFiles } = await buildAndGetResults(
fixturePath,
'rslib.distpath.config.ts',
'dts',
);

expect(entryFiles.esm!.endsWith('index.d.ts')).toEqual(true);
expect(entries).toMatchSnapshot();
expect(entryFiles.esm!.startsWith('./dist/custom')).toEqual(true);
});
});
3 changes: 2 additions & 1 deletion e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export async function getResults(

export const buildAndGetResults = async (
fixturePath: string,
configFile = 'rslib.config.ts',
type: 'js' | 'dts' = 'js',
): Promise<{
contents: Record<string, Record<string, string>>;
Expand All @@ -113,7 +114,7 @@ export const buildAndGetResults = async (
rspackConfig: InspectConfigResult['origin']['bundlerConfigs'];
rsbuildConfig: InspectConfigResult['origin']['rsbuildConfig'];
}> => {
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
const rslibConfig = await loadConfig(join(fixturePath, configFile));
process.chdir(fixturePath);
const rsbuildInstance = await build(rslibConfig);
const {
Expand Down
1 change: 1 addition & 0 deletions scripts/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ craco
crossorigin
datauri
deepmerge
distpath
docgen
dogfooding
envinfo
Expand Down

0 comments on commit 8cd057d

Please sign in to comment.