Skip to content

Commit

Permalink
Merge pull request #25 from ShellyDCMS/add-sinon-with-args-tests
Browse files Browse the repository at this point in the history
Add some tests, update readme
  • Loading branch information
ShellyDCMS authored Aug 28, 2024
2 parents dea5567 + ca96918 commit 5745029
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# ts-stubber

![ts-stubber](https://github.com/ShellyDCMS/ts-stubber/actions/workflows/npm-publish.yml/badge.svg)
![NPM](https://img.shields.io/npm/v/ts-stubber)
[![NPM](https://img.shields.io/npm/v/ts-stubber)](https://www.npmjs.com/package/ts-stubber)
![MIT](https://camo.githubusercontent.com/a4426cbe5c21edb002526331c7a8fbfa089e84a550567b02a0d829a98b136ad0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)
![typescript](https://camo.githubusercontent.com/017786f7ebc845ae38c14f3bc28dc6162e756f33ea8549fd4f9071c405edb5de/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2533432532462533452d547970655363726970742d2532333030373463312e737667)
Expand All @@ -20,11 +19,11 @@ Thus, enabling both avoiding side effects that may occur while class constructor

## Installation

`npm i -D @ts-stubber`
`npm i -D ts-stubber`

or

`yarn add -D @ts-stubber`
`yarn add -D ts-stubber`

## Usage

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ts-stubber",
"description": "Lazy Stubbing a TypeScript Class or Interface with any Mocking Framework for testing in Isolation",
"version": "1.0.17",
"version": "1.0.18",
"author": "Shelly Goldblit",
"private": false,
"license": "MIT",
Expand Down
24 changes: 24 additions & 0 deletions src/stub-builder.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ describe("Cypress stub builder tests with Sinon Stubs", () => {
expect(await mockMyInheritedClass.asynFunc(input)).to.eq(returnValue);
});

it("async function should return value according to param", async () => {
const mockMyClass = stubbedInstanceCreator.createStubbedInstance();
mockMyClass.asynFunc.withArgs(1).resolves(101);
expect(await mockMyClass.asynFunc(1)).to.eq(101);
});

it("property async should return value according to param", async () => {
const mockMyClass = stubbedInstanceCreator.createStubbedInstance();
mockMyClass.asyncPropertyFunc.withArgs(1).returns(Promise.resolve(101));
expect(await mockMyClass.asyncPropertyFunc(1)).to.eq(101);
});

it("func should return value according to param", () => {
const mockMyClass = stubbedInstanceCreator.createStubbedInstance();
mockMyClass.func.withArgs(0, "whatever").returns(99);
expect(mockMyClass.func(0, "whatever")).to.eq(99);
});

it("property func should return value according to param", () => {
const mockMyClass = stubbedInstanceCreator.createStubbedInstance();
mockMyClass.propertyFunc.withArgs(3).returns(55);
expect(mockMyClass.propertyFunc(3)).to.eq(55);
});

it("should stub async function using overrides", async () => {
const stub = sinon.stub().returns(Promise.resolve(returnValue));
const mockMyInheritedClass =
Expand Down
1 change: 1 addition & 0 deletions src/stub-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const StubbedInstanceCreator = <T, StubT>(
target: Record<string, unknown>,
prop: string
) => {
debugger;
if (!(prop in target)) {
target[prop] = createStub(prop);
}
Expand Down

0 comments on commit 5745029

Please sign in to comment.