https://github.com/rodesp/sinon-function-stub-example
This shows how to stub a function that's being imported into an ES6 module using Typescript and Sinon.js
https://github.com/rodesp/sinon-function-stub-example
Last synced: 8 days ago
JSON representation
This shows how to stub a function that's being imported into an ES6 module using Typescript and Sinon.js
- Host: GitHub
- URL: https://github.com/rodesp/sinon-function-stub-example
- Owner: RodEsp
- Created: 2021-11-08T23:19:14.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-16T21:04:13.000Z (over 3 years ago)
- Last Synced: 2025-02-17T08:18:22.997Z (3 months ago)
- Language: TypeScript
- Size: 43 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## This repo shows how to stub a function for unit testing with Mocha, Typescript, and Sinon.js.
> Please refer to [test.ts](./test/test.ts) for the example.
## How to run
1. Clone this repo
2. Run `npm install` or `yarn install`
3. Run `npm test` or `yarn test`## Explanation
[test.ts](./test/test.ts) imports the stubee module using the `as` keyword so that it can then use `sinon.stub()` to stub out the default export.[test.ts - line 5](./test/test.ts#L5)
```js
import * as stubableFunction from '../src/stubee';
```
[test.ts - line 11](./test/test.ts#L11)
```js
sandbox.stub(stubableFunction, 'default').resolves('this is working');
```When you run `yarn test` you should see the `console.log()` call in [moduleUnderTest.ts - line 7](./src/moduleUnderTest.ts#L7) output what we told our stub to resolve to and the test passing:
