مرجع سريع

ورقة غش إطار اختبار جافاسكريبت QUnit

دليل مرجعي سريع لـ QUnit: إطار اختبار وحدات جافاسكريبت، الوحدات، التوكيدات، الخطافات غير المتزامنة، ومشغلات DOM.

· تاريخ النشر

QUnit هو إطار عمل قوي وسهل الاستخدام لاختبار وحدات JavaScript يُستخدم في مشاريع رائدة مثل jQuery و jQuery UI و Ember.js.

هيكل مجموعة الاختبارات (Test Suite Structure)

QUnit.module('Math operations', (hooks) => {
  hooks.beforeEach(() => {
    // Setup before each test
  });

  hooks.afterEach(() => {
    // Teardown after each test
  });

  QUnit.test('adds two numbers', (assert) => {
    assert.equal(1 + 1, 2, '1 + 1 should equal 2');
    assert.strictEqual(2 + 2, 4, 'strictly equals 4');
  });
});

دليل دوال التوكيد (Assertion Reference)

Assertion Description
assert.ok(state, [message]) Asserts value is truthy
assert.notOk(state, [message]) Asserts value is falsy
assert.equal(actual, expected) Non-strict comparison (==)
assert.strictEqual(actual, expected) Strict comparison (===)
assert.deepEqual(actual, expected) Deep recursive equality on objects/arrays
assert.throws(fn, [expected]) Asserts a function throws an error

الاختبارات غير المتزامنة (Asynchronous Tests)

// Modern Promise / Async Await
QUnit.test('fetches user profile', async (assert) => {
  const profile = await fetchProfile(10);
  assert.equal(profile.name, 'Alice');
});
// Callback Done Handler
QUnit.test('timer test', (assert) => {
  const done = assert.async();

  setTimeout(() => {
    assert.ok(true, 'Timer completed');
    done();
  }, 100);
});

أوراق غش ذات صلة

موضوعات ذات صلة