Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add transforms fixtures for codemod test #1421

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# transforms testfixtures
packages/codemods/src/transforms/testfixtures/**
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a .prettierignore file to prevent the test environment snapshots of testfixtures from being modified by the formatter. Is there a better approach to handle this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, that's good

3 changes: 3 additions & 0 deletions knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
},
"packages/react-dom": {
"ignoreDependencies": ["react-dom", "@types/react-dom"]
},
"packages/codemods": {
"ignore": ["**/testfixtures/**"]
}
}
}
7 changes: 6 additions & 1 deletion packages/codemods/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { suspensiveTypeScriptConfig } from '@suspensive/eslint-config'

export default [...suspensiveTypeScriptConfig]
export default [
...suspensiveTypeScriptConfig,
{
ignores: ['./src/transforms/testfixtures/**'],
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { queryOptions } from "@suspensive/react-query";
export { queryOptions }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { queryOptions } from "@tanstack/react-query";
export { queryOptions }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-expect-error - type definitions not available
import { defineInlineTest } from 'jscodeshift/dist/testUtils'
import transform from '../tanstack-query-import'
import { getTestfixtures } from '../utils/getTestfixtures'

const { input, expectedOutput, testName } = getTestfixtures('tanstack-query-import', 'jsx')

defineInlineTest(transform, null, input, expectedOutput, testName)
60 changes: 60 additions & 0 deletions packages/codemods/src/transforms/utils/getTestfixtures.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import type { Mock } from 'vitest'
import { getTestfixtures } from './getTestfixtures'

vi.mock('node:fs', () => ({
readFileSync: vi.fn(),
}))

const mockReadFileSync = readFileSync as Mock

describe('getTestfixtures', () => {
const mockInput = 'mock input content'
const mockOutput = 'mock output content'
const transformName = 'sampleTransform'
const extension = 'js'
const FIXTURE_DIR = join(__dirname, '../testfixtures', transformName)
const inputPath = join(FIXTURE_DIR, `${transformName}.input.${extension}`)
const outputPath = join(FIXTURE_DIR, `${transformName}.output.${extension}`)

beforeEach(() => {
vi.clearAllMocks()
})

it('should correctly read input and output fixture files', () => {
mockReadFileSync.mockImplementation((filePath: string) => {
if (filePath === inputPath) return mockInput
if (filePath === outputPath) return mockOutput
return ''
})

const result = getTestfixtures(transformName, extension)

expect(result).toEqual({
input: mockInput,
expectedOutput: mockOutput,
testName: transformName,
})
expect(mockReadFileSync).toHaveBeenCalledWith(inputPath, 'utf8')
expect(mockReadFileSync).toHaveBeenCalledWith(outputPath, 'utf8')
})

it('should throw an error if input file is missing', () => {
mockReadFileSync.mockImplementation((filePath: string) => {
if (filePath === outputPath) return mockOutput
throw new Error('File not found')
})

expect(() => getTestfixtures(transformName, extension)).toThrow('File not found')
})

it('should throw an error if output file is missing', () => {
mockReadFileSync.mockImplementation((filePath: string) => {
if (filePath === inputPath) return mockInput
throw new Error('File not found')
})

expect(() => getTestfixtures(transformName, extension)).toThrow('File not found')
})
})
17 changes: 17 additions & 0 deletions packages/codemods/src/transforms/utils/getTestfixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { readFileSync } from 'node:fs'
import { join } from 'node:path'

export function getTestfixtures(transformName: string, extension: 'js' | 'jsx' | 'ts' | 'tsx' = 'js') {
const FIXTURE_DIR = join(__dirname, '../testfixtures', transformName)
const inputPath = join(FIXTURE_DIR, `${transformName}.input.${extension}`)
const outputPath = join(FIXTURE_DIR, `${transformName}.output.${extension}`)

const input = readFileSync(inputPath, 'utf8').trim()
const expectedOutput = readFileSync(outputPath, 'utf8').trim()

return {
input,
expectedOutput,
testName: transformName,
}
}
Loading