Coder Social home page Coder Social logo

vitest-iterator's Introduction

vitest-iterator

npm version

Additional vitest matchers for iterators and generators.

Installation

Pnpm: pnpm i -D vitest-iterator

Yarn: yarn add -D vitest-iterator

Npm: npm i -D vitest-iterator

Setup

Add to vitest.config.ts

import { defineConfig } from 'vitest/config';

export default defineConfig({
  // ...
  test: {
    setupFiles: ['vitest-iterator'],
  },
});

TypeScript

Add a .d.ts file with the following triple-slash directive:

/// <reference types="vitest-iterator/assertion" />

Alternatively, add a types entry in compilerOptions:

{
  "compilerOptions": {
    "types": ["vitest-iterator/assertion"]
  }
}

Usage

test('counts to three', () => {
  const iterator = [0, 1, 2].values();
  expect(iterator).next.toBe(0);
  expect(iterator).next.toBe(1);
  expect(iterator).next.toBe(2);
  expect(iterator).next.toBeDone();
});

.toBeDone() can be used on IteratorResult directly.

test('counts', () => {
  const iterator = [1, 2, 3].values();
  const result = iterator.next();
  expect(result).toHaveProperty('value', 0);
  expect(result).not.toBeDone();
});

This can be used to test generators as well since they implement the iterator protocol.

function* exampleGenerator() {
  yield 1;
}

test('testing generator', () => {
  const iterator = exampleGenerator();
  expect(iterator).next.toBe(1);
  expect(iterator).next.toBeDone();
});

API

.next

Also under the alias: .yields.

Use the .next modifier to unwrap the value in the IteratorResult so that any other matcher can be chained. This can be called with generators as well since they implement the iterator protocol.

Calling expect(iterator).next.toBe() is equivalent to expect(iterator.next().value).toBe().

test('count to three', () => {
  const iterator = [0, 1, 2].values();
  expect(iterator).next.toBe(0);
  expect(iterator).next.toBe(1);
  expect(iterator).next.toBe(2);
});

Since this modifier calls .next() on the iterator, call .next() manually instead if you want to use the result for the same iteration in any other matchers.

test('count to three', () => {
  const iterator = [0, 1, 2].values();
  const result = iterator.next();
  expect(result).toHaveProperty('value', 0);
  expect(result).not.toBeDone();
});

.toBeDone()

Use .toBeDone() to check whether an iterator is done.

test('count is done', () => {
  const iterator = [0].values();
  expect(iterator).next.not.toBeDone(); // 0 is consumed here
  expect(iterator).next.toBeDone();
});

The IteratorResult can also be passed directly into the matcher.

test('count is not done', () => {
  const iterator = [0].values();
  expect(iterator.next()).not.toBeDone();
});

vitest-iterator's People

Contributors

renovate[bot] avatar stevezhu avatar

Stargazers

Lex avatar

Watchers

 avatar  avatar

vitest-iterator's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update dependency prettier to v3.3.3
  • Update dependency tsup to v8.2.4
  • Update pnpm to v9
  • Update typescript-eslint monorepo to v8 (major) (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • Update vitest monorepo to v2 (major) (@vitest/coverage-v8, @vitest/ui)
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
npm
package.json
  • @sinclair/typebox ^0.32.15
  • @trivago/prettier-plugin-sort-imports ^4.3.0
  • @types/chai ^4.3.12
  • @types/node ^20.11.27
  • @typescript-eslint/eslint-plugin ^7.2.0
  • @typescript-eslint/parser ^7.2.0
  • @vitest/coverage-v8 ^1.3.1
  • @vitest/ui ^1.3.1
  • eslint ^8.57.0
  • eslint-config-prettier ^9.1.0
  • eslint-plugin-import ^2.29.1
  • eslint-plugin-simple-import-sort ^12.0.0
  • nps ^5.10.0
  • nps-utils ^1.7.0
  • prettier ^3.2.5
  • prettier-plugin-packagejson ^2.4.12
  • tsup ^8.0.2
  • typescript ^5.4.2
  • vitest >=0.31.0
  • node >=16
  • pnpm 8.15.4

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.