|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import * as path from 'path'; |
| 9 | +import * as fs from 'graceful-fs'; |
| 10 | +import {cleanup, runYarnInstall, writeFiles} from '../Utils'; |
| 11 | +import runJest from '../runJest'; |
| 12 | + |
| 13 | +const DIR = path.resolve(__dirname, '../to-match-inline-snapshot-crlf'); |
| 14 | +const TESTS_DIR = path.resolve(DIR, '__tests__'); |
| 15 | +const JEST_CONFIG_PATH = path.resolve(DIR, 'jest.config.js'); |
| 16 | +const PRETTIER_RC_PATH = path.resolve(DIR, '.prettierrc'); |
| 17 | + |
| 18 | +const readFile = (filename: string) => |
| 19 | + fs.readFileSync(path.join(TESTS_DIR, filename), 'utf8'); |
| 20 | + |
| 21 | +beforeAll(() => { |
| 22 | + runYarnInstall(DIR); |
| 23 | +}); |
| 24 | +beforeEach(() => { |
| 25 | + cleanup(TESTS_DIR); |
| 26 | + cleanup(JEST_CONFIG_PATH); |
| 27 | + cleanup(PRETTIER_RC_PATH); |
| 28 | +}); |
| 29 | +afterAll(() => { |
| 30 | + cleanup(TESTS_DIR); |
| 31 | + cleanup(JEST_CONFIG_PATH); |
| 32 | + cleanup(PRETTIER_RC_PATH); |
| 33 | +}); |
| 34 | + |
| 35 | +test('prettier with crlf newlines', () => { |
| 36 | + writeFiles(DIR, { |
| 37 | + 'jest.config.js': ` |
| 38 | + module.exports = {prettierPath: require.resolve('prettier')}; |
| 39 | + `, |
| 40 | + }); |
| 41 | + writeFiles(DIR, { |
| 42 | + '.prettierrc': '{"endOfLine": "crlf"}', |
| 43 | + }); |
| 44 | + writeFiles(TESTS_DIR, { |
| 45 | + 'test.ts': `test('snapshots', () => { |
| 46 | + expect({test: 1}).toMatchInlineSnapshot(); |
| 47 | +
|
| 48 | + expect({test: 2}).toMatchInlineSnapshot(); |
| 49 | + }); |
| 50 | +`.replaceAll('\n', '\r\n'), |
| 51 | + }); |
| 52 | + const {stderr, exitCode} = runJest(DIR, ['--ci=false']); |
| 53 | + expect(stderr).toContain('Snapshots: 2 written, 2 total'); |
| 54 | + expect(exitCode).toBe(0); |
| 55 | + |
| 56 | + const fileAfter = readFile('test.ts'); |
| 57 | + expect(fileAfter).toBe( |
| 58 | + `test("snapshots", () => { |
| 59 | + expect({ test: 1 }).toMatchInlineSnapshot(\` |
| 60 | + { |
| 61 | + "test": 1, |
| 62 | + } |
| 63 | + \`); |
| 64 | +
|
| 65 | + expect({ test: 2 }).toMatchInlineSnapshot(\` |
| 66 | + { |
| 67 | + "test": 2, |
| 68 | + } |
| 69 | + \`); |
| 70 | +}); |
| 71 | +`.replaceAll('\n', '\r\n'), |
| 72 | + ); |
| 73 | +}); |
0 commit comments