@sefr/test
TypeScript icon, indicating that this package has built-in type declarations

1.2.11 • Public • Published

✨ @sefr/test

💡 What does it do ?

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

🔧 Compatibility

TypeScript EcmaScript
>= 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

Package Sidebar

Install

npm i @sefr/test

Weekly Downloads

1

Version

1.2.11

License

MIT

Unpacked Size

14.4 kB

Total Files

9

Last publish

Collaborators

  • sefr