Skip to content

Commit

Permalink
Showing 8 changed files with 100 additions and 1 deletion.
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/**
3 changes: 3 additions & 0 deletions knip.json
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 339b78a

Please sign in to comment.