|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Migrating between versions |
| 4 | +breadcrumb: migrating |
| 5 | +--- |
| 6 | + |
| 7 | +## Sinon 19 |
| 8 | + |
| 9 | +A breaking change is that Fake Timers 13 now fake all timers by default. Previously |
| 10 | +a couple of timers were explicitly ignored, but this no longer applies. |
| 11 | +If you want the old behavior, specify the `toFake` option and leave the name |
| 12 | +of the timers giving you trouble. |
| 13 | + |
| 14 | +The new version of fake-timers also no longer creating dates using the original Date |
| 15 | +class, but a subclass (proxy). This should not matter unless you are doing some kind |
| 16 | +of identity checks on the constructor: functionally they are the same. |
| 17 | + |
| 18 | +Removal of `legacyRoutes` option that was enabled in a previous `nise` version as the |
| 19 | +underlying library, `path-to-regexp`, had a fundamental change to its parsing that |
| 20 | +made the option a no-op. |
| 21 | + |
| 22 | +## Sinon 18 |
| 23 | + |
| 24 | +Mostly removal of some deprecated exports related to `sinon-test`, such as |
| 25 | +sinon.defaultConfig and related modules. |
| 26 | + |
| 27 | +## Sinon 17 |
| 28 | + |
| 29 | +Drops support for Node 16 |
| 30 | + |
| 31 | +## Sinon 15 |
| 32 | + |
| 33 | +Removes option to pass a custom formatter. |
| 34 | + |
| 35 | +## Sinon 7 |
| 36 | + |
| 37 | +For users upgrading to Sinon 7 the only known breaking API change is that |
| 38 | +**negative ticks** are not allowed in `Sinon@7` due to updating to lolex 3 |
| 39 | +internally. This means you cannot use negative values in |
| 40 | +sinon.useFakeTimers().tick(); |
| 41 | + |
| 42 | +If you experience any issues moving from Sinon 6 to Sinon 7, please let us |
| 43 | +know! https://github.com/sinonjs/sinon/issues/new?template=Bug_report.md. |
| 44 | + |
| 45 | +## Sinon 6 |
| 46 | + |
| 47 | +There should be no reason for any code changes with the new Sinon 6. |
| 48 | +Usually, `MAJOR` releases should come with breaking changes, but there are |
| 49 | +no known breaking changes in `sinon@6` at the time of this writing. We chose |
| 50 | +to release a new major version as a pragmatic solution to some noise related |
| 51 | +to releasing Sinon 5.1 https://github.com/sinonjs/sinon/pull/1829#issue- |
| 52 | +193284761, which featured some breaking changes related to ESM (which since |
| 53 | +has been resolved). |
| 54 | + |
| 55 | +If you should experience any issues moving from Sinon 5 to Sinon 6, please |
| 56 | +let us know! |
| 57 | +https://github.com/sinonjs/sinon/issues/new?template=Bug_report.md. |
| 58 | + |
| 59 | +## Sinon 5 |
| 60 | + |
| 61 | +As with all `MAJOR` releases in semver http://semver.org/, there are |
| 62 | +breaking changes in `sinon@5`. |
| 63 | +This guide will walk you through those changes. |
| 64 | + |
| 65 | +## `spy.reset()` is removed, use `spy.resetHistory()` |
| 66 | + |
| 67 | +In a previous version we deprecated and aliased `spy.reset` in favour of |
| 68 | +using `spy.resetHistory`. `spy.reset` has now been removed, you should use |
| 69 | +`spy.resetHistory`. |
| 70 | + |
| 71 | +## `sinon` is now a (default) sandbox |
| 72 | + |
| 73 | +Since `sinon@5.0.0`, the `sinon` object is a default sandbox. Unless you |
| 74 | +have a very advanced setup or need a special configuration, you probably |
| 75 | +want to only use that one. |
| 76 | + |
| 77 | +The old sandbox API is still available, so you don't **have** to do |
| 78 | +anything. |
| 79 | + |
| 80 | +However, switching to using the default sandbox can help make your code more |
| 81 | +concise. |
| 82 | +Now would be a good opportunity to tidy up your tests. |
| 83 | + |
| 84 | +### Before `sinon@5.0.0` |
| 85 | + |
| 86 | +In earlier versions, you would manually have to create sandboxes and keep |
| 87 | +references to them in order to restore them. |
| 88 | + |
| 89 | +const sandbox = sinon.createSandbox(); |
| 90 | + |
| 91 | +describe("myFunction", function () { |
| 92 | +afterEach(function () { |
| 93 | +sandbox.restore(); |
| 94 | +}); |
| 95 | + |
| 96 | +it("should make pie"); |
| 97 | +}); |
| 98 | + |
| 99 | +### From `sinon@5.0.0` |
| 100 | + |
| 101 | +describe("myFunction", function () { |
| 102 | +afterEach(function () { |
| 103 | +sinon.restore(); |
| 104 | +}); |
| 105 | + |
| 106 | +it("should make pie"); |
| 107 | +}); |
| 108 | + |
| 109 | +## Sinon 4 |
| 110 | + |
| 111 | +As with all `MAJOR` releases in semver http://semver.org/, there are |
| 112 | +breaking changes in `sinon@4`. |
| 113 | +This guide will walk you through those changes. |
| 114 | + |
| 115 | +## `sinon.stub(obj, 'nonExistingProperty')` - Throws |
| 116 | + |
| 117 | +Trying to stub a non-existing property will now fail, to ensure you are |
| 118 | +creating |
| 119 | +less error-prone tests https://github.com/sinonjs/sinon/pull/1557. |
| 120 | + |
| 121 | +## Sinon 3 |
| 122 | + |
| 123 | +As with all `MAJOR` releases in semver http://semver.org/, there are |
| 124 | +breaking changes in `sinon@3`. |
| 125 | +This guide will walk you through those changes. |
| 126 | + |
| 127 | +## `sinon.stub(object, "method", func)` - Removed |
| 128 | + |
| 129 | +Please use `sinon.stub(obj, "method").callsFake(func)` instead. |
| 130 | + |
| 131 | +var stub = sinon.stub(obj, "stubbedMethod").callsFake(function () { |
| 132 | +return 42; |
| 133 | +}); |
| 134 | + |
| 135 | +A codemod is available https://github.com/hurrymaplelad/sinon-codemod to |
| 136 | +upgrade your code |
| 137 | + |
| 138 | +## `sinon.stub(object, property, value)` - Removed |
| 139 | + |
| 140 | +Calling `sinon.stub` with three arguments will throw an Error. This was |
| 141 | +deprecated with `sinon@2` and has been removed with `sinon@3` |
| 142 | + |
| 143 | +## `sinon.useFakeTimers([now, ]prop1, prop2, ...)` - Removed |
| 144 | + |
| 145 | +`sinon.useFakeTimers()` signature has changed |
| 146 | +To define which methods to fake, |
| 147 | +please use `config.toFake`. Other options are now available when configuring |
| 148 | +`useFakeTimers`. Please consult the documentation for more information. |
| 149 | + |
| 150 | +## `sinon.sandbox.create(config)` - Config changes |
| 151 | + |
| 152 | +The changes in configuration for fake timers implicitly affect sandbox creation. |
| 153 | +If your config used to look |
| 154 | +like `{ useFaketimers: ["setTimeout", "setInterval"]}`, you |
| 155 | +will now need to change it to `{ useFaketimers: { toFake: ["setTimeout", |
| 156 | +"setInterval"] }}`. |
| 157 | + |
| 158 | +## `sandbox.stub(obj, 'nonExistingProperty')` - Throws |
| 159 | + |
| 160 | +Trying to stub a non-existing property will now fail to ensure you are |
| 161 | +creating |
| 162 | +less error-prone tests |
| 163 | +https://github.com/sinonjs/sinon/issues/1537#issuecomment-323948482. |
| 164 | + |
| 165 | +## Removal of internal helpers |
| 166 | + |
| 167 | +The following internal functions were deprecated as of `sinon@1.x` and have |
| 168 | +been removed in `sinon@3`: |
| 169 | + |
| 170 | +• `sinon.calledInOrder` |
| 171 | +• `sinon.create` |
| 172 | +• `sinon.deepEqual` |
| 173 | +• `sinon.format` |
| 174 | +• `sinon.functionName` |
| 175 | +• `sinon.functionToString` |
| 176 | +• `sinon.getConfig` |
| 177 | +• `sinon.getPropertyDescriptor` |
| 178 | +• `sinon.objectKeys` |
| 179 | +• `sinon.orderByFirstCall` |
| 180 | +• `sinon.restore` |
| 181 | +• `sinon.timesInWorlds` |
| 182 | +• `sinon.valueToString` |
| 183 | +• `sinon.walk` |
| 184 | +• `sinon.wrapMethod` |
| 185 | +• `sinon.Event` |
| 186 | +• `sinon.CustomEvent` |
| 187 | +• `sinon.EventTarget` |
| 188 | +• `sinon.ProgressEvent` |
| 189 | +• `sinon.typeOf` |
| 190 | +• `sinon.extend` |
| 191 | + |
| 192 | +## Sinon 2 |
| 193 | + |
| 194 | +Sinon v2.0 is the second major release, we have made several breaking |
| 195 | +changes in this release as a result of modernising the internals of Sinon. |
| 196 | +This guide is intended to walk you through the changes. |
| 197 | + |
| 198 | +## sinon.log and sinon.logError Removed |
| 199 | + |
| 200 | +`sinon.log` and `sinon.logError` were used in Sinon v1.x to globally |
| 201 | +configure `FakeServer`, `FakeXMLHttpRequest` and `FakeXDomainRequest`; these |
| 202 | +three functions now allow the logger to be configured on a per-use basis. In |
| 203 | +v1.x you may have written: |
| 204 | + |
| 205 | +sinon.log = function (msg) { // your logging impl }; |
| 206 | + |
| 207 | +You would now individually import and configure the utility upon creation: |
| 208 | + |
| 209 | +var sinon = require("sinon"); |
| 210 | + |
| 211 | +var myFakeServer = sinon.fakeServer.create({ |
| 212 | +logger: function (msg) { // your logging impl } |
| 213 | +}); |
| 214 | + |
| 215 | +## sinon.test, sinon.testCase and sinon.config Removed |
| 216 | + |
| 217 | +`sinon.test` and `sinon.testCase` have been extracted from the Sinon API and |
| 218 | +moved into their own node module, sinon-test |
| 219 | +https://www.npmjs.com/package/sinon-test. Please refer to the sinon-test |
| 220 | +README https://github.com/sinonjs/sinon-test/blob/master/README.md for |
| 221 | +migration examples. |
| 222 | + |
| 223 | +## stub.callsFake replaces stub(obj, 'meth', fn) |
| 224 | + |
| 225 | +`sinon.stub(obj, 'meth', fn)` return a spy, not a full stub. Behavior could |
| 226 | +not be redefined. `stub.callsFake` |
| 227 | +now returns a full stub. Here's a codemod script |
| 228 | +https://github.com/hurrymaplelad/sinon-codemod to help you migrate. |
| 229 | +See discussion https://github.com/sinonjs/sinon/pull/823. |
| 230 | + |
| 231 | +// Old |
| 232 | +sinon.stub(obj, "meth", fn); |
| 233 | +// New |
| 234 | +sinon.stub(obj, "meth").callsFake(fn); |
| 235 | + |
| 236 | +## stub.resetHistory replaces stub.reset |
| 237 | + |
| 238 | +`stub.reset()` now resets the history and the behaviour of the stub. |
| 239 | +Previously `stub.reset()` only reset the history of the stub. Stubs now have |
| 240 | +separate methods for resetting the history and the behaviour. To mimic the |
| 241 | +old behaviour replace all `stub.reset()` calls with `stub.resetHistory()`. |
| 242 | + |
| 243 | +// Old |
| 244 | +stub.reset(); |
| 245 | +// New |
| 246 | +stub.resetHistory(); |
| 247 | + |
| 248 | +## Deprecation of internal helpers |
| 249 | + |
| 250 | +The following utility functions are being marked as deprecated and are |
| 251 | +planned for removal in Sinon v3.0; please check your codebase for usage to |
| 252 | +ease future migrations: |
| 253 | + |
| 254 | +• `sinon.calledInOrder` |
| 255 | +• `sinon.create` |
| 256 | +• `sinon.deepEqual` |
| 257 | +• `sinon.format` |
| 258 | +• `sinon.functionName` |
| 259 | +• `sinon.functionToString` |
| 260 | +• `sinon.getConfig` |
| 261 | +• `sinon.getPropertyDescriptor` |
| 262 | +• `sinon.objectKeys` |
| 263 | +• `sinon.orderByFirstCall` |
| 264 | +• `sinon.restore` |
| 265 | +• `sinon.timesInWorlds` |
| 266 | +• `sinon.valueToString` |
| 267 | +• `sinon.walk` |
| 268 | +• `sinon.wrapMethod` |
| 269 | +• `sinon.Event` |
| 270 | +• `sinon.CustomEvent` |
| 271 | +• `sinon.EventTarget` |
| 272 | +• `sinon.ProgressEvent` |
| 273 | +• `sinon.typeOf` |
| 274 | +• `sinon.extend` |
| 275 | + |
| 276 | +## `sandbox.useFakeXMLHttpRequest` no longer returns a "server" |
| 277 | + |
| 278 | +In Sinon 1.x, the sandbox' `useFakeXMLHttpRequest` was the same as its |
| 279 | +`useFakeServer`. In 2.x, it maps directly to `sinon.useFakeXMLHttpRequest` |
| 280 | +(but with sandboxing). If you use `sandbox.useFakeXMLHttpRequest`, replace |
| 281 | +it with `sandbox.useFakeServer`, and your tests should behave as they always |
| 282 | +did. |
| 283 | + |
| 284 | +## `sinon.behavior` is gone |
| 285 | + |
| 286 | +The `sinon.behavior` object is no longer exposed for random modification. |
| 287 | +However, there is a new mechanism in place aided to add new behavior to |
| 288 | +stubs, `sinon.addBehavior(name, fn)`, see the stub docs. |
0 commit comments