Skip to content

Commit

Permalink
chore: add fixture project to allow for better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fwuensche committed Sep 7, 2024
1 parent d0b9502 commit 6ff6da1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/git.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'

import { guessProjectName } from './git.js' // Import the function to test
import { guessProjectName } from './git.js'
import path from 'path'

// Partially mock the module
vi.mock('./git.js', async (importOriginal) => {
Expand All @@ -11,13 +12,20 @@ vi.mock('./git.js', async (importOriginal) => {
}
})

// We have a fixture project in `test/fixtures/project_one` that we use to create test scenarios
const originalCwd = process.cwd()
const fixturesPath = path.join(originalCwd, 'test/fixtures/project_one')

describe('guessProjectName', () => {
beforeAll(() => process.chdir(fixturesPath)) // Change to `test/fixtures/project_one`

Check failure on line 20 in src/git.test.js

View workflow job for this annotation

GitHub Actions / test

src/git.test.js > guessProjectName

Error: ENOENT: no such file or directory, chdir '/home/runner/work/cherry-cli/cherry-cli' -> '/home/runner/work/cherry-cli/cherry-cli/test/fixtures/project_one' ❯ src/git.test.js:20:27 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -2, code: 'ENOENT', syscall: 'chdir', path: '/home/runner/work/cherry-cli/cherry-cli', dest: '/home/runner/work/cherry-cli/cherry-cli/test/fixtures/project_one' }
afterAll(() => process.chdir(originalCwd)) // Change back to the original working directory

it('should return an empty string if no remotes are found', async () => {
// TODO: It'd be better to improve guessProjectName to take url as a param, so we can test it without mocking git
const { git } = await import('./git.js') // Import the mocked `git` function

git.mockResolvedValueOnce([]) // Mock `git('remote')` to return an empty array
const result = await guessProjectName()

expect(result).toBe('cherrypush/cherry-cli')
expect(result).toBe('cherrypush/project_one')
})
})

0 comments on commit 6ff6da1

Please sign in to comment.