From 27451e69ba9c15adc95a47cf16900c01c03eee85 Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Thu, 28 Mar 2024 14:47:45 +0800 Subject: [PATCH] test: fix toSaveImageSnapshot error --- jest-setup.js | 6 +++--- ...veSnapshot-toSaveImageSnapshot-template.test.js | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/jest-setup.js b/jest-setup.js index 499a5798..32595829 100644 --- a/jest-setup.js +++ b/jest-setup.js @@ -71,7 +71,7 @@ function toSaveSnapshot(received, { let pass = true; try { - checkSnapshotDir(snapshotDir); + checkSnapshotDir(path.dirname(filePath)); fs.writeFileSync(filePath, received); writeTestCaseToSnapshotFile(testPath.replace(`${_rootDir}/`, ""), filePath); } catch (e) { @@ -108,7 +108,7 @@ function toSaveImageSnapshot( SNAPSHOTS_DIR, }); const _fileName = createFileName({ - fileName: customSnapshotIdentifier ? customSnapshotIdentifier() : "", + fileName: customSnapshotIdentifier ? `${customSnapshotIdentifier()}.png` : "", testPath, currentTestName, fileType: "png", @@ -118,7 +118,7 @@ function toSaveImageSnapshot( let pass = true; try { - checkSnapshotDir(snapshotDir); + checkSnapshotDir(path.dirname(filePath)); fs.writeFileSync(filePath, Buffer.from(received, "base64")); writeTestCaseToSnapshotFile(testPath.replace(`${_rootDir}/`, ""), filePath); } catch (e) { diff --git a/pages/toSaveSnapshot-toSaveImageSnapshot-template.test.js b/pages/toSaveSnapshot-toSaveImageSnapshot-template.test.js index 06b47bcf..bfd7db8c 100644 --- a/pages/toSaveSnapshot-toSaveImageSnapshot-template.test.js +++ b/pages/toSaveSnapshot-toSaveImageSnapshot-template.test.js @@ -10,7 +10,7 @@ describe('toSaveSnapshot & toSaveImageSnapshot template', () => { const text2 = 'test-toSaveSnapshot-1-2-' + Date.now() expect(text2).toSaveSnapshot() }) - + it('test toSaveSnapshot 2', async () => { const text1 = 'test-toSaveSnapshot-2-1-' + Date.now() expect(text1).toSaveSnapshot({ @@ -21,13 +21,13 @@ describe('toSaveSnapshot & toSaveImageSnapshot template', () => { expect(text2).toSaveSnapshot({ customSnapshotsDir: './customDir/__file_snapshots__' }) - + const text3 = 'test-toSaveSnapshot-2-3-' + Date.now() expect(text3).toSaveSnapshot({ fileName: 'test-toSaveSnapshot-2-3.json' }) }) - + it('test toSaveImageSnapshot 1', async () => { const img1 = await program.screenshot(); expect(img1).toSaveImageSnapshot() @@ -41,7 +41,7 @@ describe('toSaveSnapshot & toSaveImageSnapshot template', () => { expect(img1).toSaveImageSnapshot({ customSnapshotsDir: './customDir/__image_snapshots__', customSnapshotIdentifier() { - return 'test-toSaveImageSnapshot-2-1.png'; + return 'test-toSaveImageSnapshot-2-1'; }, }) @@ -49,12 +49,12 @@ describe('toSaveSnapshot & toSaveImageSnapshot template', () => { expect(img2).toSaveImageSnapshot({ customSnapshotsDir: './customDir/__image_snapshots__', }) - + const img3 = await program.screenshot(); expect(img3).toSaveImageSnapshot({ customSnapshotIdentifier() { - return 'test-toSaveImageSnapshot-2-3.png'; + return 'test-toSaveImageSnapshot-2-3'; }, }) }) -}); \ No newline at end of file +}); -- GitLab