|
1 | 1 | import os from 'node:os';
|
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +import prettier from 'prettier'; |
| 5 | + |
2 | 6 | import { prettierFormatter } from '../src/formatters/prettier';
|
3 | 7 |
|
4 |
| -test('uses default format with no available config', () => { |
| 8 | +import type { AnyFunction } from '@xunnamius/jest-types'; |
| 9 | + |
| 10 | +type SpiedFunction<T extends AnyFunction> = jest.SpyInstance< |
| 11 | + ReturnType<T>, |
| 12 | + Parameters<T> |
| 13 | +>; |
| 14 | + |
| 15 | +let prettierSpy: SpiedFunction<typeof prettier.format>; |
| 16 | + |
| 17 | +beforeEach(() => { |
| 18 | + prettierSpy = jest.spyOn(prettier, 'format'); |
| 19 | +}); |
| 20 | + |
| 21 | +it('uses default prettier options when no user-supplied config is available', () => { |
5 | 22 | expect.hasAssertions();
|
6 | 23 |
|
7 | 24 | const result = prettierFormatter(` var a = 'hi' `, { cwd: os.tmpdir() });
|
8 | 25 | expect(result).toBe('var a = "hi";\n');
|
9 | 26 | });
|
10 | 27 |
|
11 |
| -test('defaults all options', () => { |
| 28 | +it('uses user-supplied prettier config at project root if available (found starting at cwd)', () => { |
12 | 29 | expect.hasAssertions();
|
13 | 30 |
|
14 | 31 | const result = prettierFormatter(`var a = "hi";`);
|
15 | 32 | expect(result).toBe(`var a = 'hi';\n`);
|
16 | 33 | });
|
| 34 | + |
| 35 | +it('treats deprecated `filename` option as if it were `filepath`', () => { |
| 36 | + expect.hasAssertions(); |
| 37 | + |
| 38 | + const expectedFilename = path.join(__dirname, 'fake.js'); |
| 39 | + prettierFormatter(` var a = 'hi' `, { filename: expectedFilename }); |
| 40 | + |
| 41 | + expect(prettierSpy.mock.calls).toMatchObject([ |
| 42 | + [expect.any(String), expect.objectContaining({ filepath: expectedFilename })] |
| 43 | + ]); |
| 44 | +}); |
| 45 | + |
| 46 | +it('treats deprecated `config` option as if it were `prettierOptions`', () => { |
| 47 | + expect.hasAssertions(); |
| 48 | + |
| 49 | + const expectedConfig = { endOfLine: 'crlf' } as const; |
| 50 | + prettierFormatter(` var a = 'hi' `, { config: expectedConfig }); |
| 51 | + |
| 52 | + expect(prettierSpy.mock.calls).toMatchObject([ |
| 53 | + [expect.any(String), expect.objectContaining(expectedConfig)] |
| 54 | + ]); |
| 55 | +}); |
0 commit comments