{"_id":"55c25ef1f2cff20d006d0b8a","category":{"_id":"559b65be8b04e90d00702d58","version":"559b65bd8b04e90d00702d57","pages":["559b65bf8b04e90d00702d5a","55a46241ffd44e0d00fca056","55a46f45e75ca60d00a39323","55a470b768c7e20d0036d4d6","55a4a758dad9d00d0071fcba","55b6b6276127b12500364560","55baf505d0736b3100bf1033","55c25ef1f2cff20d006d0b8a","56269a8b44c87f0d00fe6378","56498f62f176080d000c78ac","566f59c4fc3abb0d0020fcd5","567cd2228eb5b20d0021059d"],"project":"559b65bd8b04e90d00702d54","__v":12,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-07-07T05:38:06.409Z","from_sync":false,"order":0,"slug":"chimpjs","title":"Chimp.js"},"parentDoc":null,"version":{"_id":"559b65bd8b04e90d00702d57","project":"559b65bd8b04e90d00702d54","__v":6,"createdAt":"2015-07-07T05:38:05.890Z","releaseDate":"2015-07-07T05:38:05.890Z","categories":["559b65be8b04e90d00702d58","55a46e7468c7e20d0036d4d1","55a4971be6c2470d002a6483","55a4a690e10ba50d00b40bba","55a4bcf31a5f991700a94066","56cf5c8d336aa60b0086a40d"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"project":"559b65bd8b04e90d00702d54","user":"55a4738a68c7e20d0036d4de","__v":12,"updates":["561f1b66cfc3610d00567179"],"next":{"pages":[],"description":""},"createdAt":"2015-08-05T19:07:29.452Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":7,"body":"With Chimp you can write your tests in a synchronous style. So you don't have to deal with confusing promise chains or callback hell.\n\nAll the APIs that are shipped with Chimp can be used in a synchronous style by default. This includes the WebdriverIO commands, request module, and the DDP client.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Making asynchronous functions synchronous\"\n}\n[/block]\nIf you have asynchronous functions that you want to use in your tests, you can wrap them with our helper functions `wrapAsync` and `wrapAsyncObject`. Both helpers work with asynchronous functions that return a promise or use a callback.\n\n* **wrapAsync(fn: Function, [context: Object]): Function**\n  Takes the asynchronous function as first argument and optional the function context (the value of `this` inside the function). It returns a synchronous version of the asynchronous function.\n\n* **wrapAsyncObject(object: Object, properties: Array, [options: Object]): Object**\n  Creates synchronous versions of the asynchronous methods of an object. It returns the wrapper object with the synchronous and asynchronous methods.The first argument is the object. The second argument is an array of the method names that should be made synchronous.\n\n### Example\n\nYou have an object `myObject` with an asynchronous method `myFunc`:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var myObject = {\\n  myFunc: function (callback) { ... }  // Asynchronous function\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Before\"\n    }\n  ]\n}\n[/block]\nYou wrap it with `wrapAsyncObject`:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var myObjectWrapper = wrapAsyncObject(myObject, ['myFunc'])\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n`myObjectWrapper` is:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"myObjectWrapper = {\\n  myFunc: function () { ... } // Synchronous version\\n  myFuncSync: function () { ... } // Synchronous version\\n  myFuncAsync: function (callback) { ... } // Asynchronous version\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nmyObject has not changed.\n\n### options\n\n* **syncByDefault: Boolean (default: true)**\n\nIf you set this to `false`, you get this result instead:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"myObjectWrapper = {\\n  myFunc: function (callback) { ... } // Asynchronous version\\n  myFuncSync: function () { ... } // Synchronous version\\n  myFuncAsync: function (callback) { ... } // Asynchronous version\\n}\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n`myFunc` is asynchronous.\n\n* **wrapAsync: Function (default: wrapAsync)**\n\nIf you want to use another function to wrap the asynchronous methods, you could pass it with this option.","excerpt":"Learn how you can write all your tests in a synchronous style","slug":"synchronous-tests","type":"basic","title":"Synchronous tests"}

Synchronous tests

Learn how you can write all your tests in a synchronous style

With Chimp you can write your tests in a synchronous style. So you don't have to deal with confusing promise chains or callback hell. All the APIs that are shipped with Chimp can be used in a synchronous style by default. This includes the WebdriverIO commands, request module, and the DDP client. [block:api-header] { "type": "basic", "title": "Making asynchronous functions synchronous" } [/block] If you have asynchronous functions that you want to use in your tests, you can wrap them with our helper functions `wrapAsync` and `wrapAsyncObject`. Both helpers work with asynchronous functions that return a promise or use a callback. * **wrapAsync(fn: Function, [context: Object]): Function** Takes the asynchronous function as first argument and optional the function context (the value of `this` inside the function). It returns a synchronous version of the asynchronous function. * **wrapAsyncObject(object: Object, properties: Array, [options: Object]): Object** Creates synchronous versions of the asynchronous methods of an object. It returns the wrapper object with the synchronous and asynchronous methods.The first argument is the object. The second argument is an array of the method names that should be made synchronous. ### Example You have an object `myObject` with an asynchronous method `myFunc`: [block:code] { "codes": [ { "code": "var myObject = {\n myFunc: function (callback) { ... } // Asynchronous function\n}", "language": "javascript", "name": "Before" } ] } [/block] You wrap it with `wrapAsyncObject`: [block:code] { "codes": [ { "code": "var myObjectWrapper = wrapAsyncObject(myObject, ['myFunc'])", "language": "javascript" } ] } [/block] `myObjectWrapper` is: [block:code] { "codes": [ { "code": "myObjectWrapper = {\n myFunc: function () { ... } // Synchronous version\n myFuncSync: function () { ... } // Synchronous version\n myFuncAsync: function (callback) { ... } // Asynchronous version\n}", "language": "javascript" } ] } [/block] myObject has not changed. ### options * **syncByDefault: Boolean (default: true)** If you set this to `false`, you get this result instead: [block:code] { "codes": [ { "code": "myObjectWrapper = {\n myFunc: function (callback) { ... } // Asynchronous version\n myFuncSync: function () { ... } // Synchronous version\n myFuncAsync: function (callback) { ... } // Asynchronous version\n}", "language": "text" } ] } [/block] `myFunc` is asynchronous. * **wrapAsync: Function (default: wrapAsync)** If you want to use another function to wrap the asynchronous methods, you could pass it with this option.