@wixc3/testing
v18.0.0
Published
Making mocha/chai testing easier
Downloads
3,582
Maintainers
Keywords
Readme
testing package
Utils for making mocha + chai testing easy and fun
Remarks
Env variables
DEBUG=true/positive number env variable will set test timeouts and time scale to infinity so tests (that don't explicitly override timeout) will not time out on breakpoints
TIMEOUT_MULTIPLIER=number env variable will multiply all test timeouts by the given number
Test timeout manipulation
scaleTimeout() multiplies timeouts when debugging or running on slow CI machines, based on TIMEOUT_MULTIPLIER and DEBUG env variables
adjusts current test timeout (for use in non step async actions)
locatorTimeout() creates a locator timeout and adjust the current test
Steps
Steps are a convenient way to craft async tests. A step has a timeout and a description, making test timeouts easy to understand and debug. Each step timeout auto increases the test timeout, assuring the step will time out before the test
Available steps:
withTimeout() adds timeout and description to a promise
allWithTimeout() time limited Promise.all
waitForSpyCall() spies on a method and wait for first call
waitForStubCall() creates a one off stub and wait for it to be called
step() adds a description (but not timeout) to a promise, useful for playwright locator waitFor
sleep() sleep (and adjust test time)
Functions
| Function | Description | | ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | adjustCurrentTestTimeout(ms) | Add ms to current test timeout | | adjustTestsTimeouts() | Adjust tests timeouts based on DEBUG and TIMEOUT_MULTIPLIER environment variables | | allWithTimeout(actions) | Limits the time a list of promises can take- Note: useable only within a mocha test/hook. The total test timeout will be adjusted to make sure the test will not time out waiting for this step | | chaiRetryPlugin(_, { flag, inspect }) | Plugin that allows to re-run function passed to expect, in order to achieve that use new retry method, retrying would be performed until the result will pass the chained assertion or timeout exceeded or retries limit reached. Should be applied through Chai.use function, for example: | | createDisposalGroup(name, constraints) | Creates a new disposal group | | createTestDisposables(disposeHook) | Creates a disposable group that will be disposed after the test is done | | debugSafeTimeout(ms, rest) | Creates an object with scaled timeout and adjust the current test timeout accordingly | | defaults() | default values for steps of the current test | | disposeAfter(disposable, options) | | | getTimeoutScale() | | | initAndDisposeAfter(target, options, args) | | | isDebugMode() | | | locatorTimeout(ms) | | | mochaCtx() | Active mocha context | | overrideDebugMode(value) | override the DEBUG environment variable for the current test | | overrideTimeoutScale(scale) | Overrides the TIMEOUT_MULTIPLIER for the current test | | randomizeTestsOrder(shouldRandomize) | Randomizes tests orderTo avoid confusion, it can only be set once, before the testing begins (i.e. not in a running test) | | scaleTimeout(timeout) | Scales a timeout based on the TIMEOUT_MULTIPLIER and DEBUG environment variable | | sleep(ms) | Resolves after ms milliseconds | | step(action) | Adds a step description to a promise if it's rejected* - Note: useable only within a mocha test/hook. The total test timeout will be adjusted to make sure the test will not time out waiting for this step | | useSafeFakeTimers() | Makes it easy to safely use fake timers | | waitForSpyCall(scope, method) | Spies on an object method, waiting until it's called. The spy is removed once called | | waitForStubCall(action, waitForAction) | Creates a stub, then waits for it to be called | | withTimeout(action) | Limits the time a promise can take- Note: useable only within a mocha test/hook. The total test timeout will be adjusted to make sure the test will not time out waiting for this step |
Interfaces
| Interface | Description | | ------------------------------------------------------------------------------------------ | --------------------------------- | | Info | Step info base, added step errors | | PollDefaults | Defaults for poll steps | | PromiseStep | | | PromiseWithTimeout | WithTimeout API | | StepBase | Common step props | | StepsDefaults | Test step defaults | | TimeoutDefaults | Step timeout defaults |
Variables
| Variable | Description | | -------------------------------------------------------------------------------------------------- | ----------- | | codeMatchers | | | DEFAULT_DISPOSAL_GROUP | |
Type Aliases
| Type Alias | Description | | ------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Description | Sets step description | | Predicate | A predicate functionAny return value other than **false** or throwing is considered as satisfying the predicate | | RetryOptions | The retry options for the chaiRetryPlugin. {Object} RetryOptions {number} [timeout] - The maximum duration in milliseconds to wait before failing the retry operation. {number} [retries] - The number of times to retry the function before failing. {number} [delay] - The delay in milliseconds between retries. | | Stub | A generated stub | | Timeout | Sets step timeout |