# Fix "TypeError: stub(obj, 'meth', fn) has been removed" After Upgrading Sinon

Yes. An agent bumps sinon from 1 to the current release, runs mocha to catch the removed three-argument stub, rewrites each call with callsFake and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/fix-stub-obj-meth-fn-has-been-removed/sinon/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the repository into its own cloud sandbox and reads the test setup before touching the version. In a small Node library like haysclark/cli-docs, a helper for reading per-topic documentation files for a CLI, `package.json` asks for `"sinon": "^1.17.0"`, `"mocha": "^2.3.3"` and `"chai": "^3.3.0"`, and `"test"` runs `mocha spec -u tdd -R spec --recursive`; `.travis.yml` still pins Node 0.12, and the four spec files under `spec/` cover `lib/builder.js`, `lib/config-factory.js` and `lib/docs-factory.js`. A search for `sinon.` finds spies in `spec/builder.spec.js` and two stubs in `spec/docs-factory.spec.js`, at lines 40 and 53, both in the three-argument form sinon 1 allowed: `sinon.stub(fs, 'readFileSync', function onStubCallback(path, format) { ... })`, where the fake asserts on the path or the encoding and then calls `fs.readFileSync.restore()` from inside itself.

Then it runs the suite twice. On the pinned 1.17.7, `npm test` reports 21 passing. After `npm install --save-dev sinon@latest`, 22.1.0 today, the same command reports 2 failing: the first is `TypeError: stub(obj, 'meth', fn) has been removed, see documentation` thrown from `spec/docs-factory.spec.js:40`, and the second, from line 53, is `TypeError: Attempted to wrap readFileSync which is already wrapped`. The second failure is a consequence of the first: current sinon wraps the method before it rejects the third argument, so the refused stub leaves `fs.readFileSync` wrapped and the next test cannot stub it again.

The fix is the form sinon has used since 3: `sinon.stub(fs, 'readFileSync').callsFake(function onStubCallback(path, format) { ... })`. The agent also takes the `fs.readFileSync.restore()` calls out of the fakes, because a restore that runs inside the first call of a fake is skipped whenever the assertion before it throws, and adds `sinon.restore()` to the `afterEach` the describe block already has, which restores every stub and spy on the default sandbox after each test. With those three edits, `npm test` on 22.1.0 reports 21 passing again, the same count as on 1.17.7.

It leaves the rest alone and says so. `spec/builder.spec.js` uses `sinon.spy(docsFactory, 'create')`, whose signature did not change, and `mocha` 2 still runs the suite on Node 22, so neither is bumped in this pull request; the Node 0.12 pin in `.travis.yml` and the istanbul 0.3 coverage command go in the description as findings with their lines, not as changes.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Upgrade sinon to the current release. Read package.json and every spec file first and list each sinon call with its file and line, marking any three-argument sinon.stub(obj, 'method', fn) form. Run the test suite on the pinned version and record the result, then install the latest sinon and run it again, quoting the first failure with its file and line. Rewrite each removed form with sinon.stub(obj, 'method').callsFake(fn), move any restore() calls that live inside a fake into an afterEach that calls sinon.restore(), and run the suite again to show the passing count matches the baseline. Change nothing else; report any other outdated test tooling you notice with its line. Open a pull request that quotes the failing and passing runs.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request touching `package.json` and `spec/docs-factory.spec.js`: the sinon range moved to the current 22 release, the two `sinon.stub(fs, 'readFileSync', fn)` calls rewritten as `.callsFake(fn)`, the in-fake `restore()` lines gone and one `sinon.restore()` added to the existing `afterEach`. The description lists the three sinon call sites it found, quotes `stub(obj, 'meth', fn) has been removed` from line 40 and the `already wrapped` error that followed from line 53, and shows 21 passing before and after.

A reviewer checks that each fake still asserts what it did, the expected path in one test and `utf8` in the other, that `sinon.restore()` sits in an `afterEach` and not inside the fake, and that no spy in the other spec files was touched. The run's record has both `npm test` runs in full, so the two failures and the fixed count can be opened rather than taken on trust.

## Run it on every Monday morning

Add an automation to the repository's Project with this prompt and a `schedule` trigger, `cron` set to `0 9 * * 1` in your `timezone`. Each Monday one run bumps sinon, runs the suite and opens one pull request on its own branch; a week where the current release introduces a removal the prompt does not cover ends `needs_review` with the failing test quoted, and a week already on the current release opens nothing.

The same body works from your own server with `POST /o/{org}/sessions` and an API key when a dependency bot's sinon bump turns red; the key's `webhook_url` then receives `run.completed` or `run.failed` with the session URL and the pull request once the run ends.

## Why run it on Rush?

A test-double library that jumps twenty majors fails inside the suite, and the second failure it causes looks like a different bug. The agent runs mocha at the old and the new version in its own sandbox, so the pull request shows which error is the cause and which is the symptom, and every run with its full output stays in the record.

Knowing that a refused stub stays wrapped is the kind of detail that costs an hour on a laptop. Because the agent reads sinon's own stack trace in the run, it fixes the restore placement in the same change instead of landing a `callsFake` rewrite that still fails on the second test.

## Frequently asked questions

### Why was the three-argument stub removed?

Sinon 3 dropped `stub(obj, 'method', fn)` because it mixed creating a stub with defining its behaviour, which made the other stub behaviours impossible to combine with it. The replacement is `stub(obj, 'method').callsFake(fn)`, which creates the stub and then attaches the fake like any other behaviour.

### Why move the restore() out of the fake?

A `restore()` inside the fake runs only when the fake is reached and only after any assertion above it passes. If that assertion throws, the method stays stubbed and the next test fails with `already wrapped`, which is exactly what the upgraded run showed. `sinon.restore()` in `afterEach` restores everything on the default sandbox whether the test passed or not.

### Does the agent also upgrade mocha and chai?

Not in this pull request. Mocha 2 and chai 3 still run the suite on a current Node, so the agent names them in the description with their lines and leaves each to its own change. One pull request per removed API keeps the review small enough to read.

### What if my repository has many three-argument stubs?

The agent's first step is a list of every `sinon.stub(` call with its file and line, so the count is known before anything changes. Each rewrite is the same mechanical move to `.callsFake`, and the suite run after the change shows the passing count matches the baseline.

## More jobs like this: Upgrade dependencies weekly

- [Fix $.ajax(...).success Is Not a Function After a jQuery Upgrade](https://getrush.ai/run/fix-success-is-not-a-function/jquery/)
- [Fix "Textual SQL expression should be explicitly declared as text()" in SQLAlchemy 2](https://getrush.ai/run/fix-textual-sql-expression-argument-error/sqlalchemy/)
- [Fix "TypeError: this.getOptions is not a function" From sass-loader on webpack 4](https://getrush.ai/run/fix-this-getoptions-is-not-a-function/sass-loader/)
- [Fix THREE.Geometry Is Not a Constructor After a three.js Upgrade](https://getrush.ai/run/fix-three-geometry-is-not-a-constructor/three/)
- [Fix TypeScript Errors After an Upgrade](https://getrush.ai/run/fix-type-errors/nestjs/)
- [Fix Type Has No Properties in Common With FindOneOptions After a TypeORM Upgrade](https://getrush.ai/run/fix-type-has-no-properties-in-common-with-findoneoptions/typeorm/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
