cap-js-community / event-queue

An event queue that enables secure multi-tenant enabled transactional processing of asynchronous events, featuring instant event processing with Redis Pub/Sub and load distribution across all application instances.
https://cap-js-community.github.io/event-queue/
Apache License 2.0
12 stars 1 forks source link

issues with jest testing and event-queue periodic events #142

Closed js1972 closed 6 months ago

js1972 commented 7 months ago

I use a periodic event in my CAP app as a way to trigger a regular background job. However, I'm now trying to add some CAP tests with jest and I'm getting errors such as:

Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "[eventQueue/initialize] - event queue initialized {
      registerAsEventProcessor: true,
      processEventsAfterPublish: true,
      multiTenancyEnabled: false,
      redisEnabled: false,
      runInterval: 300000
    }".

      at console.info (node_modules/@jest/console/build/CustomConsole.js:138:10)
      at Function.logger.info (node_modules/@sap/cds/lib/log/cds-log.js:97:59)
      at Object.initialize (node_modules/@cap-js-community/event-queue/src/initialize.js:86:10)
      at cds.<anonymous> (node_modules/@cap-js-community/event-queue/cds-plugin.js:9:5)

  ●  Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "[eventQueue/runner] - first event-queue run scheduled { firstRunScheduledFor: '2024-03-05T06:50:03.612Z' }".

      at console.info (node_modules/@jest/console/build/CustomConsole.js:138:10)
      at Function.logger.info (node_modules/@sap/cds/lib/log/cds-log.js:97:59)
      at _scheduleFunction (node_modules/@cap-js-community/event-queue/src/runner.js:53:10)

Any ideas on how to get the event-queue to play nice with jest? Is there a way to temporarily block events from occuring during test runs?

soccermax commented 7 months ago

Yes there is. And I'm about to add a section in the documentation about that. But in short, you can do the following: Set the following event-queue parameters for testing:

"registerAsEventProcessor": false,
"updatePeriodicEvents": false,
"isRunnerDeactivated": true,

For this, you can leverage the profile feature of cds to set these only during testing (cds section of your package.json)

    "eventQueue": {
      "configFilePath": "./srv/eventConfig.yml",
      [... more settings ...]
      "[test]": {
        "registerAsEventProcessor": false,
        "isRunnerDeactivated": true,
        "updatePeriodicEvents": false,
      },
    },
js1972 commented 6 months ago

Thanks again @soccermax. I have since found that the jest tests actually run properly - its just warnings that it is issuing - seems it doesn't like random log output after a test is executed. I'll test the above using cds profiles. If I setup a [test] profile - does it still see my [dev] profile dummy users? Probably not so I'd need to replicate that config..

soccermax commented 6 months ago

That depends on how you define the dummy users? cds profiles can be stacked this should not be a problem

js1972 commented 6 months ago

Have you got an example of how profiles can be stacked?. The capire documentation does not show this: https://cap.cloud.sap/docs/node.js/cds-env#profiles

js1972 commented 6 months ago

Ignore my previous comment. From some quick research is seem the capire document is not very clear on the topic of profiles. When you DO NOT specify the production profile it will merge in the development profile regardless of what other profile you may choose to run. i.e. if I run cds w --profile hybrid, the development profile is merged in with the hybrid profile. This seems strange to me but it does explain some odd behaviour I have seen where you run with hybrid and it still seems to see your mock users defined under development. ;-)

See this post David Kunz:

image

Which raises the question - what is special about a profile called test? If you use the test profile does it NOT merge in development... Weird.

soccermax commented 6 months ago

No, because it works like inheritance. As the test profile is more specific, it overrides the values from the development profile.

You can also check the output of the env based on the profile:

cds env eventQueue --profile test or cds env eventQueue --profile development