jest-setup.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. const path = require("path");
  2. const fs = require("fs");
  3. const {
  4. configureToMatchImageSnapshot
  5. } = require('jest-image-snapshot');
  6. let saveImageSnapshotDir = process.env.saveImageSnapshotDir || path.join(__dirname, '__snapshot__');
  7. expect.extend({
  8. toMatchImageSnapshot: configureToMatchImageSnapshot({
  9. customSnapshotIdentifier(args) {
  10. return args.currentTestName.replace(/\//g, "-").replace(" ", "-");
  11. },
  12. customSnapshotsDir: process.env.saveImageSnapshotDir,
  13. customDiffDir: path.join(saveImageSnapshotDir, "diff"),
  14. }),
  15. toSaveSnapshot,
  16. toSaveImageSnapshot,
  17. });
  18. const testCaseToSnapshotFilePath =
  19. process.env.testCaseToSnapshotFilePath || "./testCaseToSnapshotFilePath.json";
  20. if (!fs.existsSync(testCaseToSnapshotFilePath)) {
  21. fs.writeFileSync(testCaseToSnapshotFilePath, "{}");
  22. }
  23. function writeTestCaseToSnapshotFile(testCaseName, snapshotFilePath) {
  24. const data = JSON.parse(fs.readFileSync(testCaseToSnapshotFilePath));
  25. if (testCaseName.includes(__dirname)) {
  26. testCaseName = testCaseName.substring(`${__dirname}`.length);
  27. if (testCaseName[0] == '/' || testCaseName[0] == '\\') {
  28. testCaseName = testCaseName.substring(1);
  29. };
  30. };
  31. if (!data[testCaseName]) {
  32. data[testCaseName] = [snapshotFilePath];
  33. } else {
  34. data[testCaseName].push(snapshotFilePath);
  35. }
  36. fs.writeFileSync(testCaseToSnapshotFilePath, JSON.stringify(data, null, 2));
  37. }
  38. function toSaveSnapshot(received, {
  39. customSnapshotsDir,
  40. fileName
  41. } = {}) {
  42. const {
  43. snapshotState: {
  44. _rootDir
  45. },
  46. testPath,
  47. currentTestName,
  48. } = this;
  49. const SNAPSHOTS_DIR = "__file_snapshots__";
  50. const snapshotDir =
  51. process.env.saveSnapshotDir ||
  52. createSnapshotDir({
  53. customSnapshotsDir,
  54. testPath,
  55. SNAPSHOTS_DIR,
  56. });
  57. const _fileName = createFileName({
  58. fileName,
  59. testPath,
  60. currentTestName,
  61. });
  62. const filePath = path.join(snapshotDir, _fileName);
  63. let message = () => `${currentTestName} toSaveSnapshot success`;
  64. let pass = true;
  65. try {
  66. checkSnapshotDir(path.dirname(filePath));
  67. fs.writeFileSync(filePath, received);
  68. writeTestCaseToSnapshotFile(testPath.replace(`${_rootDir}/`, ""), filePath);
  69. } catch (e) {
  70. console.log("toSaveSnapshot fail", e);
  71. message = () => e.message;
  72. pass = false;
  73. }
  74. return {
  75. message,
  76. pass,
  77. };
  78. }
  79. function toSaveImageSnapshot(
  80. received, {
  81. customSnapshotsDir,
  82. customSnapshotIdentifier
  83. } = {}
  84. ) {
  85. const {
  86. snapshotState: {
  87. _rootDir
  88. },
  89. testPath,
  90. currentTestName,
  91. } = this;
  92. const SNAPSHOTS_DIR = "__image_snapshots__";
  93. const snapshotDir =
  94. process.env.saveImageSnapshotDir ||
  95. createSnapshotDir({
  96. customSnapshotsDir,
  97. testPath,
  98. SNAPSHOTS_DIR,
  99. });
  100. const _fileName = createFileName({
  101. fileName: customSnapshotIdentifier ? `${customSnapshotIdentifier()}.png` : "",
  102. testPath,
  103. currentTestName,
  104. fileType: "png",
  105. });
  106. const filePath = path.join(snapshotDir, _fileName);
  107. let message = () => `${currentTestName} toSaveImageSnapshot success`;
  108. let pass = true;
  109. try {
  110. checkSnapshotDir(path.dirname(filePath));
  111. fs.writeFileSync(filePath, Buffer.from(received, "base64"));
  112. writeTestCaseToSnapshotFile(testPath.replace(`${_rootDir}/`, ""), filePath);
  113. } catch (e) {
  114. console.log("toSaveImageSnapshot fail", e);
  115. message = () => e.message;
  116. pass = false;
  117. }
  118. return {
  119. message,
  120. pass,
  121. };
  122. }
  123. function createSnapshotDir({
  124. customSnapshotsDir,
  125. testPath,
  126. SNAPSHOTS_DIR
  127. }) {
  128. return customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR);
  129. }
  130. function createFileName({
  131. fileName,
  132. testPath,
  133. currentTestName,
  134. fileType
  135. }) {
  136. return (
  137. fileName ||
  138. createSnapshotIdentifier({
  139. testPath,
  140. currentTestName,
  141. fileType,
  142. })
  143. );
  144. }
  145. function createSnapshotIdentifier({
  146. testPath,
  147. currentTestName,
  148. fileType = "txt",
  149. }) {
  150. const snapshotIdentifier = kebabCase(
  151. `${path.basename(testPath)}-${currentTestName}`
  152. );
  153. const counter = timesCalled.get(`${snapshotIdentifier}-${fileType}`) || 1;
  154. timesCalled.set(`${snapshotIdentifier}-${fileType}`, counter + 1);
  155. return `${snapshotIdentifier}-${counter}.${fileType}`;
  156. }
  157. function kebabCase(str) {
  158. return str
  159. .replaceAll(/([a-z])([A-Z])/g, "$1-$2")
  160. .replaceAll(/\s+/g, "-")
  161. .replaceAll(/_+/g, "-")
  162. .replaceAll(/\/+/g, "-")
  163. .replaceAll(/\.+/g, "-")
  164. .toLowerCase();
  165. }
  166. function checkSnapshotDir(snapshotDir) {
  167. if (!fs.existsSync(snapshotDir)) {
  168. fs.mkdirSync(snapshotDir, {
  169. recursive: true,
  170. });
  171. }
  172. }
  173. const timesCalled = new Map();