Əsas məzmuna keç
FrontDepth, Ana səhifə

Mövzuya qayıt

Məşq: Nəyi və hansı səviyyədə test etməli

Əvvəlcə cavab ver, sonra müqayisə et. Özünü dürüst qiymətləndir — zəif yerlərin siyahısı buna əsasən qurulur.

3 nəticə sualı · 4 açıq sual

Sualların səviyyəsi

Seçilmiş səviyyəyə qədər olan suallar göstərilir.

Bu səviyyə üçün hələ məşq sualı yoxdur.

  1. Nəticəni təxmin etJunior

    Bu, Vitest deyil, test runner-in adi JS simulyasiyasıdır: test() funksiyanı işə salır və o, xəta atmırsa, "pass" yazır, expect().toBe() isə dəyərlər Object.is-ə görə fərqlənəndə, Vitest-in toBe-si kimi, xəta atır. cartTotal-da bug var. Konsola nə çıxacaq?

    JavaScript
    const results = [];
    
    function test(name, fn) {
      try {
        fn();
        results.push("pass " + name);
      } catch (error) {
        results.push("fail " + name + ": " + error.message);
      }
    }
    
    function expect(actual) {
      return {
        toBe(expected) {
          if (!Object.is(actual, expected)) {
            throw new Error("expected " + expected + ", got " + actual);
          }
        },
      };
    }
    
    function cartTotal(items) {
      let total = 0;
      for (const item of items) total += item.price;
      return total;
    }
    
    test("sums one item", () => {
      expect(cartTotal([{ price: 4, quantity: 1 }])).toBe(4);
    });
    
    test("multiplies by quantity", () => {
      expect(cartTotal([{ price: 4, quantity: 2 }])).toBe(8);
    });
    
    test("handles two items", () => {
      cartTotal([
        { price: 4, quantity: 2 },
        { price: 10, quantity: 1 },
      ]);
    });
    
    for (const line of results) console.log(line);
  2. Açıq sualJunior

    Unit, inteqrasiya və end-to-end testlər nədir və hər biri nəyi tutur və neçəyə başa gəlir?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.

  3. Nəticəni təxmin etMiddle

    Bu, səbətin üç versiyası üzərində işə salınan iki testin adi JS simulyasiyasıdır: V1 ilkin versiyadır, V2 item-ləri Map-də saxlayan refaktorinqdir, V3 isə V1-in saxlama üsulunu saxlayır, amma total()-ı dəyişir. internalsTest private _items-i oxuyur, behaviourTest isə yalnız public API-dən istifadə edir. Hər sətir iki testin keçib-keçmədiyini göstərir. Konsola nə çıxacaq?

    JavaScript
    function createCartV1() {
      const items = [];
      return {
        _items: items,
        add(item) {
          const found = items.find((i) => i.id === item.id);
          if (found) found.quantity += 1;
          else items.push({ ...item, quantity: 1 });
        },
        total() {
          return items.reduce((sum, i) => sum + i.price * i.quantity, 0);
        },
      };
    }
    
    function createCartV2() {
      const items = new Map();
      return {
        _items: items,
        add(item) {
          const quantity = (items.get(item.id)?.quantity ?? 0) + 1;
          items.set(item.id, { ...item, quantity });
        },
        total() {
          let sum = 0;
          for (const i of items.values()) sum += i.price * i.quantity;
          return sum;
        },
      };
    }
    
    function createCartV3() {
      const cart = createCartV1();
      cart.total = () => cart._items.reduce((sum, i) => sum + i.price, 0);
      return cart;
    }
    
    function internalsTest(createCart) {
      const cart = createCart();
      cart.add({ id: "tea", price: 4 });
      cart.add({ id: "tea", price: 4 });
      return Array.isArray(cart._items) && cart._items[0].quantity === 2;
    }
    
    function behaviourTest(createCart) {
      const cart = createCart();
      cart.add({ id: "tea", price: 4 });
      cart.add({ id: "tea", price: 4 });
      return cart.total() === 8;
    }
    
    for (const createCart of [createCartV1, createCartV2, createCartV3]) {
      console.log(internalsTest(createCart), behaviourTest(createCart));
    }
  4. Nəticəni təxmin etMiddle

    Bu, branch coverage-in adi JS simulyasiyasıdır: coverage alətinin kodu instrumentasiya etdiyi kimi, discount iki budağından hansının işlədiyini qeyd edir, coverage() isə işləmiş budaqların payını göstərir. İlk iki çağırış assertion-sız testləri, üçüncüsü isə assertion-lı testi təmsil edir. Konsola nə çıxacaq?

    JavaScript
    const branchesRun = new Set();
    
    function discount(total, code) {
      if (code === "VIP") {
        branchesRun.add("vip");
        return total * 0.8;
      }
      branchesRun.add("regular");
      return total - 10;
    }
    
    const coverage = () => (branchesRun.size / 2) * 100 + "%";
    
    discount(100, "VIP");
    console.log(coverage());
    
    discount(100, "");
    console.log(coverage());
    
    console.log(discount(100, "") === 90);
    console.log(discount(5, ""));
  5. Açıq sualMiddle

    Test piramidası ilə testing trophy-ni müqayisə et. React tətbiqi üçün hansına əməl edərdin və niyə?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.

  6. Açıq sualMiddle

    "Davranışı test et, implementasiya detallarını yox" nə deməkdir və bu qaydanı pozan testi necə tanıyırsan?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.

  7. Açıq sualSenior

    Bug production-a çatıb. Onun üçün hansı testi yazacağına necə qərar verirsən və bütün test dəstini CI-da necə etibarlı saxlayırsan?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.