Options
All
  • Public
  • Public/Protected
  • All
Menu

Package harp-test-utils

@here/harp-test-utils

Overview

This module provides utilities used in tests.

Index

References

Classes

Interfaces

Type aliases

Variables

Functions

References

EventSource

Re-exports EventSource

EventSource

Re-exports EventSource

RenderingTestHelper

Re-exports RenderingTestHelper

TestOptions

Re-exports TestOptions

assertLogsSync

Re-exports assertLogsSync

assertLogsSync

Re-exports assertLogsSync

assertRejected

Re-exports assertRejected

assertRejected

Re-exports assertRejected

getPlatform

Re-exports getPlatform

getTestResourceUrl

Re-exports getTestResourceUrl

getTestResourceUrl

Re-exports getTestResourceUrl

getWebGlInfo

Re-exports getWebGlInfo

inBrowserContext

Re-exports inBrowserContext

inBrowserContext

Re-exports inBrowserContext

inNodeContext

Re-exports inNodeContext

inNodeContext

Re-exports inNodeContext

inWebWorkerContext

Re-exports inWebWorkerContext

inWebWorkerContext

Re-exports inWebWorkerContext

loadTestResource

Re-exports loadTestResource

loadTestResource

Re-exports loadTestResource

setGlobalReporter

Re-exports setGlobalReporter

silenceLoggingAroundFunction

Re-exports silenceLoggingAroundFunction

silenceLoggingAroundFunction

Re-exports silenceLoggingAroundFunction

stubGlobalConstructor

Re-exports stubGlobalConstructor

stubGlobalConstructor

Re-exports stubGlobalConstructor

waitForEvent

Re-exports waitForEvent

waitForEvent

Re-exports waitForEvent

willEventually

Re-exports willEventually

willEventually

Re-exports willEventually

Type aliases

PerformanceTestResults

PerformanceTestResults: PerformanceTestResultEntry[]

Variables

Const getCurrentTime

getCurrentTime: (Anonymous function) = getNowFunc()

Get current time in milliseconds.

Const loadTestResource

loadTestResource: loadTestResourceWeb = loadTestResourceWeb

Function which runs a cross-environment resource loader which gets the static resources requested. (e.g. needed for tests purposes).

The following folders are mapped in test environment for resources that are needed for testing purposes:

  • @here/${module}/test/resources
  • @here/${module}/resources

The fileName must be relative to package root.

Exemplary methods invocation:

const binaryData = await loadTestResourceWeb('test-utils', 'test/resources/test.bin', 'binary');

const textData = await  loadTestResourceWeb('test-utils', 'test/resources/test.txt', 'text');

const theme = await loadTestResource('map-theme', 'resources/berlin_tilezen_base.json', 'json');

These locations above are mapped in the same way regardless of the runtime environment used (node.js or browser).

Internally this function loads resources in an environment-agnostic way, using the following:

  • fs module when run in a node.js environment
  • fetch module when run in a browser environment
param

-: module name, @here/ (e.g. @here/harp-vectortile-datasource)

param

-: the requested resource (e.g. @here/harp-vectortile-datasource/test/resources/berlin.bin)

Const loadTestResource

loadTestResource: loadTestResourceNode = loadTestResourceNode

Function which runs a cross-environment resource loader which gets the static resources requested. (e.g. needed for tests purposes).

The following folders are mapped in test environment for resources that are needed for testing purposes:

  • @here/${module}/test/resources
  • @here/${module}/resources

The fileName must be relative to package root.

Exemplary methods invocation:

const binaryData = await loadTestResourceWeb('test-utils', 'test/resources/test.bin', 'binary');

const textData = await  loadTestResourceWeb('test-utils', 'test/resources/test.txt', 'text');

const theme = await loadTestResource('map-theme', 'resources/berlin_tilezen_base.json', 'json');

These locations above are mapped in the same way regardless of the runtime environment used (node.js or browser).

Internally this function loads resources in an environment-agnostic way, using the following:

  • fs module when run in a node.js environment
  • fetch module when run in a browser environment
param

-: module name, @here/ (e.g. @here/harp-vectortile-datasource)

param

-: the requested resource, (e.g. @here/harp-vectortile-datasource/test/resources/berlin.bin)

Functions

addPerformanceResultsSample

  • addPerformanceResultsSample(name: string, time: number): void

assertLogsSync

  • assertLogsSync(fn: () => void, channel: IConsoleLike, type: keyof IConsoleLike, errorMessagePattern: string | RegExp): void
  • Assert that synchronous test function fn will logs specfic message.

    Captures error messages of type from channel and asserts that at least one contain message that matches errorMessagePattern.

    Swallows matching messages, so they don't appear in actual output so "test log" is clean from expected messages.

    Example with console.warn

      assertWillLogSync(() => {
          console.warn("error: fooBar"),
          console,
          "warn"
          /error/
      );

    Parameters

    • fn: () => void

      test function that shall provoke certain log output

        • (): void
        • Returns void

    • channel: IConsoleLike

      Console like object

    • type: keyof IConsoleLike

      type of console message i.e "log" | "error" | "warn" | "info

    • errorMessagePattern: string | RegExp

      string or regular expression that we look for in logs

    Returns void

assertRejected

  • assertRejected(v: Promise<any> | (() => Promise<any>), errorMessagePattern: string | RegExp): Promise<void>
  • Assert that promise is rejected.

    Check that promise v (passed directly or result of function) is eventually rejected with error that matches errorMessagePattern.

    Parameters

    • v: Promise<any> | (() => Promise<any>)
    • errorMessagePattern: string | RegExp

    Returns Promise<void>

calculateStats

canvasToImageData

  • canvasToImageData(canvas: HTMLCanvasElement): Promise<ImageData>

compareImages

  • compareImages(actualImage: ImageData, referenceImage: ImageData, options: TestOptions): { diffImage: ImageData; mismatchedPixels: any }

countCall

  • countCall(name: string, delta?: number): void
  • Report call.

    Convenience utility to be used temporarily in development to confirm expectations about number of calls when measuring performance.

    Call counts are logged after all tests (if in Mocha environment). See reportCallCountsAndReset.

    Usage:

    class Foo {
        push() {
            countCall("Foo#bar")
        }
    }

    It reports following after all tests:

    #countCall: Foo#push called=123

    Parameters

    • name: string
    • Default value delta: number = 1

    Returns void

countCalls

  • countCalls<T>(name: string, fun?: T): T
  • countCalls<T>(fun?: T): T
  • countCalls(): MethodDecorator
  • Count function/method calls decorator.

    Convenience utility to be used temporarily in development to confirm expectations about number of calls when measuring performance.

    Call counts are logged after all tests (if in Mocha environment). See reportCallCountsAndReset.

    Usage - basic functional composition:

    const bar = countCalls("bar",  () => { ... })

    Usage - experimental TypeScript decorators:

    class Foo {
        @countCalls()
        push() {}
    }

    It reports following after execution of all tests:

    ProfileHelper: Foo#push called=123
    ProfileHelper: bar called=1111

    Type parameters

    • T: (...args: any[]) => any

    Parameters

    • name: string
    • Optional fun: T

    Returns T

  • Type parameters

    • T: (...args: any[]) => any

    Parameters

    • Optional fun: T

    Returns T

  • Returns MethodDecorator

createImgElement

  • createImgElement(data: ImageData | ImageBitmap | string): HTMLImageElement

defaultReferenceImageResolver

expressMiddleware

  • expressMiddleware(req: express.Request, res: express.Response, next: express.NextFunction): any

genHtmlReport

getIbctReport

  • getIbctReport(req: express.Request, res: express.Response): Promise<void>

getOutputImagePath

  • getOutputImagePath(imageProps: TestImageProps, outputBasePath: string): string

getOverride

  • getOverride(name: string, defaultValue: string): string

getPerformanceTestResults

getPlatform

  • getPlatform(): string
  • Get platform property.

    Reads platform from:

    • query param IBCT_PLATFORM_OVERRIDE - overrides any detection
    • navigator.userAgent in form $name-$version-$os, optionally adds query param IBCT_PLATATFORM_EXTRA

    IBCT_PLATATFORM_EXTRA is intended to inform about platform properties not detecatble from browser api like "no-gpu" or "headless" or "-nvidia-gtx-whatever".

    Returns string

getReferenceImage

  • getReferenceImage(req: express.Request, res: express.Response): void

getReferenceImageUrl

getTestResourceUrl

  • getTestResourceUrl(module: string, fileName: string): string

getTestResourceUrl

  • getTestResourceUrl(module: string, fileName: string): string

getWebGLRendererStub

  • getWebGLRendererStub(sandbox: sinon.SinonSandbox, clearColorStub: sinon.SinonStub): { getSize: getSize; setClearColor: SinonStub<any[], any>; setSize: setSize; clear: any; dispose: any; forceContextLoss: any; getClearColor: any; getPixelRatio: any; render: any; setOpaqueSort: any; setPixelRatio: any; capabilities: object; debug: object; domElement: object; info: object }

getWebGlInfo

  • getWebGlInfo(): WebGlInfo

imageDataFromImage

  • imageDataFromImage(img: HTMLImageElement): Promise<ImageData>

imageDataToDataUrl

  • imageDataToDataUrl(image: ImageData): string

inBrowserContext

  • inBrowserContext(): boolean

inNodeContext

  • inNodeContext(): boolean

inWebWorkerContext

  • inWebWorkerContext(): boolean

installMagnifier

  • installMagnifier(actual: HTMLImageElement, expected: HTMLImageElement, diff: HTMLImageElement): void
  • Install magnifier control on set of images.

    When user hover mouse over any of images, an popup with information given pixel will be shown. If window has focus, one can also move focus point with keyboard.

    Parameters

    • actual: HTMLImageElement

      -

    • expected: HTMLImageElement

      -

    • diff: HTMLImageElement

      -

    Returns void

installMiddleware

  • installMiddleware(app: express.Router, basePath: string): void
  • Install RenderingTestResultServer in express.Router.

    Example usage i.e webpack-dev-server configuration in webpack.config.js:

    devServer: {
      before: function(app) {
        require('ts-node/register'); // so we can load typescript seamlessly
        const RenderingTestResultServer = require(
         "coresdk/@here/harp-test-utils/lib/rendering/RenderingTestResultServer"
        );
        RenderingTestResultServer.installMiddleware(app);
      }
    }

    Parameters

    • app: express.Router
    • basePath: string

    Returns void

loadImageData

  • loadImageData(url: string): Promise<ImageData>

loadSavedResults

measurePerformanceSync

  • measurePerformanceSync(name: string, repeats: number, test: () => void): Promise<void>
  • Measure time performance of code.

    Executes test code repeats times and measures: min, med (median), sum and avg (average) execution times.

    [[performance.now]] is used as time provider, with fallback to new Date().getTime().

    Measurement reports are saved for later and logged after all tests (if in Mocha environment). See reportPerformanceAndReset.

    Example:

    it('performance test', async () => {
        await measurePerformanceSync("Array/grow", 50, () => {
            // the code under test
            // will be executed 50 times ----^^
        });
    });

    Will print report like this after all tests:

    #performance: Array/grow: min=1 med=2 avg=1.8 sum=72 (50 repeats)

    Parameters

    • name: string

      name of performance test

    • repeats: number

      number of test repeats

    • test: () => void

      tested code

        • (): void
        • Returns void

    Returns Promise<void>

measureThroughputSync

  • measureThroughputSync(name: string, testDuration: number, test: () => void): Promise<void>
  • Measure throughput performance of code.

    Executes test code for timeout milliseconds and reports throughput and aggregated times: min, med (median), sum and avg (average) execution times.

    [[performance.now]] is used as time provider, with fallback to new Date().getTime().

    Measurement reports are saved for later and logged after all tests (if in Mocha environment). See reportPerformanceAndReset.

    Example:

    it('throughput test', async () => {
        await measureThroughputSync("Array/grow", 100, () => {
            // the code under test
            // will be executed for 100 milliseconds ----^^
        });
    });

    Will print report like this after all tests:

    #performance: Array/grow: min=1 med=2 avg=1.8 sum=72 repeats=123 throughput=1242/s

    Parameters

    • name: string

      name of performance test

    • testDuration: number
    • test: () => void

      tested code

        • (): void
        • Returns void

    Returns Promise<void>

postIbctFeedback

  • postIbctFeedback(req: express.Request, res: express.Response): Promise<void>

reportCallCountsAndReset

  • reportCallCountsAndReset(): void

reportPerformanceAndReset

  • reportPerformanceAndReset(): void
  • Report and reset performance measurement results.

    Designed to be called after round of tests. Shows results of all performance tests executed by measurePerformanceSync and measureThroughputSync.

    It resets results afterwards.

    If baseline is available (.profile-helper-baseline.json, customized by PROFILEHELPER_OUTPUT_) then this run results are compared to baseline

    If PROFILEHELPER_COMMAND=baseline resutls are saved as baseline.

    In Mocha runtime it is called automatically in global after callback.

    Returns void

reportPerformanceEntry

reportPerformanceEntryWithBaseline

reportPerformanceResults

setGlobalReporter

  • setGlobalReporter(reporter: Reporter): void

setReferenceImageResolver

  • setReferenceImageResolver(resolver: (imageProps: TestImageProps) => string): void

silenceLoggingAroundFunction

  • silenceLoggingAroundFunction(loggerName: string | string[], func: () => void, minLogLevel?: LogLevel): Promise<void>
  • Sets the specified loggers to only log at the given minLogLevel. Only use this function when you know that you can safely ignore a warning, otherwise you should consider to fix the issue. All previous logging levels are reset after the function is executed.

    Parameters

    • loggerName: string | string[]

      The loggerName, or array of names to set to error

    • func: () => void

      The function to execute with the changed logging

        • (): void
        • Returns void

    • Default value minLogLevel: LogLevel = LogLevel.Error

      The minimum log level that is shown, defaults to LogLevel.Error

    Returns Promise<void>

startStandaloneServer

  • startStandaloneServer(host: string, port: number): void

stubGlobalConstructor

  • stubGlobalConstructor(sandbox: sinon.SinonSandbox, name: string): SinonStub<any[], any> | SinonStub<unknown[], unknown>
  • Create stub of global constructor managed by sandbox.

    A prototype preserving, node/browser environment aware version of `sandbox.stub(window | global, name).

    Use to stub global constructors like Worker or XMLHttpRequest.

    Parameters

    • sandbox: sinon.SinonSandbox

      sinon.Sandbox instance, required for proper cleanup after test

    • name: string

      name of global symbol to be constructor

    Returns SinonStub<any[], any> | SinonStub<unknown[], unknown>

waitForEvent

  • waitForEvent<T>(source: EventSource<T>, eventType: string): Promise<T>
  • Wait for particular event on THREE.EventDispatcher or DOM EventTarget compatible objects.

    Automatically unregisters itself receiving the event.

    Type parameters

    • T

    Parameters

    • source: EventSource<T>

      event source or target that has add/removeEventListener(type, listener) method. protocol

    • eventType: string

      type of event

    Returns Promise<T>

    promise that resolves to first event that is received

waitImageLoaded

  • waitImageLoaded(img: HTMLImageElement): Promise<void>

willEventually

  • willEventually<T>(test: () => T): Promise<T>
  • Repeats block of code until it passes without AssertionError.

    Additionally, if test fails due to timeout, last error that was encountered is rethrown, so any error that have constructor called AssertionError (matches chai assertions) will cause test to be repeated after 1ms delay.

    The last error that blocked willEventually from resolving will be rethrown in afterEach to mark which assertion didn't hold (see [[reportWillEventuallyBlockingAssertion]]).

    Use for APIs that are internally asynchronous without explicit means to monitor completion of tasks.

    Example:

    const foo = someCodeThatIsAsync({count: 6})

    await willEventually(() => { assert.equals(foo.readyCount, 6); });

    Type parameters

    • T

    Parameters

    • test: () => T

      closure with assertions that must pass

        • (): T
        • Returns T

    Returns Promise<T>

    promise that resolves when test passes without any error

Generated using TypeDoc