Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.3 KB
Raw Blame
import dependencyExtractor from '../../jest.dependencyExtractor'

const namedModules = (code: string): Set<string> =>
    new Set([...code.matchAll(/(?:require\(|from\s+)['"]([^'"]+)['"]/g)].map((match) => match[1]))

describe('jest.dependencyExtractor', () => {
    it.each([
        [
            'drops an import used only as a type',
            'shape.ts',
            `import { Shape } from './shape'\nexport const area = (shape: Shape): number => shape.width`,
            [],
        ],
        [
            'keeps an import used as a value',
            'run.ts',
            `import { helper } from './helper'\nexport const run = (): void => helper()`,
            ['./helper'],
        ],
        [
            'keeps an import used only in JSX',
            'Page.tsx',
            `import { Button } from './Button'\nexport const Page = (): JSX.Element => <Button />`,
            ['./Button'],
        ],
        [
            'keeps every import of a file the transformer cannot parse',
            'broken.ts',
            `import { Shape } from './shape'\nconst = `,
            ['./shape'],
        ],
    ])('%s', (_name, filePath, code, expected) => {
        expect([...dependencyExtractor.extract(code, filePath, namedModules)]).toEqual(expected)
    })
})