Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Oct 16, 2024
1 parent 2d8a2e8 commit 43dedc2
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 81 deletions.
132 changes: 124 additions & 8 deletions test/dts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,29 +258,29 @@ test('should emit declaration files with experimentalDts', async () => {
export function sharedFunction<T>(value: T): T | null {
return value || null
}
type sharedType = {
shared: boolean
}
export type { sharedType }
`,
'src/server.ts': `
export * from './shared'
/**
* Comment for server render function
* Comment for server render function
*/
export function render(options: ServerRenderOptions): string {
return JSON.stringify(options)
}
export interface ServerRenderOptions {
/**
* Comment for ServerRenderOptions.stream
*
*
* @public
*
*
* @my_custom_tag
*/
stream: boolean
Expand All @@ -298,7 +298,7 @@ test('should emit declaration files with experimentalDts', async () => {
import * as ServerThirdPartyNamespace from 'react-dom';
export { ServerThirdPartyNamespace }
// Export a third party module
// Export a third party module
export * from 'react-dom/server';
`,
Expand All @@ -308,7 +308,7 @@ test('should emit declaration files with experimentalDts', async () => {
export function render(options: ClientRenderOptions): string {
return JSON.stringify(options)
}
export interface ClientRenderOptions {
document: boolean
}
Expand Down Expand Up @@ -473,3 +473,119 @@ test('declaration files with multiple entrypoints #316', async () => {
'dist/bar/index.d.ts',
).toMatchSnapshot()
})

test('custom dts output extension', async ({ expect, task }) => {
const { outFiles } = await run(
getTestName(),
{
'src/types.ts': `export type Person = { name: string }`,
'src/index.ts': `export const foo = [1, 2, 3]\nexport type { Person } from './types'`,
'tsup.config.ts': `export default {
name: '${task.name}',
entry: { index: 'src/index.ts' },
dts: true,
format: ['esm', 'cjs'],
outExtension({ format }) {
return {
js: format === 'esm' ? '.cjs' : '.mjs',
dts: format === 'esm' ? '.d.cts' : '.d.mts',
}
},
}`,
'package.json': JSON.stringify(
{
name: 'custom-dts-output-extension',
description: task.name,
type: 'module',
},
null,
2,
),
'tsconfig.json': JSON.stringify(
{
compilerOptions: {
outDir: './dist',
rootDir: './src',
moduleResolution: 'Bundler',
module: 'ESNext',
strict: true,
skipLibCheck: true,
},
include: ['src'],
},
null,
2,
),
},
{
entry: [],
},
)
expect(outFiles).toStrictEqual([
'index.cjs',
'index.d.cts',
'index.d.mts',
'index.mjs',
])
})

test('custom dts output extension with api-extractor', async ({
expect,
task,
}) => {
const { outFiles } = await run(
getTestName(),
{
'src/types.ts': `export type Person = { name: string }`,
'src/index.ts': `export const foo = [1, 2, 3]\nexport type { Person } from './types'`,
'tsup.config.ts': `export default {
name: '${task.name}',
entry: { index: 'src/index.ts' },
format: ['esm', 'cjs'],
experimentalDts: true,
outExtension({ format }) {
return {
js: format === 'cjs' ? '.cjs' : '.mjs',
dts: format === 'cjs' ? '.d.cts' : '.d.mts',
}
},
}`,
'package.json': JSON.stringify(
{
name: 'custom-dts-output-extension-with-api-extractor',
description: task.name,
type: 'module',
},
null,
2,
),
'tsconfig.json': JSON.stringify(
{
compilerOptions: {
outDir: './dist',
rootDir: './src',
moduleResolution: 'Bundler',
module: 'ESNext',
strict: true,
skipLibCheck: true,
},
include: ['src'],
},
null,
2,
),
},
{
entry: [],
},
)

expect(outFiles).toStrictEqual([
'_tsup-dts-rollup.d.cts',
'_tsup-dts-rollup.d.mts',
'index.cjs',
'index.d.cts',
'index.d.mts',
'index.mjs',
])
})
73 changes: 0 additions & 73 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,79 +663,6 @@ test('custom output extension', async () => {
`)
})

test('custom dts output extension', async () => {
const { outFiles } = await run(
getTestName(),
{
'input.ts': `export const foo = [1,2,3]`,
'tsup.config.ts': `export default {
dts: true,
outExtension({ format }) {
return {
js: format === 'cjs' ? '.cjs' : '.mjs',
dts: format === 'cjs' ? '.d.cts' : '.d.mts'
}
}
}`,
},
{
entry: ['input.ts'],
flags: ['--format', 'esm,cjs'],
},
)
expect(outFiles).toStrictEqual([
'input.cjs',
'input.d.cts',
'input.d.mts',
'input.mjs',
])
})

test('custom dts output extension with api-extractor', async () => {
const { outFiles } = await run(
getTestName(),
{
'src/index.ts': `export const foo = [1,2,3]`,
'tsup.config.ts': `export default {
entry: { index: 'src/index.ts' },
format: ['esm', 'cjs'],
experimentalDts: true,
outExtension({ format }) {
return {
js: format === 'cjs' ? '.cjs' : '.mjs',
dts: format === 'cjs' ? '.d.cts' : '.d.mts'
}
}
}`,
'package.json': JSON.stringify({
name: 'some-package',
type: 'module',
}),
'tsconfig.json': JSON.stringify({
compilerOptions: {
outDir: './dist',
rootDir: './src',
moduleResolution: 'NodeNext',
module: 'NodeNext',
},
include: ['src'],
}),
},
{
entry: [],
},
)

expect(outFiles).toStrictEqual([
'_tsup-dts-rollup.d.cts',
'_tsup-dts-rollup.d.mts',
'index.cjs',
'index.d.cts',
'index.d.mts',
'index.mjs',
])
})

test('custom config file', async () => {
const { outFiles } = await run(
getTestName(),
Expand Down

0 comments on commit 43dedc2

Please sign in to comment.