|
| 1 | +const path = require('path'); |
| 2 | +var fs = require('fs'); |
| 3 | +const restDocs = fs.realpathSync('./test/restDocs'); |
| 4 | + |
| 5 | +const isChildOf = (parent, child) => { |
| 6 | + const relative = path.relative(parent, child); |
| 7 | + return relative && !relative.startsWith('..') && !path.isAbsolute(relative); |
| 8 | +}; |
| 9 | + |
| 10 | +module.exports = { |
| 11 | + // resolves from test to snapshot path |
| 12 | + resolveSnapshotPath: (testPath, snapshotExtension) => { |
| 13 | + if (isChildOf(restDocs, testPath)) { |
| 14 | + return testPath.replace('__tests__', '__snapshots__') + snapshotExtension; |
| 15 | + } |
| 16 | + return path.join( |
| 17 | + path.join(path.dirname(testPath), '__snapshots__'), |
| 18 | + path.basename(testPath) + snapshotExtension |
| 19 | + ); |
| 20 | + }, |
| 21 | + // resolves from snapshot to test path |
| 22 | + resolveTestPath: (snapshotFilePath, snapshotExtension) => { |
| 23 | + if (isChildOf(restDocs, snapshotFilePath)) { |
| 24 | + return snapshotFilePath |
| 25 | + .replace('__snapshots__', '__tests__') |
| 26 | + .slice(0, -snapshotExtension.length); |
| 27 | + } |
| 28 | + return path.join( |
| 29 | + path.dirname(snapshotFilePath), |
| 30 | + '..', |
| 31 | + path.basename(snapshotFilePath, snapshotExtension) |
| 32 | + ); |
| 33 | + }, |
| 34 | + // Example test path, used for preflight consistency check of the implementation above |
| 35 | + testPathForConsistencyCheck: 'some/__tests__/example.test.js', |
| 36 | +}; |
0 commit comments