hapijs / lab

Node test utility
Other
739 stars 176 forks source link

Problem with --coverage #999

Closed binarymist closed 3 years ago

binarymist commented 3 years ago

Support plan

Context

How can we help?

There seems to be a problem with --coverage, it's only happened after recently upgrading packages. The problem line seems to be here. The build referes to this line.

Details of failing build:

Executing "step_script" stage of the job script
$ npm install
> pre-commit@1.2.2 install /builds/purpleteam-labs/purpleteam/node_modules/pre-commit
> node install.js
> core-js@2.6.12 postinstall /builds/purpleteam-labs/purpleteam/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"
> spawn-sync@1.0.15 postinstall /builds/purpleteam-labs/purpleteam/node_modules/spawn-sync
> node postinstall
npm WARN purpleteam@0.1.0-alpha.1 No license field.
added 689 packages from 764 contributors and audited 691 packages in 15.234s
46 packages are looking for funding
  run `npm fund` for details
found 6 low severity vulnerabilities
  run `npm audit fix` to fix them, or `npm audit` for details
$ npm run test
> purpleteam@0.1.0-alpha.1 pretest /builds/purpleteam-labs/purpleteam
> npm run lint
> purpleteam@0.1.0-alpha.1 lint /builds/purpleteam-labs/purpleteam
> eslint .
> purpleteam@0.1.0-alpha.1 test /builds/purpleteam-labs/purpleteam
> lab --assert @hapi/code --coverage

  ..................../builds/purpleteam-labs/purpleteam/src/view/dashboard.js:131
  global.__$$labCov._line('/builds/purpleteam-labs/purpleteam/src/view/dashboard.js',131);if (global.__$$labCov._statement('/builds/purpleteam-labs/purpleteam/src/view/dashboard.js',23,131,logger.instance !== 'To be assigned')) {
                                                                                                                                                                                                    ^
TypeError: Cannot read property 'instance' of undefined
    at Object.handleTesterProgress (/builds/purpleteam-labs/purpleteam/src/view/dashboard.js:131:197)
    at handleModelTesterEvents (/builds/purpleteam-labs/purpleteam/src/presenter/apiDecoratingAdapter.js:425:205)
    at Model.<anonymous> (/builds/purpleteam-labs/purpleteam/src/presenter/apiDecoratingAdapter.js:437:302)
    at Model.emit (events.js:315:20)
    at Model.propagateTesterMessage (/builds/purpleteam-labs/purpleteam/src/models/model.js:41:97)
    at EventSource.<anonymous> (/builds/purpleteam-labs/purpleteam/src/presenter/apiDecoratingAdapter.js:270:121)
    at EventSource.emit (events.js:315:20)
    at _emit (/builds/purpleteam-labs/purpleteam/node_modules/eventsource/lib/eventsource.js:242:17)
    at onConnectionClosed (/builds/purpleteam-labs/purpleteam/node_modules/eventsource/lib/eventsource.js:52:5)
    at OverriddenClientRequest.<anonymous> (/builds/purpleteam-labs/purpleteam/node_modules/eventsource/lib/eventsource.js:231:7)
    at OverriddenClientRequest.emit (events.js:315:20)
    at Socket.<anonymous> (/builds/purpleteam-labs/purpleteam/node_modules/propagate/index.js:64:17)
    at Socket.emit (events.js:315:20)
    at /builds/purpleteam-labs/purpleteam/node_modules/nock/lib/socket.js:96:14
    at processTicksAndRejections (internal/process/task_queues.js:75:11)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! purpleteam@0.1.0-alpha.1 test: `lab --assert @hapi/code --coverage`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the purpleteam@0.1.0-alpha.1 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-27T07_22_04_564Z-debug.log
Cleaning up file based variables
ERROR: Job failed: exit code 1

The NPM script used to execute the failing test (coverage) is here. Now if I remove the --coverage the tests pass.

I've also tried the coverage advice even though it specifies it's valid for node 0.10. This didn't change the test result or seem to change the behaviour. Do I need to exclude something else? Is this a bug?

Thanks muchly!

cjihrig commented 3 years ago

Details of failing build here:

That link 404s for me.

binarymist commented 3 years ago

That link 404s for me.

Dam, sorry @cjihrig . I should have checked a non logged in browser. I've updated my original post with the relevant section of the build output..

Thanks for your patience.

geek commented 3 years ago

The test code has the following lines:

const logger = internals.infoOuts[testerType].loggers.find((l) => l.sessionId === sessionId);
if (logger.instance !== 'To be assigned') {

and the error output is:

TypeError: Cannot read property 'instance' of undefined

This seems to be expected because the code isn't checking if logger was found before trying to read a property on it. If find can't satisfy the testing function then it returns undefined.

binarymist commented 3 years ago

Thanks @geek

Turned out I had some async code still executing after a couple of tests.
In those tests I was restoring the module within dashboard.js in flags.onCleanup.
Because I restored the module, the logger no longer existed. So I was getting this weird error that was produced outside of any of the tests context which confused me.

Bit of a hunt to work it out, but got there in the end.

Thanks again.