TLDR: With jest-extended package you can write: expect([...set]).toIncludeSameMembers([value1, value2]);. If you are looking to a native, but longer solution scroll down a bit.

Table of contents

    Let's say that you want to create a tic tac toe game solver and need to test if a function that returns possible moves from a given state works properly.

    const state = [
      ['X', 'O', 'X'],
      ['X', 'O', null],
      ['O', null, 'X'],
    ];
    const possibleMoves = new Set();
    possibleMoves.add([
      ['X', 'O', 'X'],
      ['X', 'O', null],
      ['O', 'O', 'X'],
    ]);
    possibleMoves.add([
      ['X', 'O', 'X'],
      ['X', 'O', 'O'],
      ['O', null, 'X'],
    ]);
    const foundPossibleMoves = getPossibleMoves(state, 'O');

    Jest doesn't have a built-in functions for working with a set data structure, but it greatly supports comparing arrays. You can convert set to array and check for inclusion (elements ordering does not matter), but then you also need to check if sets have equal sizes:

    expect([...foundPossibleMoves]).toEqual(
      expect.arrayContaining([...possibleMoves]),
    );
    expect(foundPossibleMoves.size).toEqual(possibleMoves.size);

    Fortunately, jest-extended package contains matcher named toIncludeSameMembers that does the both:

    expect([...foundPossibleMoves]).toIncludeSameMembers([...possibleMoves]);

    Jest performs deep elements comparison, so the check above should pass.

    jest array compare jest array equality jest expect to not contain jest expects set.contains expected array all the elements test values code examples error message custom matcher received array

    Artur Ziętkiewicz
    Artur Ziętkiewicz Elixir & React Developer

    Read more
    on #curiosum blog

    async defer javascript file html parsing src attribute async scripts javascript files defer parsing javascript defer attributes

    Async vs Defer - Which Script Tag Attribute is More Efficient when Loading JavaScript?

    JavaScript is the most popular language in the world. Why? Because it's the only well-adopted language supported by Web Browsers that brings dynamics to the frontend experience.

    Because of that, almost any web application requires developers to write at least a bit of JavaScript code. While it's not a problem (unless you hate this language 😉), some of us tend to create very complex, frontend-heavy applications that load.. well, definitely not fast.

    In this article, you'll get familiar with async and defer - two script tag attributes that will speed up your website's rendering.