|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 American Express Travel Related Services Company, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +const fs = require('fs'); |
| 16 | +const os = require('os'); |
| 17 | +const childProcess = require('child_process'); |
| 18 | +const path = require('path'); |
| 19 | +const rimraf = require('rimraf'); |
| 20 | + |
| 21 | +describe('OutdatedSnapshotReporter', () => { |
| 22 | + const jestImageSnapshotDir = path.join(__dirname, '..'); |
| 23 | + const imagePath = path.join(__dirname, 'stubs/TestImage.png'); |
| 24 | + const jestBinPath = path.join(jestImageSnapshotDir, 'node_modules/.bin/jest'); |
| 25 | + let tmpDir = os.tmpdir(); |
| 26 | + |
| 27 | + function setupTestProject(dir) { |
| 28 | + const jestConfig = { |
| 29 | + reporters: [ |
| 30 | + 'default', |
| 31 | + `${jestImageSnapshotDir}/src/outdated-snapshot-reporter.js`, |
| 32 | + ], |
| 33 | + }; |
| 34 | + const jestConfigFile = `module.exports = ${JSON.stringify(jestConfig)}`; |
| 35 | + |
| 36 | + const commonTest = ` |
| 37 | + const fs = require('fs'); |
| 38 | + const {toMatchImageSnapshot} = require('${jestImageSnapshotDir}'); |
| 39 | + expect.extend({toMatchImageSnapshot}); |
| 40 | + `; |
| 41 | + const imageTest = `${commonTest} |
| 42 | + it('should run an image snapshot test', () => { |
| 43 | + expect(fs.readFileSync('image.png')).toMatchImageSnapshot(); |
| 44 | + }); |
| 45 | + `; |
| 46 | + const doubleTest = `${imageTest} |
| 47 | + it('should run an image snapshot test', () => { |
| 48 | + expect(fs.readFileSync('image.png')).toMatchImageSnapshot(); |
| 49 | + }); |
| 50 | + `; |
| 51 | + |
| 52 | + fs.writeFileSync(path.join(dir, 'jest.config.js'), jestConfigFile); |
| 53 | + fs.writeFileSync(path.join(dir, 'image.test.js'), imageTest); |
| 54 | + fs.writeFileSync(path.join(dir, 'double.test.js'), doubleTest); |
| 55 | + fs.copyFileSync(imagePath, path.join(dir, 'image.png')); |
| 56 | + } |
| 57 | + |
| 58 | + function runJest(cliArgs, environment = {}) { |
| 59 | + return childProcess.spawnSync(jestBinPath, cliArgs, { |
| 60 | + cwd: tmpDir, |
| 61 | + encoding: 'utf-8', |
| 62 | + env: { ...process.env, ...environment }, |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + function getSnapshotFiles() { |
| 67 | + return fs.readdirSync(path.join(tmpDir, '__image_snapshots__')); |
| 68 | + } |
| 69 | + |
| 70 | + beforeAll(() => { |
| 71 | + tmpDir = fs.mkdtempSync( |
| 72 | + path.join(os.tmpdir(), 'jest-image-snapshot-tests') |
| 73 | + ); |
| 74 | + setupTestProject(tmpDir); |
| 75 | + }); |
| 76 | + |
| 77 | + afterAll(() => { |
| 78 | + rimraf.sync(tmpDir); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should write the image snapshot on first run', () => { |
| 82 | + const { status, stdout, stderr } = runJest(['-u']); |
| 83 | + expect(stderr).toContain('snapshots written'); |
| 84 | + expect(status).toEqual(0); |
| 85 | + expect(stdout).toEqual(''); |
| 86 | + |
| 87 | + expect(getSnapshotFiles()).toHaveLength(3); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should not delete the snapshot when environment flag is not enabled', () => { |
| 91 | + const { status, stdout } = runJest(['-u', 'image.test.js']); |
| 92 | + expect(status).toEqual(0); |
| 93 | + expect(stdout).toEqual(''); |
| 94 | + |
| 95 | + expect(getSnapshotFiles()).toHaveLength(3); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should delete the snapshot when environment flag is enabled', () => { |
| 99 | + const { status, stdout, stderr } = runJest(['-u', 'image.test.js'], { |
| 100 | + JEST_IMAGE_SNAPSHOT_TRACK_OBSOLETE: '1', |
| 101 | + }); |
| 102 | + expect(stderr).toContain('outdated snapshot'); |
| 103 | + expect(status).toEqual(0); |
| 104 | + expect(stdout).toEqual(''); |
| 105 | + |
| 106 | + expect(getSnapshotFiles()).toHaveLength(1); |
| 107 | + }); |
| 108 | +}); |
0 commit comments