@telefonica/acceptance-testing 3.0.0-beta9 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -251,29 +251,68 @@ await elementHandle.uploadFile(prepareFile('/path/to/file'));
251
251
 
252
252
  ## Collecting coverage
253
253
 
254
- This library automatically collects code coverage after each test by reading the `window.__coverage__` object
255
- if available.
254
+ Set the `COLLECT_ACCEPTANCE_COVERAGE` environment variable to enable coverage collection.
256
255
 
257
- To create that object you should instrument the code with an `istanbul` compatible tool (for example
258
- [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul)).
256
+ The code must be instrumented with [nyc](https://github.com/istanbuljs/nyc/blob/main/docs/instrument.md),
257
+ [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul) or any
258
+ [istanbul](https://github.com/istanbuljs/istanbuljs) compatible tool.
259
259
 
260
- The coverage report for each test will be saved as `json` files. To change the destination folder, set the
261
- `coveragePath` property in your config. The default value is `reports/coverage-acceptance`.
260
+ ### Frontend coverage information
261
+
262
+ After each test the coverage information will be collected by reading the `window.__coverage__` object from
263
+ the opened page.
264
+
265
+ ### Backend coverage information
266
+
267
+ To collect coverage from your backend, you must create an endpoint that serves the coverage information and
268
+ specify it the `coverageUrls` property in your config. The library will make a `GET` request to each URL and
269
+ save the report from the response as a `json` file. The default value is `[]`.
270
+
271
+ The backend coverage will be collected after all the tests in the suite have run.
272
+
273
+ The response must be a JSON with the following structure: `{coverage: data}`.
274
+
275
+ Example route in NextJS to serve coverage information:
276
+
277
+ ```ts
278
+ import {NextResponse} from 'next/server';
279
+
280
+ export const GET = (): NextResponse => {
281
+ const coverage = (globalThis as any).__coverage__;
282
+ if (coverage) {
283
+ return NextResponse.json({coverage});
284
+ }
285
+ return NextResponse.json({error: 'Not found'}, {status: 404});
286
+ };
287
+
288
+ export const dynamic = 'force-dynamic';
289
+ ```
290
+
291
+ ### Configuration
292
+
293
+ The coverage information will be saved as `json` files. To change the destination folder, set the
294
+ `coveragePath` property in your config. The default value is `reports/coverage-acceptance`. The `json` files
295
+ will be stored inside `<coveragePath>/.nyc_output`.
262
296
 
263
297
  Example config:
264
298
 
265
299
  ```json
266
300
  {
267
301
  "acceptanceTests": {
268
- "coveragePath": "coverage/acceptance"
302
+ "coveragePath": "coverage/acceptance",
303
+ "coverageUrls": ["http://localhost:3000/api/coverage"]
269
304
  }
270
305
  }
271
306
  ```
272
307
 
273
- After running the tests, you can use a tool like `nyc` to generate the coverage summaries.
308
+ ### Generate coverage reports
309
+
310
+ After running the tests, you can use a tool like `nyc` to generate the coverage reports.
274
311
 
275
312
  ## Troubleshooting
276
313
 
314
+ ### Unhandled browser errors
315
+
277
316
  If you see an acceptance test failing without any apparent reason, it could be caused by an unhandled error in
278
317
  the browser. You can inspect it by adding a listener to the `pageerror` event:
279
318
 
@@ -283,3 +322,28 @@ page.on('pageerror', (err) => {
283
322
  process.emit('uncaughtException', err);
284
323
  });
285
324
  ```
325
+
326
+ ### Executing with --ui fails (Linux)
327
+
328
+ If your desktop environment uses Wayland, you may see the following error when running the tests with the
329
+ `--ui` flag:
330
+
331
+ ```
332
+ Error: Jest: Got error running globalSetup - /home/pladaria/bra/mistica-web/node_modules/jest-environment-puppeteer/setup.js, reason: ErrorEvent {
333
+ "error": [Error: socket hang up],
334
+ "message": "socket hang up",
335
+ ...
336
+ ```
337
+
338
+ To workaround this issue, you can install a newer Chrome in the repo where you are using the
339
+ `acceptance-testing` library:
340
+
341
+ - From the repo root: `npx @puppeteer/browsers install chrome@stable`
342
+ - Remove the chrome installed by puppeteer:
343
+ `rm -rf node_modules/puppeteer/.local-chromium/linux-901912/chrome-linux`
344
+ - Move downloaded chrome to the expected location:
345
+ `mv chrome/linux-<version>/chrome-linux64 node_modules/puppeteer/.local-chromium/linux-901912/chrome-linux`
346
+ - Cleanup. Remove chrome folder from the repo root: `rm -rf chrome`
347
+
348
+ Note that this browser will only be used when running the tests with the `--ui` flag. In headless mode, the
349
+ dockerized chromium will be used.