1.2.11 ā€¢ Published 7 months ago

@sefr/test v1.2.11

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

āœØ @sefr/test

šŸ’” What does it do ?

Provide an API to easily stub a ES6 class with type-checking.

šŸ”§ Compatibility

TypeScriptEcmaScript
>= 2.8.0>= ES2015

šŸŽ± Dependencies

This package heaviliy depends on Sinon.

šŸ’¾ Installation

Nothing more than :

npm i -D @sefr/test

šŸ“š How to use

This package re-exports all the following modules from amazing libraries :

assert, expect from Chai
sinon, SinonStubbedInstance, spy from sinon
StubbedType, stubInterface from @salesforce/ts-sinon

It also configures plugins sinon-chai and chai-as-promised which are includes with exports from Chai.

This package includes additionnal feature which allow you to easily stub a class :

import { expect, stubClass } from "@sefr/test";

class FakeClass {
	public readonly firstAttribute: string;
	public readonly secondAttribute: number;

	constructor(firstAttribute: string, secondAttribute: number) {
		this.firstAttribute = firstAttribute;
		this.secondAttribute = secondAttribute;
	}

	public firstMethod(someParameter: boolean): string {
		if (someParameter) {
			return "true";
		}
		return "false";
	}

	public secondMethod(someParameter: string): number {
		if (someParameter === "1") {
			return 1;
		}
		return 0;
	}
}

let myStubbedClassInstance: StubbedClass<FakeClass>;

describe('MyTestSuite', () => {
	context('MyTestContext', () => {
		it('Check something', () => {
			// Given
			myStubbedClassInstance = stubClass(FakeClass);
			myStubbedClassInstance.firstMethod.returns("Toto"); // can be any method from a Sinon stub

			// When
			const result = myStubbedClassInstance.firstMethod(true);

			// Then
			expect(result).to.equal("Toto");
			expect(myStubbedClassInstance.firstMethod).to.have.been.calledOnceWith("Toto"); // any assertion that you need
		});
	});
});

šŸ“Ž Credits

šŸ“œ License

This software is available under the MIT License. See the LICENSE file for more informations.

ā¬†ļø Go back to top

1.2.8

10 months ago

1.2.7

10 months ago

1.2.9

9 months ago

1.2.10

8 months ago

1.2.11

7 months ago

1.2.6

11 months ago

1.2.5

11 months ago

1.2.4

12 months ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago