Skip to content

Latest commit

 

History

History
466 lines (314 loc) · 26.7 KB

TESTING.md

File metadata and controls

466 lines (314 loc) · 26.7 KB

Testing

What we test

Suite Location Runs in Tests
XYZ API apps/xyz/tests Node API modules, query templates, providers, user and workspace modules
MAPP library apps/mapp/tests Headless Chromium MAPP library modules against a local XYZ host
MAPP.UI library apps/ui/tests Headless Chromium MAPP.UI modules and user flows in the XYZ default view against a local XYZ host

How we test

All suites use Vitest.

XYZ API tests import the API modules in Node. Tests which need a database run against the devdb, eg. the query module tests. Dependencies the devdb can't provide, like login views or remote sources, are replaced with Vitest mocks. Requests and responses are created with node-mocks-http.

MAPP library tests run with Vitest browser mode in Chromium with the Playwright provider. The tests have the DOM, Openlayers and the mapp global. Vite serves the apps/mapp/lib source modules, so no build is required.

The global setup from packages/testhost starts one devdb database and one XYZ host for the test run. The devdb can't create vector tiles, so the testhost encodes the tiles of mvt layers. The mapp.host is the testhost url, so mapp.utils.xhr requests the XYZ API with the test workspace.

Test data is the devdb seed data. The devdb is a PGlite PostgreSQL database with PostGIS, which runs in memory in the Node process. Every run seeds a new database from the seed files.

Workspaces configure the layers and templates for the tests:

Workspaces Used by
apps/xyz/tests/assets XYZ API tests
apps/mapp/tests/workspaces MAPP library tests
packages/testhost/workspaces Test views and checks for bugs and features of a pull request

Tests don't depend on the network for data. Openlayers is loaded from the ol package, the tile layers in test workspaces are not displayed, and requests to external services are stubbed.

There are two exceptions:

  • CDN imports: libraries which MAPP loads from a CDN at runtime are tested with the CDN import, eg. the maplibre format tests import the pinned maplibre-gl version with mapp.utils.esmImport from esm.sh, as MAPP does in production. A broken CDN build of a pinned version fails the tests. Modules which only call a CDN library as a dependency of the module under test may still stub the import.
  • Mapbox API: the maplibre format tests request Mapbox styles and tiles with an access token, and are skipped without one. The invalid-token test always requests the Mapbox API, so it needs network access to api.mapbox.com with or without a token, eg. for pull requests from forks.

The osm template is the base map of all test workspaces. Each workspace adds the template as the osm layer of its default locale, so every locale has the osm layer. The layer has display: false, and the testview workspace overrides it with display: true. The tiles format tests use the layer, including its style.contextFilter.

Running tests

Install the dependencies and the Chromium browser for the MAPP tests:

pnpm install
pnpm --filter=@geolytix/mapp exec playwright install chromium

Run all suites:

pnpm test

Run a suite:

Command Suite
pnpm test:xyz XYZ API
pnpm test:mapp MAPP library
pnpm test:ui MAPP.UI library
pnpm knowledge:test Knowledge bundle checker, not part of pnpm test
pnpm knowledge:examples The JSON workspace examples of the knowledge concepts, served by the testhost with the devdb

Run a single file or the tests matching a name from the package directory:

cd apps/mapp
pnpm test tests/layer/styleParser.test.mjs
pnpm test -t "graduated"
pnpm exec vitest run --reporter=verbose    # list every test with its result

On Windows, run the MAPP tests from a directory path with an uppercase drive letter, eg. C:\Users\.... Vite serves a module from two urls if the working directory has a lowercase drive letter, eg. in a terminal which was opened in c:\Users\.... Each test file then imports another instance of a lib module than the mapp global has, and every is assigned to mapp test fails with values which have no visual difference.

The XYZ API test scripts run Vitest with the environment in varlock/vanilla.env. The tests assign the xyzEnv properties they need, so they also pass when Vitest runs without varlock, eg. in VS Code.

Mapbox access token

The Mapbox API tests need a Mapbox access token. The MAPP vitest config reads the MAPBOX_ACCESS_TOKEN from the environment, or from one of Vite's .env files for the test mode in the repository root: .env, .env.local, .env.test and .env.test.local. A value in the environment takes precedence over the files. The .env files are gitignored, so the token is not committed.

# .env.local
MAPBOX_ACCESS_TOKEN=pk.xxx

# Optional, another Mapbox style instead of the default Mapbox Studio style of the tests.
MAPBOX_STYLE_URL=mapbox://styles/{account}/{style}

The tests which require a token are skipped without a token. The tests with an invalid or without a token always run. The invalid-token test requests the Mapbox API and expects an HTTP 401 or HTTP 403.

The Unit Tests workflow provides the token from the MAPBOX_ACCESS_TOKEN repository secret. Pull requests from forks don't receive the secret and skip the tests. The Build and Release workflows don't provide the token, so a rotated token can't block a build or a release. The invalid-token test still runs in both and needs api.mapbox.com to be reachable, so a Mapbox outage can fail a build or a release. The Mapbox code paths are not included in the coverage report.

Coverage

pnpm test:xyz:coverage
pnpm test:mapp:coverage
pnpm test:ui:coverage

The terminal shows the coverage per file, with the uncovered line numbers, and the totals. Run with Coverage in the VS Code extension highlights the covered and uncovered lines in the source.

The coverage runs also write an lcov.info for the SonarQube analysis in CI: coverage/lcov.info for the XYZ API, apps/mapp/coverage/lcov.info for the MAPP library and apps/ui/coverage/lcov.info for the MAPP.UI library. The MAPP.UI coverage is from the module tests only.

The MAPP and MAPP.UI coverage have thresholds in apps/mapp/vitest.config.mjs and apps/ui/vitest.config.mjs. A coverage run fails if the coverage drops below the thresholds. Raise the thresholds when new tests increase the coverage.

The uhtml module is a minified third party library. The module is excluded from the MAPP coverage in the Vitest config, and from the SonarQube coverage with the sonar.coverage.exclusions in the sonar-project.properties.

Debugging

MAPP library tests can run in a visible Chromium window:

cd apps/mapp
pnpm exec vitest --browser.headless=false tests/layer/styleParser.test.mjs

Open the DevTools in the browser window. The Sources panel has the lib source modules for breakpoints, and a debugger; statement in a test pauses the test. The Console shows the MAPP logs and the Network panel shows the requests to the testhost.

The testhost can serve a test workspace for a manual check in a browser:

pnpm testview --workspace=apps/mapp/tests/workspaces/_workspace.json

Open a locale of the workspace in the locale view, eg. http://127.0.0.1:3001/views/locale/?locale=smoke. The view has the layers of the locale with their feature count and a checkbox to show them, eg. the hidden OSM Basemap.

VS Code

The Vitest extension (vitest.explorer) is the preferred way to run, debug and check the coverage of tests.

The extension finds the vitest.config.mjs of each app and lists the tests by config in the Testing view. Each test has gutter icons in the editor to run or debug it.

Action Description
Run Test Runs a test, a file or a suite. Failed assertions are shown inline in the editor.
Debug Test Runs a test with the VS Code debugger. Breakpoints in the test and in the source modules pause the test.
Run with Coverage Runs the tests with the coverage settings of the config. The editor highlights covered and uncovered lines, and the Test Coverage view lists the coverage per file.
The extension runs the MAPP library tests in Chromium. Debug Test attaches the debugger to the browser, so breakpoints in apps/mapp/lib modules pause in VS Code. Debug a single test file at a time.

The MAPP and MAPP.UI coverage thresholds apply to coverage runs in the extension. A run with coverage for a single file reports a threshold error, which can be ignored.

CI

The Unit Tests workflow runs pnpm test, which includes the MAPP.UI module and default view tests, for pushes and pull requests to the main, major, minor and patch branches.

The Build workflow runs the XYZ API, MAPP library and MAPP.UI library coverage for the SonarQube analysis. The coverage runs fail below the coverage thresholds.

The Knowledge check workflow runs pnpm knowledge:test, pnpm knowledge:check and pnpm knowledge:examples. The Knowledge freshness workflow checks with pnpm knowledge:changed that a pull request updates the concepts of the modules it changes; only this workflow runs again when the knowledge-unaffected label changes. See knowledge checks.

Writing tests

These conventions apply to all suites:

  • One test file per module, mirroring the source path, eg. apps/mapp/tests/layer/themes/categorized.test.mjs for apps/mapp/lib/layer/themes/categorized.mjs.
  • A describe() block per module or feature, with an it() test per behaviour. Use it.each() for the same behaviour with different input.
  • Assert specific values, eg. a style colour, a feature count, a status code or a request param. Avoid snapshots of whole objects.
  • Add seed files for new test data rather than changing existing seed rows, which tests depend on.
  • Tests which write to the database use the empty example.scratch_location table. Each test inserts its own record, because all test files of a run share the devdb, and asserts only on its own records.
  • The API authorizes location writes by the layer config. The scratch layers have a hidden draw config for location_new, deleteLocation for location_delete, and edit on the entries a test updates. A layer config assigned in the browser, eg. layer.draw = {...}, is not sent to the API.
  • A test for a known bug is written for the correct behaviour and marked with it.fails(), with a link to the issue. The test fails once the bug is fixed, and the .fails must be removed with the fix.
  • Suppress expected console output and assert on it where the message is the behaviour.

See the Vitest API for the available test methods and assertions.

XYZ API tests

tests/setup.mjs runs before each test file and creates the globalThis.xyzEnv. A test which needs xyzEnv properties assigns them before it imports the module under test, because modules read the xyzEnv when they are imported.

A devdb database is started by a test file which needs a database. Tests should use the devdb rather than mocks where possible. A test against the devdb runs the real SQL and the real database responses, while a mocked database only returns what the test expects. See query.test.mjs:

import { afterAll } from 'vitest';
import { startDevDb } from '../../../../packages/devdb/index.js';

const devdb = await startDevDb({ port: 0 });

afterAll(() => devdb.stop());

globalThis.xyzEnv = {
  WORKSPACE: 'file:./tests/assets/query_workspace.json',
  DBS_DEVDB: devdb.connectionString,
};

Add seed files to the devdb for the data a test needs.

Mock the dependencies which the devdb can't provide, eg. login views, remote sources or a database error.

Module mocks replace the import of a module. vi.mock() calls are hoisted to the top of the file. Use a vi.fn() proxy to change the mock implementation in a test:

import { describe, expect, it, vi } from 'vitest';

const fromACLFn = vi.fn();

vi.mock('@geolytix/xyz-app/mod/user/fromACL.js', () => ({
  default: (...args) => fromACLFn(...args),
}));

const { default: auth } = await import('@geolytix/xyz-app/mod/user/auth.js');

describe('auth', () => {
  it('returns the user from the acl', async () => {
    // The auth module receives this user from the mocked fromACL module.
    fromACLFn.mockImplementation(() => ({ email: 'test@geolytix.co.uk' }));

    // Call auth with a request and assert on the returned user.
  });
});

See auth.test.mjs for the complete tests.

Request mocks from node-mocks-http provide the req and res for an API module:

import { createMocks } from 'node-mocks-http';

const { req, res } = createMocks({ params: { template: 'simple_select' } });

await query(req, res);

expect(res.statusCode).toBe(200);
expect(res._getData()).toEqual({ greeting: 'hello' });

Fetch mocks intercept fetch requests from API modules with the undici MockAgent:

import { MockAgent, setGlobalDispatcher } from 'undici';

const mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);

mockAgent
  .get('https://example.com')
  .intercept({ path: '/workspace.json' })
  .reply(200, { locale: {} });

Console mocks from mockConsole() in tests/scaffold.mjs replace a console method for the test file and collect the messages. Call it at the top level of a test file or in a describe() body, not in an it() test:

import { mockConsole } from '../../scaffold.mjs';

const mockWarnings = mockConsole('warn');

it('warns about a potential SQL injection', () => {
  sql_table_insert(req);

  expect(mockWarnings[0]).toBe(
    'Potential SQL Injection in sql_table_insert request body.',
  );
});

MAPP.UI library tests

The MAPP.UI library has two kinds of tests in apps/ui/tests:

Tests Files Runner Test
Module tests *.test.mjs, mirroring apps/ui/lib Vitest browser mode A ui module, eg. an element, legend or infoj entry, with its params and edge cases
Default view tests *.spec.mjs Playwright Test User flows in the XYZ default view across modules

pnpm test:ui runs both. From apps/ui, pnpm test:modules runs the module tests and pnpm test:view the default view tests. pnpm test:ui:coverage reports the coverage of apps/ui/lib from the module tests.

Module tests

The module tests run like the MAPP library tests, with the testhost global setup and the UI test workspace. tests/setup.mjs loads Openlayers, the MAPP library and the MAPP.UI library, which assigns itself as mapp.ui. Tests call the modules from mapp.ui:

import { describe, expect, it, vi } from 'vitest';

const { chkbox } = mapp.ui.elements;

describe('ui/elements/chkbox', () => {
  it('calls onchange with the checked state and the val param', () => {
    const onchange = vi.fn();

    const node = chkbox({ label: 'Show', val: 'a', onchange });

    // A click on a checkbox which is not in the document doesn't dispatch a change event.
    document.body.append(node);

    node.querySelector('input').click();

    expect(onchange).toHaveBeenCalledWith(true, 'a');

    node.remove();
  });
});

Remove the elements which a test appends to the document, eg. dialogs and toasts, so they don't affect the next test. The setup restores all vi.spyOn mocks after every test.

tests/helpers.mjs exports the MAPP test helpers and provides helpers for tests with layers and locations. The tests use the grocery and scratch locales from the MAPP library tests:

Helper Description
groceryMapview(configure) Creates a mapview for the grocery locale and adds the locale layers. The configure method can change the layer configuration before the mapview is created.
layersListview(configure) Creates a layers listview for a grocery mapview, like the default view, and returns the mapview, listview and the retail_points layer with its layer.view.
locationsListview(mapview) Creates a locations listview for a mapview, like the default view, with the layers and locations tabs. The listview creates the location.view of a location.
lidlLocation(configure) Gets the Lidl Walworth Road location of the retail_points layer with its location.view. The record must not be changed.
scratchLocation(configure, values) Inserts a record into the example.scratch_location table and gets its location with its location.view, for tests which update or delete a location.
entry(location, field) Returns the infoj entry of a location for a field.
import { expect, it } from 'vitest';
import { layersListview } from '../helpers.mjs';

it('orders the panels by the viewConfig.keyOrder', async () => {
  const { layer } = await layersListview((layers) => {
    layers.retail_points.viewConfig = { keyOrder: ['filter', 'style'] };
  });

  expect(layer.view.querySelector('.content').firstElementChild.dataset.id).toBe(
    'filter-drawer',
  );
});

Await the layer.loading promise after a test reloads a layer, eg. with layer.show() or a click on a legend category, before the assertions on the reloaded data.

The test files run one at a time, for the same reason as the default view tests run in one worker. The test timeout is 30 seconds, so a test doesn't time out while the testhost is blocked.

Default view tests

The default view tests run with Playwright Test and open the XYZ default view (/view?locale={key}) from the testhost. The tests use the view like a user, with Playwright locators for the panels, buttons and inputs.

The playwright.config.mjs starts the testhost with the UI test workspace. The testhost redirects the built bundles and stylesheets of the view to the MAPP and MAPP.UI source modules, so the tests don't require a build.

tests/fixtures.mjs provides the test with a page that serves Openlayers from the ol package for the CDN script of the view and aborts other external requests, and collects uncaught errors in page.errors. openView(page, locale) opens the default view and resolves once the view has added the layers and panels.

import { expect, openView, test } from './fixtures.mjs';

test('lists the locale layers in the layers panel', async ({ page }) => {
  await openView(page, 'grocery');

  await expect(page.locator('#layers').getByText('OSM Basemap')).toBeVisible();
});

The tests run in one worker. With parallel pages, closing an HTTP socket in the testhost blocks the Node process for several seconds on Windows and the page loads time out.

Debug a test in a visible browser with pnpm exec playwright test --headed or step through it with --debug from apps/ui. A failed test keeps a trace in test-results, which opens with pnpm exec playwright show-trace.

MAPP library tests

The MAPP library tests depend on the XYZ service running locally with a devdb database. The global setup from packages/testhost starts the testhost with a new devdb database on a free port before the tests and stops it after the run, so no host needs to be started for the tests. The layer data, locales and query responses in the tests come from this host, not from mocks.

tests/setup.mjs runs in the browser before each test file. It loads Openlayers and the MAPP library with the testhost as mapp.host. Most MAPP modules use the mapp global, so tests can call mapp.* or import a module from lib directly.

tests/helpers.mjs documents the seed rows and provides:

Helper Description
getLocale(key) Requests a locale with its layers from the test workspace.
createMapview(locale, view, params) Creates a mapview with additional mapview params, eg. an attribution. The mapview is disposed when the test finishes.
addLayers(mapview) Adds the locale layers and awaits mapview.renderComplete.
changeEnd(mapview) Resolves after the debounced mapview.changeEnd has called the layer changeEnd methods. Create the promise before the map view changes.
sourceFeatures(layer) Returns the features of a vector layer sorted by id.
newLocation(layer, values) Inserts a record with the location_new template and returns its id.
getRecord(layer, id) Requests a record with the location_get template, or null without a record.
pointerEvent(mapview, type, pixel, init) Passes an Openlayers map browser event for a map pixel to the mapview.Map. See Map interactions.
pointerClick(mapview, pixel, init) Passes the events of a click on a map pixel to the mapview.Map.
pointerDrag(mapview, from, to) Passes the events of a drag between two map pixels to the mapview.Map.
pixelPosition(pixel) Returns the position of a map pixel for the userEvent methods.

A test suite which requests the XYZ API has its own locale file in tests/workspaces/locales. Add the locale file as a template to the _workspace.json and request the locale by its template key:

"templates": {
  "smoke": {
    "src": "file:apps/mapp/tests/workspaces/locales/smoke.json"
  }
}
import { describe, expect, it } from 'vitest';
import {
  addLayers,
  createMapview,
  getLocale,
  sourceFeatures,
} from './helpers.mjs';

describe('Smoke', () => {
  it('renders wkt layers with the devdb rows', async () => {
    const locale = await getLocale('smoke');

    const mapview = await createMapview(locale, { lat: 48, lng: 5, z: 4 });

    await addLayers(mapview);

    const ids = sourceFeatures(mapview.layers.locations).map((feature) =>
      feature.get('id'),
    );

    expect(ids).toEqual([1, 2, 3, 4]);
  });
});

Modules which don't request the API are tested without a locale, with layer, feature and style objects created in the test. Stub external libraries which mapp.utils loads from a CDN, eg. with vi.spyOn(mapp.utils, 'esmImport').

Map interactions

The mapview interactions in lib/mapview/interactions respond to pointer events on the map: the highlight interaction highlights and selects features, and the draw, modify and zoom interactions wrap an Openlayers interaction. The interaction tests have two means to create these events.

Map browser events are the default. The pointerEvent() helper creates an Openlayers MapBrowserEvent for a map pixel and passes it to mapview.Map.handleMapBrowserEvent(). The Map dispatches the event to its listeners and to its interactions. This is how Openlayers tests its own interactions.

import { pointerClick, pointerEvent } from '../../helpers.mjs';

const pixel = mapview.Map.getPixelFromCoordinate(
  feature.getGeometry().getCoordinates(),
);

// The highlight interaction highlights the feature at the pixel.
pointerEvent(mapview, 'pointermove', pixel);

expect(mapview.interaction.current.F).toBe(feature);

// A touch click, or a right click to remove the last vertex of a drawing.
pointerEvent(mapview, 'click', pixel, { pointerType: 'touch' });
pointerEvent(mapview, 'pointerdown', pixel, { button: 2, buttons: 2 });

// Each click adds a vertex to the drawing.
pointerClick(mapview, [100, 100]);

The events are handled synchronously, so a test asserts right after the event without waiting. The test controls the pointer type and the buttons of the event. Keep these in mind:

  • The Map only handles events once it has rendered a frame. The helper renders the Map if required. Call mapview.Map.renderSync() before reading a coordinate from a pixel in a test which has not awaited addLayers().
  • Openlayers checks the first pointermove after a click for a drag and does not update the drawing. Pass two pointermove events to move the sketch of a draw interaction.
  • The mousedown, mouseup, mouseleave and touchstart listeners of the highlight interaction are on the map target, not on the Map. Dispatch these events on the mapview.Map.getTargetElement().
  • The draw and modify interactions have a keyup listener on the document. Finish an interaction before the test finishes, or a later Escape key finishes the interaction of a disposed map.
  • A popup with autoPan moves the map view. Read the coordinates of pixels before the popup is shown.

Real input from the userEvent methods of the vitest/browser module checks the path which the map browser events skip: the browser dispatches trusted pointer events on the map, and Openlayers creates the map browser events from them. Each interaction has one test with real input.

import { userEvent } from 'vitest/browser';
import { pixelPosition } from '../../helpers.mjs';

const target = mapview.Map.getTargetElement();

await userEvent.hover(target, { position: pixelPosition(pixel) });
await userEvent.click(target, { position: pixelPosition(pixel) });

// Drag a vertex of the modified geometry.
await userEvent.dragAndDrop(target, target, {
  sourcePosition: pixelPosition([300, 100]),
  targetPosition: pixelPosition([350, 50]),
});

Real input is async and slower, and the position is only exact to a pixel. A render between two inputs may create the features of a cluster layer again. Assert with a tolerance, and don't assert the identity of cluster features after an await.

Methods which don't depend on the pointer are called directly, eg. mapview.interaction.getFeature(), mapview.interaction.finish() and the deleteCondition() of the modify interaction.