|
| 1 | +/* eslint-disable */ |
| 2 | + |
| 3 | +/** |
| 4 | + * Reference: https://github.com/americanexpress/jest-image-snapshot/blob/main/examples/image-reporter.js |
| 5 | + * |
| 6 | + * To enable this image reporter, add it to your `jest.config.js` "reporters" definition: |
| 7 | + * "reporters": [ "default", "<rootDir>/image-reporter.js" ] |
| 8 | + * |
| 9 | + * Note: Image Reporter may not work with jest's --forceExit flag |
| 10 | + * |
| 11 | + * Note: If image reporter doesn't work in pipeline can try running as standalone script |
| 12 | + * by creating a separate script file and running like this: |
| 13 | + * "test:integration": "jest test/local-development.test || node test/utils/image-reporter-standalone.js" |
| 14 | + */ |
| 15 | + |
| 16 | +const fs = require('fs'); |
| 17 | +const AWS = require('aws-sdk/global'); |
| 18 | +const S3 = require('aws-sdk/clients/s3'); // this is needed |
| 19 | +require('dotenv').config(); |
| 20 | + |
| 21 | +const UPLOAD_BUCKET = process.env.S3_BUCKET_NAME; |
| 22 | + |
| 23 | +if ( |
| 24 | + !process.env.S3_BUCKET_NAME || |
| 25 | + !process.env.AWS_SECRET_ACCESS_KEY || |
| 26 | + !process.env.AWS_ACCESS_KEY_ID |
| 27 | +) { |
| 28 | + console.log('Missing env variables. Skipping upload of image diff files.'); |
| 29 | +} |
| 30 | + |
| 31 | +AWS.config.update({ |
| 32 | + accessKeyId: process.env.AWS_ACCESS_KEY_ID, |
| 33 | + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, |
| 34 | +}); |
| 35 | + |
| 36 | +const s3 = new AWS.S3({ apiVersion: '2006-03-01' }); |
| 37 | + |
| 38 | +const targetDirectories = [ |
| 39 | + './test/__image_snapshots__/', |
| 40 | + './test/__image_snapshots__/__diff_output__/', |
| 41 | +]; |
| 42 | +targetDirectories.forEach((targetDirectory) => { |
| 43 | + fs.readdirSync(targetDirectory, { withFileTypes: true }).forEach((dirent) => { |
| 44 | + if (!dirent.isFile()) return; |
| 45 | + const path = `images/${dirent.name}`; |
| 46 | + const params = { |
| 47 | + Body: fs.readFileSync(`${targetDirectory}/${dirent.name}`), |
| 48 | + Bucket: UPLOAD_BUCKET, |
| 49 | + Key: path, |
| 50 | + ContentType: 'image/png', |
| 51 | + }; |
| 52 | + s3.putObject(params, (err) => { |
| 53 | + if (err) { |
| 54 | + console.log(err, err.stack); |
| 55 | + } else { |
| 56 | + console.log( |
| 57 | + `Uploaded file to https://${UPLOAD_BUCKET}.s3.amazonaws.com/${path}` |
| 58 | + ); |
| 59 | + } |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments