Skip to content

يدعم مُشغل اختبارات Bun اختبار اللقطات على غرار Jest عبر .toMatchSnapshot().

ts
import { test, expect } from "bun:test";

test("لقطة", () => {
  expect({ foo: "bar" }).toMatchSnapshot();
});

في المرة الأولى التي يتم فيها تنفيذ هذا الاختبار، سيقوم Bun بتقييم القيمة المُمررة إلى expect() وكتابتها إلى القرص في دليل يسمى __snapshots__ يوجد بجانب ملف الاختبار. (لاحظ سطر snapshots: +1 added في الناتج.)

sh
bun test test/snap
txt
test/snap.test.ts:
✓ snapshot [1.48ms]

 1 pass
 0 fail
 snapshots: +1 added
 1 expect() calls
Ran 1 tests across 1 files. [82.00ms]

يحتوي دليل __snapshots__ على ملف .snap لكل ملف اختبار في الدليل.

txt
test
├── __snapshots__
│   └── snap.test.ts.snap
└── snap.test.ts

ملف snap.test.ts.snap هو ملف JavaScript يُصدر نسخة مُسلسلة من القيمة المُمررة إلى expect(). تم تسلسل الكائن {foo: "bar"} إلى JSON.

js
// Bun Snapshot v1, https://bun.com/docs/test/snapshots

exports[`snapshot 1`] = `
{
  "foo": "bar",
}
`;

لاحقًا، عند تنفيذ ملف الاختبار هذا مرة أخرى، سيقرأ Bun ملف اللقطة ويقارنه بالقيمة المُمررة إلى expect(). إذا كانت القيم مختلفة، فسيفشل الاختبار.

sh
bun test
txt
bun test v1.3.3 (9c68abdb)
test/snap.test.ts:
✓ snapshot [1.05ms]

 1 pass
 0 fail
 1 snapshots, 1 expect() calls
Ran 1 tests across 1 files. [101.00ms]

لتحديث اللقطات، استخدم العلم --update-snapshots.

sh
bun test --update-snapshots
txt
bun test v1.3.3 (9c68abdb)
test/snap.test.ts:
✓ snapshot [0.86ms]

 1 pass
 0 fail
 snapshots: +1 added  # تم إعادة إنشاء اللقطة
 1 expect() calls
Ran 1 tests across 1 files. [102.00ms]

راجع الوثائق > مُشغل الاختبارات > اللقطات للحصول على الوثائق الكاملة للقطات مع مُشغل اختبارات Bun.

Bun بواسطة www.bunjs.com.cn تحرير