CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

hooks

Open rebajz opened this issue 3 years ago • 2 comments

  1. I add hook to file codecept.conf.js as: exports.config = { hooks: ['./someHook.ts'],

  2. from https://codecept.io/hooks/#custom-hooks I copy your example

const event = require('codeceptjs').event;

module.exports = function() {

event.dispatcher.on(event.test.before, function (test) {

console.log('--- I am before test --');

}); }

to new file ./someHook.ts

after run codeceptjs i see this:

PS C:_git\sandbox> npm run run:tests

[email protected] run:tests codeceptjs run

CodeceptJS v3.1.3 Using test root "C:_git\sandbox" (node:3740) UnhandledPromiseRejectionWarning: Error: CodeceptJS 3 allows bootstrap/teardown hooks only as async functions. More info: https://bit.ly/codecept3Up at module.exports (C:_git\sandbox\node_modules\codeceptjs\lib\hooks.js:7:11) at C:_git\sandbox\node_modules\codeceptjs\lib\codecept.js:101:39 at Array.forEach () at Codecept.runHooks (C:_git\sandbox\node_modules\codeceptjs\lib\codecept.js:101:23) at Codecept.init (C:_git\sandbox\node_modules\codeceptjs\lib\codecept.js:57:10) at module.exports (C:_git\sandbox\node_modules\codeceptjs\lib\command\run.js:24:14) at Command. (C:_git\sandbox\node_modules\codeceptjs\bin\codecept.js:9:11) at Command.listener (C:_git\sandbox\node_modules\commander\index.js:315:8) at Command.emit (events.js:400:28) at Command.parseArgs (C:_git\sandbox\node_modules\commander\index.js:651:12) (Use node --trace-warnings ... to show where the warning was created) (node:3740) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To te rminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:3740) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Please upgrade your documentation page, with working example.

rebajz avatar Oct 12 '21 00:10 rebajz

As a workaround, we got the hooks to work by importing the modules explicitly, as the hooks array are expecting functions not module references.

i.e. in your codecept.js config file:

const myHook = require('pathToMyHook/myHook.js');
.....
exports.config = {
   ....
   hooks: [myHook],
}

MumblesNZ avatar Mar 14 '22 00:03 MumblesNZ

Confirmed with CodeceptJS 3.3.0 when setting https://github.com/codeceptjs/CodeceptJS/issues/661#issuecomment-602970615 @MumblesNZ thanks for workaround.

mirao avatar Apr 02 '22 17:04 mirao