{"action":"created","issue":{"url":"https://api.github.com/repos/HystericBerry/RPNativeSinonSample/issues/1","repository_url":"https://api.github.com/repos/HystericBerry/RPNativeSinonSample","labels_url":"https://api.github.com/repos/HystericBerry/RPNativeSinonSample/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/HystericBerry/RPNativeSinonSample/issues/1/comments","events_url":"https://api.github.com/repos/HystericBerry/RPNativeSinonSample/issues/1/events","html_url":"https://github.com/HystericBerry/RPNativeSinonSample/issues/1","id":309115316,"number":1,"title":"Can't get your example test to work","user":{"login":"dlwester","id":34143080,"avatar_url":"https://avatars2.githubusercontent.com/u/34143080?v=4","gravatar_id":"","url":"https://api.github.com/users/dlwester","html_url":"https://github.com/dlwester","followers_url":"https://api.github.com/users/dlwester/followers","following_url":"https://api.github.com/users/dlwester/following{/other_user}","gists_url":"https://api.github.com/users/dlwester/gists{/gist_id}","starred_url":"https://api.github.com/users/dlwester/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dlwester/subscriptions","organizations_url":"https://api.github.com/users/dlwester/orgs","repos_url":"https://api.github.com/users/dlwester/repos","events_url":"https://api.github.com/users/dlwester/events{/privacy}","received_events_url":"https://api.github.com/users/dlwester/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-03-27T20:09:47Z","updated_at":"2018-03-28T02:40:13Z","closed_at":null,"author_association":"NONE","body":"Sorry if this is not the proper place to ask about this repo. I cannot get your example to work when I try to test with the latest version of `request`, `request-promise`, and `request-promise-native`. I'm also using the latest Node.js and TypeScript.\r\n\r\nHere's my code that I'm trying to test:\r\n```\r\nimport * as rp from 'request-promise-native';\r\n...\r\n    try {\r\n      const response: any = await rp(requestOptions);\r\n      CouchdbClientHelper.LOGGER.info({ FUNCTION_NAME, response }, 'Returning');\r\n      return Promise.resolve(response);\r\n...\r\n```\r\n\r\nAnd here's the test:\r\n```\r\nimport * as rp from 'request-promise-native';\r\n...\r\n  const responseData: any = returnData({});\r\n  sandbox.stub(rp, 'get').resolves(responseData);\r\n```\r\n\r\nThe first problem I run into is that I cannot use `'Request'` in the `stub()` call as your module does. I get the error:\r\n```\r\nsrc/common/couchdb-client-helper.spec.ts:186:20 - error TS2345: Argument of type '\"Request\"' is not assignable to parameter of type '\"del\" | \"head\" | \"debug\" | \"defaults\" | \"get\" | \"post\" | \"put\" | \"patch\" | \"delete\" | \"initParams...'.\r\n\r\n186   sandbox.stub(rp, 'Request').resolves(responseData);\r\n```\r\n\r\nBut when I use `'get'`, it's not working - it is calling the real `rp()`, and not the stubbed one. Any ideas?\r\n"},"comment":{"url":"https://api.github.com/repos/HystericBerry/RPNativeSinonSample/issues/comments/376741479","html_url":"https://github.com/HystericBerry/RPNativeSinonSample/issues/1#issuecomment-376741479","issue_url":"https://api.github.com/repos/HystericBerry/RPNativeSinonSample/issues/1","id":376741479,"user":{"login":"HystericBerry","id":9257383,"avatar_url":"https://avatars1.githubusercontent.com/u/9257383?v=4","gravatar_id":"","url":"https://api.github.com/users/HystericBerry","html_url":"https://github.com/HystericBerry","followers_url":"https://api.github.com/users/HystericBerry/followers","following_url":"https://api.github.com/users/HystericBerry/following{/other_user}","gists_url":"https://api.github.com/users/HystericBerry/gists{/gist_id}","starred_url":"https://api.github.com/users/HystericBerry/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HystericBerry/subscriptions","organizations_url":"https://api.github.com/users/HystericBerry/orgs","repos_url":"https://api.github.com/users/HystericBerry/repos","events_url":"https://api.github.com/users/HystericBerry/events{/privacy}","received_events_url":"https://api.github.com/users/HystericBerry/received_events","type":"User","site_admin":false},"created_at":"2018-03-28T02:40:13Z","updated_at":"2018-03-28T02:40:13Z","author_association":"OWNER","body":"@dlwester \r\nOH! You're not using NodeJs' native require function.\r\n\r\nThe reason why my code works is because require actually caches the module that you are importing.\r\nSo when you do the following:\r\n```node\r\n// assume this is function definition in foo.js\r\nconst rp = require('request-promise-native'); // 2nd call to rp is actually the cached, stubbed module\r\nasync function returnData() { ... rp here ... } // your function here\r\n\r\n\r\n// assume this is in fooTest.js\r\nconst rp = require('request-promise-native');\r\n// ^ first occurrence of 'require(...)' results in the module being cached as an Object\r\n...\r\nsandbox.stub(rp, 'Request')... // Stub the cached module\r\nfoo.returnData(); // <-- this is called after\r\n```\r\nRunning the fooTest.js will cache rp in the fooTest.js\r\n\r\nI didn't step through the code but my first guess is that that is your issue.\r\nThat's the tradeoff of not using native NodeJs, assuming that your problem is:\r\n```typescript\r\nimport * as rp from 'request-promise-native'; // my guess is it doesn't cache\r\n```\r\n\r\nFor an in-depth explanation for why the non-typescript variant works, refer to:\r\nhttps://github.com/request/request-promise/issues/55\r\n\r\nSo you need to figure out yourself:\r\n1) \"Does TypeScript cache on imports?\"\r\nAnd if yes:\r\n2) \"Are you doing it the right way?\""}}
{"actor":{"display_login":"HystericBerry"}}