-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(src): ensure deprecated
config
option is still supported by `pr…
…ettierFormatter` Addresses comment by @layershifter in #139
- Loading branch information
Showing
2 changed files
with
62 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,55 @@ | ||
import os from 'node:os'; | ||
import path from 'node:path'; | ||
|
||
import prettier from 'prettier'; | ||
|
||
import { prettierFormatter } from '../src/formatters/prettier'; | ||
|
||
test('uses default format with no available config', () => { | ||
import type { AnyFunction } from '@xunnamius/jest-types'; | ||
|
||
type SpiedFunction<T extends AnyFunction> = jest.SpyInstance< | ||
ReturnType<T>, | ||
Parameters<T> | ||
>; | ||
|
||
let prettierSpy: SpiedFunction<typeof prettier.format>; | ||
|
||
beforeEach(() => { | ||
prettierSpy = jest.spyOn(prettier, 'format'); | ||
}); | ||
|
||
it('uses default prettier options when no user-supplied config is available', () => { | ||
expect.hasAssertions(); | ||
|
||
const result = prettierFormatter(` var a = 'hi' `, { cwd: os.tmpdir() }); | ||
expect(result).toBe('var a = "hi";\n'); | ||
}); | ||
|
||
test('defaults all options', () => { | ||
it('uses user-supplied prettier config at project root if available (found starting at cwd)', () => { | ||
expect.hasAssertions(); | ||
|
||
const result = prettierFormatter(`var a = "hi";`); | ||
expect(result).toBe(`var a = 'hi';\n`); | ||
}); | ||
|
||
it('treats deprecated `filename` option as if it were `filepath`', () => { | ||
expect.hasAssertions(); | ||
|
||
const expectedFilename = path.join(__dirname, 'fake.js'); | ||
prettierFormatter(` var a = 'hi' `, { filename: expectedFilename }); | ||
|
||
expect(prettierSpy.mock.calls).toMatchObject([ | ||
[expect.any(String), expect.objectContaining({ filepath: expectedFilename })] | ||
]); | ||
}); | ||
|
||
it('treats deprecated `config` option as if it were `prettierOptions`', () => { | ||
expect.hasAssertions(); | ||
|
||
const expectedConfig = { endOfLine: 'crlf' } as const; | ||
prettierFormatter(` var a = 'hi' `, { config: expectedConfig }); | ||
|
||
expect(prettierSpy.mock.calls).toMatchObject([ | ||
[expect.any(String), expect.objectContaining(expectedConfig)] | ||
]); | ||
}); |