-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add obsolete snapshot reporting (#222)
Co-authored-by: Andres Escobar <[email protected]>
- Loading branch information
1 parent
621d2f3
commit 47da7c2
Showing
5 changed files
with
216 additions
and
0 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) 2020 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
const fs = require('fs'); | ||
const os = require('os'); | ||
const childProcess = require('child_process'); | ||
const path = require('path'); | ||
const rimraf = require('rimraf'); | ||
|
||
describe('OutdatedSnapshotReporter', () => { | ||
const jestImageSnapshotDir = path.join(__dirname, '..'); | ||
const imagePath = path.join(__dirname, 'stubs/TestImage.png'); | ||
const jestBinPath = path.join(jestImageSnapshotDir, 'node_modules/.bin/jest'); | ||
let tmpDir = os.tmpdir(); | ||
|
||
function setupTestProject(dir) { | ||
const jestConfig = { | ||
reporters: [ | ||
'default', | ||
`${jestImageSnapshotDir}/src/outdated-snapshot-reporter.js`, | ||
], | ||
}; | ||
const jestConfigFile = `module.exports = ${JSON.stringify(jestConfig)}`; | ||
|
||
const commonTest = ` | ||
const fs = require('fs'); | ||
const {toMatchImageSnapshot} = require('${jestImageSnapshotDir}'); | ||
expect.extend({toMatchImageSnapshot}); | ||
`; | ||
const imageTest = `${commonTest} | ||
it('should run an image snapshot test', () => { | ||
expect(fs.readFileSync('image.png')).toMatchImageSnapshot(); | ||
}); | ||
`; | ||
const doubleTest = `${imageTest} | ||
it('should run an image snapshot test', () => { | ||
expect(fs.readFileSync('image.png')).toMatchImageSnapshot(); | ||
}); | ||
`; | ||
|
||
fs.writeFileSync(path.join(dir, 'jest.config.js'), jestConfigFile); | ||
fs.writeFileSync(path.join(dir, 'image.test.js'), imageTest); | ||
fs.writeFileSync(path.join(dir, 'double.test.js'), doubleTest); | ||
fs.copyFileSync(imagePath, path.join(dir, 'image.png')); | ||
} | ||
|
||
function runJest(cliArgs, environment = {}) { | ||
return childProcess.spawnSync(jestBinPath, cliArgs, { | ||
cwd: tmpDir, | ||
encoding: 'utf-8', | ||
env: { ...process.env, ...environment }, | ||
}); | ||
} | ||
|
||
function getSnapshotFiles() { | ||
return fs.readdirSync(path.join(tmpDir, '__image_snapshots__')); | ||
} | ||
|
||
beforeAll(() => { | ||
tmpDir = fs.mkdtempSync( | ||
path.join(os.tmpdir(), 'jest-image-snapshot-tests') | ||
); | ||
setupTestProject(tmpDir); | ||
}); | ||
|
||
afterAll(() => { | ||
rimraf.sync(tmpDir); | ||
}); | ||
|
||
it('should write the image snapshot on first run', () => { | ||
const { status, stdout, stderr } = runJest(['-u']); | ||
expect(stderr).toContain('snapshots written'); | ||
expect(status).toEqual(0); | ||
expect(stdout).toEqual(''); | ||
|
||
expect(getSnapshotFiles()).toHaveLength(3); | ||
}); | ||
|
||
it('should not delete the snapshot when environment flag is not enabled', () => { | ||
const { status, stdout } = runJest(['-u', 'image.test.js']); | ||
expect(status).toEqual(0); | ||
expect(stdout).toEqual(''); | ||
|
||
expect(getSnapshotFiles()).toHaveLength(3); | ||
}); | ||
|
||
it('should delete the snapshot when environment flag is enabled', () => { | ||
const { status, stdout, stderr } = runJest(['-u', 'image.test.js'], { | ||
JEST_IMAGE_SNAPSHOT_TRACK_OBSOLETE: '1', | ||
}); | ||
expect(stderr).toContain('outdated snapshot'); | ||
expect(status).toEqual(0); | ||
expect(stdout).toEqual(''); | ||
|
||
expect(getSnapshotFiles()).toHaveLength(1); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2020 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
/* eslint-disable class-methods-use-this */ | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const TOUCHED_FILE_LIST_PATH = path.join( | ||
process.cwd(), | ||
'.jest-image-snapshot-touched-files' | ||
); | ||
|
||
const IS_ENABLED = !!process.env.JEST_IMAGE_SNAPSHOT_TRACK_OBSOLETE; | ||
|
||
class OutdatedSnapshotReporter { | ||
/* istanbul ignore next - test coverage in child process */ | ||
static markTouchedFile(filePath) { | ||
if (!IS_ENABLED) return; | ||
const touchedListFileDescriptor = fs.openSync(TOUCHED_FILE_LIST_PATH, 'as'); | ||
fs.writeSync(touchedListFileDescriptor, `${filePath}\n`); | ||
fs.closeSync(touchedListFileDescriptor); | ||
} | ||
|
||
/* istanbul ignore next - test coverage in child process */ | ||
static readTouchedFileListFromDisk() { | ||
if (!fs.existsSync(TOUCHED_FILE_LIST_PATH)) return []; | ||
|
||
return Array.from( | ||
new Set( | ||
fs | ||
.readFileSync(TOUCHED_FILE_LIST_PATH, 'utf-8') | ||
.split('\n') | ||
.filter(file => file && fs.existsSync(file)) | ||
) | ||
); | ||
} | ||
|
||
/* istanbul ignore next - test coverage in child process */ | ||
onRunStart() { | ||
if (!IS_ENABLED) return; | ||
if (fs.existsSync(TOUCHED_FILE_LIST_PATH)) { | ||
fs.unlinkSync(TOUCHED_FILE_LIST_PATH); | ||
} | ||
} | ||
|
||
/* istanbul ignore next - test coverage in child process */ | ||
onRunComplete() { | ||
if (!IS_ENABLED) return; | ||
const touchedFiles = OutdatedSnapshotReporter.readTouchedFileListFromDisk(); | ||
const imageSnapshotDirectories = Array.from( | ||
new Set(touchedFiles.map(file => path.dirname(file))) | ||
); | ||
const allFiles = imageSnapshotDirectories | ||
.map(dir => fs.readdirSync(dir).map(file => path.join(dir, file))) | ||
.reduce((a, b) => a.concat(b), []) | ||
.filter(file => file.endsWith('-snap.png')); | ||
const obsoleteFiles = allFiles.filter( | ||
file => !touchedFiles.includes(file) | ||
); | ||
|
||
if (fs.existsSync(TOUCHED_FILE_LIST_PATH)) { | ||
fs.unlinkSync(TOUCHED_FILE_LIST_PATH); | ||
} | ||
|
||
obsoleteFiles.forEach((file) => { | ||
process.stderr.write(`Deleting outdated snapshot "${file}"...\n`); | ||
fs.unlinkSync(file); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = OutdatedSnapshotReporter; |