CodeSampleX

Exemplo

@vitest/spy 2.1.9: isMockFunction

Amostra verificada para npm @vitest/spy 2.1.9: isMockFunction. O contrato rodou em node 22 · linux debian/x64 · docker e passou.

sha256:b89badde09e6300b99d7e0450fa69c76162ed1e58cdf6f833b134a089456c777

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 npm

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-06

Caso

HOW
Objetivo
verify @vitest/spy.isMockFunction in pkg:npm/%40vitest/spy@2.1.9
Pacotes
Símbolos
  • @vitest/spy.isMockFunction
Criado
2026-09-06T18:09:43Z

Contrato

  1. @vitest/spy.isMockFunction returns true for mock functions created with fn()
  2. @vitest/spy.isMockFunction returns true for spies created on object methods with spyOn()
  3. @vitest/spy.isMockFunction returns true for getter and setter spies created with spyOn()
  4. @vitest/spy.isMockFunction returns false for regular unmocked functions and async functions
  5. @vitest/spy.isMockFunction returns false for non-function values such as null, undefined, objects, numbers, and strings
  6. @vitest/spy.isMockFunction returns false for ordinary objects that mimic mock properties without being a function
  7. @vitest/spy.isMockFunction returns false for original methods after spy.mockRestore() is invoked

Arquivos

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify @vitest/spy.isMockFunction in pkg:npm/%40vitest/spy@2.1.9
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40vitest/spy@2.1.9
Demonstrate these symbols/APIs:
  - @vitest/spy.isMockFunction

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:1615857ec2170fe17228907df74d3044e3efdaf6851b05fb7b10013326cabc6f","contract":["@vitest/spy.isMockFunction returns true for mock functions created with fn()","@vitest/spy.isMockFunction returns true for spies created on object methods with spyOn()","@vitest/spy.isMockFunction returns true for getter and setter spies created with spyOn()","@vitest/spy.isMockFunction returns false for regular unmocked functions and async functions","@vitest/spy.isMockFunction returns false for non-function values such as null, undefined, objects, numbers, and strings","@vitest/spy.isMockFunction returns false for ordinary objects that mimic mock properties without being a function","@vitest/spy.isMockFunction returns false for original methods after spy.mockRestore() is invoked"],"goal":"verify @vitest/spy.isMockFunction in pkg:npm/%40vitest/spy@2.1.9","kind":"HOW","packages":["pkg:npm/%40vitest/spy@2.1.9"],"schemaVersion":1,"symbols":["@vitest/spy.isMockFunction"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40vitest/spy@2.1.9"],"schemaVersion":1,"subject":"pkg:npm/%40vitest/spy@2.1.9","symbols":["@vitest/spy.isMockFunction"],"verifierAdapter":"node-typescript@1"}
index.mjs
import { fn, spyOn, isMockFunction } from '@vitest/spy';

export { fn, spyOn, isMockFunction };
export default isMockFunction;
package-lock.json
{
  "name": "sample-vitest-spy-is-mock-function",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-vitest-spy-is-mock-function",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@vitest/spy": "2.1.9"
      }
    },
    "node_modules/@vitest/spy": {
      "version": "2.1.9",
      "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-2.1.9.tgz",
      "integrity": "sha512-E1B35FwzXXTs9FHNK6bDszs7mtydNi5MIfUWpceJ8Xbfb1gBMscAnwLbEu+B44ed6W3XjL9/ehLPHR1fkf1KLQ==",
      "license": "MIT",
      "dependencies": {
        "tinyspy": "^3.0.2"
      },
      "funding": {
        "url": "https://opencollective.com/vitest"
      }
    },
    "node_modules/tinyspy": {
      "version": "3.0.2",
      "resolved": "https://registry.npmjs.org/tinyspy/-/tinyspy-3.0.2.tgz",
      "integrity": "sha512-n1cw8k1k0x4pgA2+9XrOkFydTerNcJ1zWCO5Nn9scWHTD+5tp8dghT2x1uduQePZTZgd3Tupf+x9BxJjeJi77Q==",
      "license": "MIT",
      "engines": {
        "node": ">=14.0.0"
      }
    }
  }
}
package.json
{
  "name": "sample-vitest-spy-is-mock-function",
  "version": "1.0.0",
  "type": "module",
  "main": "index.mjs",
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "@vitest/spy": "2.1.9"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @vitest/spy.isMockFunction in pkg:npm/%40vitest/spy@2.1.9",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40vitest/spy@2.1.9"
  ],
  "symbols": [
    "@vitest/spy.isMockFunction"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { fn, spyOn, isMockFunction } from '@vitest/spy';

// 1. isMockFunction returns true for mock functions created with fn()
{
  const mockFn = fn();
  assert.equal(isMockFunction(mockFn), true);

  const mockWithImpl = fn((x) => x * 2);
  assert.equal(isMockFunction(mockWithImpl), true);
  assert.equal(mockWithImpl(5), 10);
}

// 2. isMockFunction returns true for spies created on object methods with spyOn()
{
  const calculator = {
    add(a, b) {
      return a + b;
    }
  };
  const spy = spyOn(calculator, 'add');
  assert.equal(isMockFunction(spy), true);
  assert.equal(isMockFunction(calculator.add), true);
  assert.equal(calculator.add(2, 3), 5);
}

// 3. isMockFunction returns true for getter and setter spies created with spyOn()
{
  const person = {
    _age: 30,
    get age() {
      return this._age;
    },
    set age(val) {
      this._age = val;
    }
  };
  const getSpy = spyOn(person, 'age', 'get');
  assert.equal(isMockFunction(getSpy), true);
  assert.equal(person.age, 30);

  const setSpy = spyOn(person, 'age', 'set');
  assert.equal(isMockFunction(setSpy), true);
  person.age = 35;
  assert.equal(person._age, 35);
}

// 4. isMockFunction returns false for regular unmocked functions and async functions
{
  function standardFunction() {}
  const arrowFunction = () => {};
  async function asyncFunction() {}
  const generatorFunction = function* () {};

  assert.equal(isMockFunction(standardFunction), false);
  assert.equal(isMockFunction(arrowFunction), false);
  assert.equal(isMockFunction(asyncFunction), false);
  assert.equal(isMockFunction(generatorFunction), false);
}

// 5. isMockFunction returns false for non-function values such as null, undefined, objects, numbers, and strings
{
  assert.equal(isMockFunction(null), false);
  assert.equal(isMockFunction(undefined), false);
  assert.equal(isMockFunction(0), false);
  assert.equal(isMockFunction(123), false);
  assert.equal(isMockFunction(''), false);
  assert.equal(isMockFunction('mock'), false);
  assert.equal(isMockFunction(true), false);
  assert.equal(isMockFunction(Symbol('mock')), false);
  assert.equal(isMockFunction({}), false);
  assert.equal(isMockFunction([]), false);
}

// 6. isMockFunction returns false for ordinary objects that mimic mock properties without being a function
{
  const fakeObj = { _isMockFunction: true };
  assert.equal(isMockFunction(fakeObj), false);

  const nonMockWithFalse = () => {};
  nonMockWithFalse._isMockFunction = false;
  assert.equal(isMockFunction(nonMockWithFalse), false);
}

// 7. isMockFunction returns false for original methods after spy.mockRestore() is invoked
{
  const target = {
    greet(name) {
      return `Hello, ${name}!`;
    }
  };
  const spy = spyOn(target, 'greet');
  assert.equal(isMockFunction(target.greet), true);

  spy.mockRestore();
  assert.equal(isMockFunction(target.greet), false);
  assert.equal(target.greet('world'), 'Hello, world!');
}

console.log('All @vitest/spy.isMockFunction contract assertions passed.');

Seeder de origem

anônimo