Given-When-Then helper for Jest.
$ npm install @valtech-commerce/jest-gwt
Import the package in your Jest test files and extends the base GWT.
//--------------------------------------------------------
// xyz.gwt.js
//--------------------------------------------------------
import path from "node:path";
import { given as givenBase, when as whenBase, then as thenBase } from "@valtech-commerce/jest-gwt";
const given = { ...givenBase };
const when = { ...whenBase };
const then = { ...thenBase };
let rootPath;
given.noRootPath = () => {
rootPath = undefined;
};
given.rootPath = (value) => {
rootPath = value;
};
when.packageIsLoaded = () => {
when.attempting(() => {
require(path.join(rootPath, "package.json"));
});
};
export { given, when, then };
//--------------------------------------------------------
// xyz.test.js
//--------------------------------------------------------
import { given, when, then } from "./xyz.gwt";
describe("Validate package.json", () => {
beforeEach(() => {
given.noException();
given.noRootPath();
});
test("Ensure package can be loaded", () => {
given.rootPath("..");
when.packageIsLoaded();
then.shouldNotHaveThrown();
});
});
The package comes with multiple helpers. If you want them all, import the root package @valtech-commerce/jest-gwt
.
Check for thrown exceptions
Reset exception listener
Listen for error when executing closure
Listen for error when executing asynchronous closure
Exception has been thrown
Exception, with message excerpt, has been thrown
No exception has been thrown
Tests for package.json
Reset root path
Reset parsed config
Reset name pattern
Reset keywords
Specify the root path of the package.json
Specify a pattern for the package name
Specify mandatory keywords
Parse the package.json file
Package name matches the defined name pattern
Package keywords contain at least the defined keywords
Package exports point to existing files
See the Changelog to see what has changed.
See the Contributing Guidelines for ways to get started.
See the Support Guide for ways to get help.
See the Security Policy for sharing vulnerability reports.
This project has a Code of Conduct. By interacting with this repository, organization, or community you agree to abide by its terms.