Skip to content

Commit

Permalink
fix broken test and esm shim
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 11, 2025
1 parent 90d4e2d commit 6791d6c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion test/integration/dynamic-require/dynamic-require.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs'
import { createIntegrationTest, getFileNamesFromDirectory } from 'testing-utils'
import { createIntegrationTest, getFileNamesFromDirectory } from '../utils'

describe('integration - dynamic-require', () => {
it('should work', async () => {
Expand Down
12 changes: 10 additions & 2 deletions test/integration/esm-shims/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createIntegrationTest, assertFilesContent } from '../utils'
import {
createIntegrationTest,
assertFilesContent,
getFileContents,
} from '../utils'

describe('integration esm-shims', () => {
it('should work with ESM shims', async () => {
Expand All @@ -15,7 +19,6 @@ describe('integration esm-shims', () => {
'const __dirname = __node_cjsPath.dirname(__filename)'

await assertFilesContent(distDir, {
'require.mjs': requirePolyfill,
'filename.mjs': filenamePolyfill,
'dirname.mjs': dirnamePolyfill,
'custom-require.mjs': (code) => !code.includes(requirePolyfill),
Expand All @@ -24,6 +27,11 @@ describe('integration esm-shims', () => {
'dirname.js': /__dirname/,
'custom-require.js': (code) => !code.includes(requirePolyfill),
})

const contents = await getFileContents(distDir, ['require.mjs'])
expect(contents['require.mjs']).not.toContain(requirePolyfill)
expect(contents['require.mjs']).toContain('function getRequireModule')
expect(contents['require.mjs']).toContain('import.meta.url')
},
)
})
Expand Down
16 changes: 11 additions & 5 deletions test/integration/unspecified-types-paths/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fs from 'fs'
import { assertFilesContent, createIntegrationTest } from '../utils'
import {
assertFilesContent,
createIntegrationTest,
getFileNamesFromDirectory,
} from '../utils'

describe('integration tsconfig-override', () => {
describe('integration - tsconfig-override', () => {
it('should not generate js types paths if not specified', async () => {
await createIntegrationTest(
{
Expand All @@ -12,8 +15,11 @@ describe('integration tsconfig-override', () => {
'./dist/subpath/nested.js': 'subpath/nested',
'./dist/subpath/nested.cjs': 'subpath/nested',
})
const subpathTypes = await import(`${dir}/dist/index.js`)
expect(fs.existsSync(subpathTypes)).toBe(false)
// No types files should be generated
expect(await getFileNamesFromDirectory(dir)).toEqual([
'dist/subpath/nested.cjs',
'dist/subpath/nested.js',
])
},
)
})
Expand Down
11 changes: 11 additions & 0 deletions test/testing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ export function assertContainFiles(dir: string, filePaths: string[]) {

type FunctionCondition = (content: string) => Boolean

export async function getFileContents(dir: string, filePaths?: string[]) {
const results: Record<string, string> = {}
const files = filePaths || (await fsp.readdir(dir))
for (const file of files) {
const fullPath = path.resolve(dir, file)
const content = await fsp.readFile(fullPath, { encoding: 'utf-8' })
results[file] = content
}
return results
}

export async function assertFilesContent(
dir: string,
conditionMap: Record<string, RegExp | string | FunctionCondition>,
Expand Down
15 changes: 10 additions & 5 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
"target": "ESNext",
"strict": false,
"jsx": "preserve",
"baseUrl": ".",
"baseUrl": "..",
"paths": {
"bunchee": ["../src/index.ts"],
"testing-utils": ["./integration/utils.ts"]
"bunchee": ["./src/index.ts"]
}
},
"include": [
"../src/**/*.test.ts",
"../src/**/*.test.tsx"
"./test/**/*.test.ts",
"./test**/*.test.tsx",
// TODO: group those internal utils
"./test/utils/*",
"./test/testing-utils.ts",
"./test/integration/utils.ts",
"./src/**/*.test.ts",
"./src/**/*.test.tsx"
],
"references": [{ "path": "../tsconfig.json" }]
}

0 comments on commit 6791d6c

Please sign in to comment.