|
| 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 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import {updateSnapshotsAndGetJestSnapshotResult} from '../snapshotUtils'; |
| 12 | +import {SnapshotState} from 'jest-snapshot'; |
| 13 | +import fs from 'node:fs'; |
| 14 | +import os from 'node:os'; |
| 15 | +import path from 'node:path'; |
| 16 | + |
| 17 | +let directory: string; |
| 18 | +beforeEach(() => { |
| 19 | + directory = fs.mkdtempSync(path.join(os.tmpdir(), 'fantom-snapshot-test-')); |
| 20 | +}); |
| 21 | +afterEach(() => fs.rmSync(directory, {recursive: true, force: true})); |
| 22 | + |
| 23 | +it('does not overwrite an existing snapshot on mismatch without -u', () => { |
| 24 | + const filePath = path.join(directory, 'example.snap'); |
| 25 | + fs.writeFileSync(filePath, 'exports[`test 1`] = `old hash`;\n'); |
| 26 | + for (const mode of ['new', 'none'] as const) { |
| 27 | + const state = new SnapshotState(filePath, { |
| 28 | + updateSnapshot: mode, |
| 29 | + rootDir: directory, |
| 30 | + snapshotFormat: {}, |
| 31 | + }); |
| 32 | + const result = updateSnapshotsAndGetJestSnapshotResult(state, [ |
| 33 | + { |
| 34 | + status: 'failed', |
| 35 | + snapshotResults: {'test 1': {pass: false, value: 'new hash'}}, |
| 36 | + }, |
| 37 | + ]); |
| 38 | + expect(result.unmatched).toBe(1); |
| 39 | + expect(result.updated).toBe(0); |
| 40 | + expect(fs.readFileSync(filePath, 'utf8')).toContain('old hash'); |
| 41 | + expect(fs.readFileSync(filePath, 'utf8')).not.toContain('new hash'); |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +it('updates a hash once with -u and counts passing snapshots once', () => { |
| 46 | + const filePath = path.join(directory, 'example.snap'); |
| 47 | + fs.writeFileSync(filePath, 'exports[`test 1`] = `old hash`;\n'); |
| 48 | + const state = new SnapshotState(filePath, { |
| 49 | + updateSnapshot: 'all', |
| 50 | + rootDir: directory, |
| 51 | + snapshotFormat: {}, |
| 52 | + }); |
| 53 | + const result = updateSnapshotsAndGetJestSnapshotResult(state, [ |
| 54 | + { |
| 55 | + status: 'passed', |
| 56 | + snapshotResults: {'test 1': {pass: false, value: 'new hash'}}, |
| 57 | + }, |
| 58 | + ]); |
| 59 | + expect(result.updated).toBe(1); |
| 60 | + expect(fs.readFileSync(filePath, 'utf8')).toContain('new hash'); |
| 61 | + const matching = new SnapshotState(filePath, { |
| 62 | + updateSnapshot: 'none', |
| 63 | + rootDir: directory, |
| 64 | + snapshotFormat: {}, |
| 65 | + }); |
| 66 | + const matched = updateSnapshotsAndGetJestSnapshotResult(matching, [ |
| 67 | + { |
| 68 | + status: 'passed', |
| 69 | + snapshotResults: {'test 1': {pass: true}}, |
| 70 | + }, |
| 71 | + ]); |
| 72 | + expect(matched.matched).toBe(1); |
| 73 | + expect(matched.unchecked).toBe(0); |
| 74 | +}); |
| 75 | + |
| 76 | +it('counts a new snapshot once', () => { |
| 77 | + const state = new SnapshotState(path.join(directory, 'example.snap'), { |
| 78 | + updateSnapshot: 'new', |
| 79 | + rootDir: directory, |
| 80 | + snapshotFormat: {}, |
| 81 | + }); |
| 82 | + const result = updateSnapshotsAndGetJestSnapshotResult(state, [ |
| 83 | + { |
| 84 | + status: 'passed', |
| 85 | + snapshotResults: {'test 1': {pass: false, value: 'new hash'}}, |
| 86 | + }, |
| 87 | + ]); |
| 88 | + expect(result.added).toBe(1); |
| 89 | + expect(result.matched).toBe(0); |
| 90 | + expect(result.updated).toBe(0); |
| 91 | + expect(result.unmatched).toBe(0); |
| 92 | +}); |
| 93 | + |
| 94 | +it('preserves unchecked snapshots when no tests ran with -u', () => { |
| 95 | + const filePath = path.join(directory, 'example.snap'); |
| 96 | + const contents = |
| 97 | + '// Jest Snapshot v1, https://goo.gl/fbAQLP\n\nexports[`test 1`] = `old hash`;\n'; |
| 98 | + fs.writeFileSync(filePath, contents); |
| 99 | + const state = new SnapshotState(filePath, { |
| 100 | + updateSnapshot: 'all', |
| 101 | + rootDir: directory, |
| 102 | + snapshotFormat: {}, |
| 103 | + }); |
| 104 | + const result = updateSnapshotsAndGetJestSnapshotResult(state, []); |
| 105 | + expect(fs.readFileSync(filePath, 'utf8')).toBe(contents); |
| 106 | + expect(result.unchecked).toBe(0); |
| 107 | + expect(result.uncheckedKeys).toEqual([]); |
| 108 | +}); |
| 109 | + |
| 110 | +it('preserves unchecked snapshots after skipped or failed tests with -u', () => { |
| 111 | + const filePath = path.join(directory, 'example.snap'); |
| 112 | + const contents = |
| 113 | + '// Jest Snapshot v1, https://goo.gl/fbAQLP\n\nexports[`other test 1`] = `other hash`;\n\nexports[`removed test 1`] = `removed hash`;\n\nexports[`test 1`] = `old hash`;\n'; |
| 114 | + for (const status of ['pending', 'failed'] as const) { |
| 115 | + fs.writeFileSync(filePath, contents); |
| 116 | + const state = new SnapshotState(filePath, { |
| 117 | + updateSnapshot: 'all', |
| 118 | + rootDir: directory, |
| 119 | + snapshotFormat: {}, |
| 120 | + }); |
| 121 | + const result = updateSnapshotsAndGetJestSnapshotResult(state, [ |
| 122 | + { |
| 123 | + status: 'passed', |
| 124 | + snapshotResults: {'test 1': {pass: true}}, |
| 125 | + }, |
| 126 | + {status, snapshotResults: {}}, |
| 127 | + ]); |
| 128 | + expect(fs.readFileSync(filePath, 'utf8')).toBe(contents); |
| 129 | + expect(result.unchecked).toBe(0); |
| 130 | + expect(result.uncheckedKeys).toEqual([]); |
| 131 | + } |
| 132 | +}); |
| 133 | + |
| 134 | +it('removes unchecked snapshots after a full successful update', () => { |
| 135 | + const filePath = path.join(directory, 'example.snap'); |
| 136 | + fs.writeFileSync( |
| 137 | + filePath, |
| 138 | + 'exports[`test 1`] = `old hash`;\nexports[`removed test 1`] = `removed hash`;\n', |
| 139 | + ); |
| 140 | + const state = new SnapshotState(filePath, { |
| 141 | + updateSnapshot: 'all', |
| 142 | + rootDir: directory, |
| 143 | + snapshotFormat: {}, |
| 144 | + }); |
| 145 | + updateSnapshotsAndGetJestSnapshotResult(state, [ |
| 146 | + { |
| 147 | + status: 'passed', |
| 148 | + snapshotResults: {'test 1': {pass: true}}, |
| 149 | + }, |
| 150 | + ]); |
| 151 | + expect(fs.readFileSync(filePath, 'utf8')).toContain('old hash'); |
| 152 | + expect(fs.readFileSync(filePath, 'utf8')).not.toContain('removed hash'); |
| 153 | +}); |
0 commit comments