Skip to content

Commit 16e02ae

Browse files
espyjanl
authored andcommitted
tests: update testing docs
1 parent 2a27f22 commit 16e02ae

2 files changed

Lines changed: 90 additions & 27 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ jobs:
277277
matrix:
278278
node: [20, 22]
279279
cmd:
280-
- COUCH_HOST=http://admin:password@127.0.0.1:5984 CLIENT=firefox npm run test-webpack
280+
- CLIENT=firefox npm run test-webpack
281281
- AUTO_COMPACTION=true npm test
282282
- npm run test-unit
283283
- npm run test-component

TESTING.md

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
# Running PouchDB Tests
1+
# Running PouchDB Tests <!-- omit from toc -->
22

33
This document covers all the types of tests the PouchDB project has and how to
44
run them. PouchDB has been primarily developed on Linux and macOS, if you are
55
using Windows then these instructions will have problems, we would love your
66
help fixing them though.
77

8-
9-
## Installation
8+
> [!WARNING] VERY IMPORTANT
9+
> **Always set the `COUCH_HOST` env var to a proper CouchDB for the integration tests!**
10+
>
11+
> If you don’t, the integration tests currently fall back to `pouchdb-server`, which is
12+
> no longer reliable and causes random test failures. (20000ms timeouts).
13+
>
14+
> Until this is resolved, please set the `COUCH_HOST` env var:
15+
> `$ CLIENT=firefox COUCH_HOST=http://admin:admin@127.0.0.1:5984 npm test`
16+
>
17+
> This is necessary for both `npm test` and `npm run dev`.
18+
19+
## Table of Contents <!-- omit from toc -->
20+
21+
- [Running the integration tests](#running-the-integration-tests)
22+
- [Test options](#test-options)
23+
- [Other sets of tests](#other-sets-of-tests)
24+
- [`find` and `mapreduce`](#find-and-mapreduce)
25+
- ["Fuzzy" tests](#fuzzy-tests)
26+
- [Performance tests](#performance-tests)
27+
- [Running tests in the browser](#running-tests-in-the-browser)
28+
- [Other test tasks](#other-test-tasks)
29+
- [Troubleshooting and useful info when working on tests](#troubleshooting-and-useful-info-when-working-on-tests)
30+
- [Where do I get a CouchDB from?](#where-do-i-get-a-couchdb-from)
31+
- [Utils won’t update](#utils-wont-update)
32+
- [Flaky tests and random timeouts](#flaky-tests-and-random-timeouts)
1033

1134
The PouchDB test suite expects an instance of CouchDB (version 1.6.1 and above)
1235
running in [Admin Party](http://guide.couchdb.org/draft/security.html#party) on
@@ -38,6 +61,9 @@ The main test suite can be run using the following command:
3861

3962
$ npm test
4063

64+
> [!NOTE]
65+
> If the tests don‘t pass on `main`, try building once before running the tests: `$npm run build`.
66+
4167
PouchDB runs in the browser and on Node.js, and has multiple different storage
4268
backends known as _adapters_. In the browser these are `idb`, `indexeddb` and
4369
`memory` and on Node.js they're `leveldb` and `memory`.
@@ -59,7 +85,7 @@ replication to a remote server, and by default we start an instance of
5985
The integration tests support the following options, configured via environment
6086
variables.
6187

62-
#### `ADAPTERS` (default: depends on `CLIENT`)
88+
#### `ADAPTERS` (default: depends on `CLIENT`) <!-- omit from toc -->
6389

6490
Comma-separated list of preferred adapter backends that PouchDB will use for local
6591
databases. These are selected automatically based on the execution environment,
@@ -74,21 +100,21 @@ a remote server, identified by `COUCH_HOST`. This is not necessary for
74100
integration tests since they use a mixture of local and remote databases to
75101
check compatibility, but it's useful for the `find` and `mapreduce` suites.
76102

77-
#### `AUTO_COMPACTION` (default: `0`)
103+
#### `AUTO_COMPACTION` (default: `0`) <!-- omit from toc -->
78104

79105
Set this to `1` to enable automatic compaction of PouchDB databases by default.
80106

81-
#### `BAIL` (default: `1`)
107+
#### `BAIL` (default: `1`) <!-- omit from toc -->
82108

83109
Normally the test runner will halt as soon as it discovers a failing test. Set
84110
this to `0` to prevent this behaviour.
85111

86-
#### `CLIENT` (default: `node`)
112+
#### `CLIENT` (default: `node`) <!-- omit from toc -->
87113

88114
Sets the target platform the tests will execute on. Set this to
89115
`firefox`, `chromium` or `webkit` to execute the tests in the browser.
90116

91-
#### `COUCH_HOST`
117+
#### `COUCH_HOST` <!-- omit from toc -->
92118

93119
Some tests perform replication between local and remote databases. When we
94120
create a remote database, we get the URL of the remote server from `COUCH_HOST`.
@@ -98,45 +124,45 @@ CORS enabled.
98124
If not set explicitly, this variable is set automatically based on the other
99125
configuration values.
100126

101-
#### `FETCH` (default: `0`)
127+
#### `FETCH` (default: `0`) <!-- omit from toc -->
102128

103129
Set this to `1` to stop PouchDB falling back to `XMLHttpRequest` if `fetch()` is
104130
not available.
105131

106-
#### `GREP`
132+
#### `GREP` <!-- omit from toc -->
107133

108134
Use this to request that a specific test is run; if you set `GREP='name of
109135
test'` then only those tests whose names include the string `name of test` will
110136
run. Regular expressions are also supported.
111137

112-
#### `PLUGINS` (default: empty)
138+
#### `PLUGINS` (default: empty) <!-- omit from toc -->
113139

114140
Comma-separated list of additional plugins that should be loaded into the test
115141
environment. For example:
116142

117143
$ PLUGINS=pouchdb-find npm test
118144

119-
#### `POUCHDB_SRC`
145+
#### `POUCHDB_SRC` <!-- omit from toc -->
120146

121147
This overrides the path used to load PouchDB in the browser. We use this in CI
122148
to select different builds of the PouchDB library, for example to test the
123149
Webpack version, etc.
124150

125151
This is an alternative to `SRC_ROOT` and `USE_MINIFIED`.
126152

127-
#### `SRC_ROOT`
153+
#### `SRC_ROOT` <!-- omit from toc -->
128154

129155
This overrides the path used to load all PouchDB files in the browser. We use
130156
this in performance tests to allow easily comparing two different versions of
131157
PouchDB, including plugin and adapter implementations.
132158

133-
#### `USE_MINIFIED`
159+
#### `USE_MINIFIED` <!-- omit from toc -->
134160

135161
This changes the file extension used for loading PouchDB files in the browser.
136162
This can be used in CI and performance testing to select the minified version of
137163
PouchDB and its adapters, plugins, etc.
138164

139-
#### `SERVER` (default: `pouchdb-express-router`)
165+
#### `SERVER` (default: `pouchdb-express-router`) <!-- omit from toc -->
140166

141167
To support remote replication tests, we start a server in the background that
142168
speaks the CouchDB replication protocol. This variable controls how that is
@@ -151,11 +177,11 @@ following values:
151177
- `couchdb-master`: use this value if you already have CouchDB running; it
152178
causes `COUCH_HOST` to be set to the correct value.
153179

154-
#### `SKIP_MIGRATION` (default: `0`)
180+
#### `SKIP_MIGRATION` (default: `0`) <!-- omit from toc -->
155181

156182
Set this to `1` to skip the migration tests.
157183

158-
#### `VIEW_ADAPTERS` (default: `memory`)
184+
#### `VIEW_ADAPTERS` (default: `memory`) <!-- omit from toc -->
159185

160186
Comma-separated list of preferred view adapter backends that PouchDB will use.
161187
This variable overrides the default choice and causes additional adapters to
@@ -226,7 +252,7 @@ This supports most of the same options as the integration suite, particularly
226252
the `CLIENT`, `ADAPTERS` and `GREP` options. It has some additional options of
227253
its own:
228254

229-
#### `ITERATIONS`
255+
#### `ITERATIONS` <!-- omit from toc -->
230256

231257
Sets the number of iterations each test uses by default.
232258

@@ -252,15 +278,20 @@ server and opening a browser window yourself. To run the server:
252278

253279
$ npm run dev
254280

281+
You will almost always want to include the `find` plugin though, without it, all the `find` tests will fail:
282+
283+
$ PLUGINS=pouchdb-find npm run dev
284+
255285
Then you can open the page for any of the test suites via the following URLs:
256286

257287
- `http://127.0.0.1:8000/tests/integration/`
258288
- `http://127.0.0.1:8000/tests/find/`
259289
- `http://127.0.0.1:8000/tests/mapreduce/`
260290
- `http://127.0.0.1:8000/tests/performance/`
261291

262-
The test options are controlled by editing the query string; some of the common
263-
command-line options and their query string equivalents are:
292+
You can re-run tests by reloading, and only run specific suites by clicking on the suite names, this sets the `grep` query string mentioned below. This works well in conjunction with setting `.only` on individual tests in that suite.
293+
294+
The test options are controlled by editing the query string; some of the common command-line options and their query string equivalents are:
264295

265296
| Environment variable | Query-string param |
266297
| -------------------- | ------------------ |
@@ -281,29 +312,61 @@ command-line options and their query string equivalents are:
281312
There are a few other tasks we run during CI and which you will find useful to
282313
run during development.
283314

284-
### `npm run eslint`
315+
### `npm run eslint` <!-- omit from toc -->
285316

286317
Checks that all code in the project follows our formatting and style guide. This
287318
runs before any other tasks are run during our CI build.
288319

289-
### `npm run test-unit`
320+
### `npm run test-unit` <!-- omit from toc -->
290321

291322
Runs the unit tests; running these can give more precise feedback about key
292323
building blocks that are not working.
293324

294-
### `npm run test-component`
325+
### `npm run test-component` <!-- omit from toc -->
295326

296327
Tests some additional components besides the core database functionality, for
297328
example authentication and read-only replication.
298329

299-
### `npm run test-coverage`
330+
### `npm run test-coverage` <!-- omit from toc -->
300331

301332
Runs the test suite with coverage analysis turned on.
302333

303-
### `npm run test-webpack`
334+
### `npm run test-webpack` <!-- omit from toc -->
304335

305336
Checks that the Webpack build of PouchDB works correctly.
306337

307-
### `npm run verify-build`
338+
### `npm run verify-build` <!-- omit from toc -->
308339

309340
Checks that the build is correct.
341+
342+
## Troubleshooting and useful info when working on tests
343+
344+
### Where do I get a CouchDB from?
345+
346+
See the [official CouchDB documentation](https://docs.couchdb.org/en/stable/install/index.html) for a guide on how to install CouchDB.
347+
348+
Your CouchDB will most likely then run at `http://127.0.0.1:5984`. All you need to do now is enable CORS so CouchDB will accept requests from PouchDB in the tests. Add `http://127.0.0.1:8000`, the test server’s domain. There are several ways to do this, easiest first:
349+
350+
1. In the [CouchDB admin UI](http://127.0.0.1:5984/_utils), click on `Configuration` -> `CORS`.
351+
2. Via cURL (`_local` is the node name in single-node databases, which your local dev CouchDB probably is):
352+
```sh
353+
curl 'http://admin:password@127.0.0.1:5984/_node/_local/_config/cors/origins' -X PUT -d '"http://127.0.0.1:8000"'
354+
```
355+
3. There’s also an older [npm package](https://github.com/pouchdb/add-cors-to-couchdb) to help you do this porgrammatically.
356+
357+
### Utils won’t update
358+
359+
If you’re modifying the test utils (`/tests/integration/utils.js` etc.) or adding logs to them, you need to rebuild these before running your tests again, eg.:
360+
361+
```sh
362+
$ npm run build && CLIENT=firefox npm test
363+
```
364+
365+
### Flaky tests and random timeouts
366+
367+
If you regularly run into random timeouts, please re-read the top of this file and set the `COUCH_HOST` env var when running tests or the dev server, eg.:
368+
369+
```sh
370+
$ CLIENT=firefox COUCH_HOST=http://admin:admin@127.0.0.1:5984 npm test
371+
```
372+

0 commit comments

Comments
 (0)