Skip to content

Menu

Assertion Generator

Generate test assertions from values

Runs entirely in your browserTesting & QAFree, no sign-up
Loading tool

01What is Assertion Generator?

Writing assertions is rarely hard, but switching between Jest, Chai and node:assert means looking up matcher names again. This tool takes the value your code returns and writes a list of assertions that all pass for that value, in the framework you pick. You fill in Actual Value, optionally Expected Value, choose Jest, Vitest, Chai, Node Assert or Playwright, and press Generate. Actual Value is parsed as a literal. JSON works first. If that fails, the tool accepts JavaScript-style literals with single or backtick quotes, unquoted keys and trailing commas, so { name: 'Alice', age: 30, } is fine. Nothing you type is executed with eval. If it can't read the value, you get a comment asking for a literal instead of guessed code. Expected Value is used in whatever way fits. If it equals the actual value it drives the equality line. A substring of a string drives toContain and toMatch. An element of an array drives toContain or toContainEqual, and a list of elements drives expect.arrayContaining. An object subset drives toMatchObject. A bare word or dot path such as user.id drives toHaveProperty. If none of those apply, the first line is a comment saying so and the equality check falls back to the actual value. The matcher choice follows each library's documented semantics. Primitives get toBe. Arrays and objects get toEqual and toStrictEqual, never toBe, because toBe compares references. Lengths and key counts come from the parsed value, and regex special characters are escaped, so $5.00 becomes /\$5\.00/. Chai uses deep.equal, nested.property and include.deep.members. Node Assert uses strictEqual, deepStrictEqual, assert.match and Object.hasOwn. Playwright treats the value as on-page text, a list of texts or an element count, and adds toBeVisible and toBeEnabled. For example, Actual [1, 2, 3] with Expected [1, 2, 3] in Jest gives eight lines. They start with toEqual, toStrictEqual and toHaveLength(3), then arrayContaining and the null and truthy checks. Some limits. The tool sees a value, not your function, so it can't write toThrow, async rejection or mock checks. Values JSON can't hold, such as Date, Map, NaN or class instances, won't parse. In Playwright mode a plain object gets only a comment plus the visibility checks. Treat the list as a menu and keep the one or two lines that state what the test is about.

JavaScript and TypeScript developers who move between Jest, Vitest, Chai, node:assert and Playwright and want matcher syntax without checking the docs. The tool runs entirely in your browser, so your data never leaves your device.

02How to use Assertion Generator

  1. Enter the value your code returns in Actual Value, for example [1, 2, 3], { id: 1 } or "hello".
  2. Optionally enter Expected Value: the same value, a substring, an element, an object subset or a property path like user.id.
  3. Choose Jest, Vitest, Chai, Node Assert or Playwright from the Framework dropdown.
  4. Click Generate to build the assertion list, grouped by category.
  5. Copy single lines with their copy icon, or use Copy All to grab the whole list.

03Worked examples

Input: Actual [1, 2, 3], Expected [1, 2, 3], Jest
Result: expect(result).toEqual([1, 2, 3]); expect(result).toStrictEqual([1, 2, 3]); expect(result).toHaveLength(3); expect(result).toEqual(expect.arrayContaining([1, 2, 3])); expect(result).not.toHaveLength(0);
First 5 of 8 lines. The rest are toBeDefined, not.toBeNull and toBeTruthy.
Input: Actual { name: 'Alice', age: 30, }, Expected name, Jest
Result: expect(result).toEqual({ name: "Alice", age: 30 }); expect(result).toStrictEqual({ name: "Alice", age: 30 }); expect(result).toHaveProperty("name"); expect(Object.keys(result)).toHaveLength(2);
Single quotes and the trailing comma are accepted. Three truthy checks follow and are left out here.
Input: Actual { user: { id: 7 }, ok: true }, Expected user.id, Chai
Result: expect(result).to.deep.equal({ user: { id: 7 }, ok: true }); expect(result).to.have.nested.property("user.id"); expect(Object.keys(result)).to.have.lengthOf(2); expect(result).to.exist; expect(result).to.be.ok;
A dot path switches Chai to nested.property. This is the full output.
Input: Actual "price: $5.00", Expected "$5.00", Node Assert
Result: assert.strictEqual(result, "price: $5.00"); assert.match(result, /\$5\.00/); assert.strictEqual(result.length, 12); assert.notStrictEqual(result, null); assert.ok(result);
The $ and . are escaped in the regex. Full output.
Input: Actual ["Home", "Docs"], Expected empty, Playwright
Result: await expect(locator).toHaveText(["Home", "Docs"]); await expect(locator).toHaveCount(2); await expect(locator).toBeVisible(); await expect(locator).toBeEnabled();
An array of strings is read as the texts of matched elements, in order.
Input: Actual 42, Expected 7, Jest
Result: // Expected Value doesn't match, isn't contained in, and isn't a subset of Actual Value, so equality uses Actual Value. expect(result).toBe(42); expect(result).toBeGreaterThanOrEqual(42); expect(result).toBeLessThanOrEqual(42);
A mismatched Expected Value is flagged, not asserted. Three truthy checks follow.

04Common use cases

  • Scaffolding the expect() block for a new Vitest test after logging a function's return value to the console
  • Remembering Chai syntax for nested.property or include.deep.members while working in an older Mocha suite
  • Writing node:test checks with assert.deepStrictEqual for a small library that avoids test dependencies
  • Drafting Playwright checks like toHaveText with an array of nav labels for an end-to-end smoke test
  • Turning a copied API response object into toMatchObject and toHaveProperty checks for a contract test
  • Showing a junior developer why an array needs toEqual and a number needs toBe

05Tips

  • Quote your strings. A bare word in Actual Value is not a literal, so you get a comment instead of assertions.
  • For objects, an unquoted Expected Value like user.id is read as a property path. That gives toHaveProperty in Jest and nested.property in Chai.
  • If the first line is a comment saying Expected Value doesn't match, check for a typo. The equality line then uses Actual Value, not your expected one.
  • Node Assert only adds Object.hasOwn for top-level keys. A dot path like user.id gets no property line in that mode.
  • Errors aren't covered. Wrap the call yourself, as in expect(() => parse("")).toThrow(), since passing the result runs it too early.

06Related concepts

Unit testing, Deep equality, Test matchers, Arrange-Act-Assert, End-to-end testing, node:assert.

Frequently Asked Questions

What is Assertion Generator?

Assertion Generator is a free, browser-based developer tool on AIDToolStack. Generate test assertions from values It requires no installation or sign-up and works entirely in your browser.

Is Assertion Generator free to use?

Yes. No sign-up, no fees and no usage cap.

Does Assertion Generator store my data?

No. It runs in your browser; nothing is uploaded.

Reviews and commentsShow

Reviews

Sign in to leave a review.

Loading reviews…

Comments

Sign in to leave a comment.

Loading comments…

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe