Testing your API with Wallaby

Wallaby.js is a popular developer productivity tool that runs your JavaScript and TypeScript tests immediately as you type, highlighting results in your IDE right next to your code.

Wallaby, after installing and configuring per its setup instructions, works quickly with the RedwoodJS web side to help run tests for pages, components, and layouts.

However, to use Wallaby on the api side to help test services, directives, functions, webhooks or custom libraries, a little extra configuration is needed.

To enable Wallaby.js in the RedwoodJS api side, you need to update your applications’s api/jest.config.js.

You will want to add:

// For Wallaby Support 
if (process.env.DEBUG?.includes('wallaby')) config.runner = 'jest-runner'

Here is a full example api/jest.config.js file:

// api/jest.config.js 
// More info at https://redwoodjs.com/docs/project-configuration-dev-test-build const config = { rootDir: '../', preset: '@redwoodjs/testing/config/jest/api', }
 // For Wallaby Support 
if (process.env.DEBUG?.includes('wallaby')) config.runner = 'jest-runner' module.exports = config
3 Likes

Hi,
there seem to be some issues with using wallaby and redwood.
I am using prisma generators like prisma-erm-generator. It seems wallaby cannot use it properly. Error: spawn prisma-dbml-generator ENOENT

I created a repository for reproduction: GitHub - dennemark/redwood-wallaby: redwood wallaby test project
There is a small description in the beginning of the readme.

The wallaby team tried to support me on it, but so far I wasn´t lucky to get it running. They also pointed out some inconsistencies on the redwood site, so I think it is worth investigating for the redwood team:

Here is the quote of their reply:

There were a couple of separate issues affecting your project:

  1. Redwood spawns processes and relies on npm behaviour that sets process.env.PATH variable to include your local ./node_modules/.bin folder. Wallaby was not previously doing this. We’ve updated Wallaby to have the same behaviour but this issue could have been addressed with a configuration file.
  2. Redwood has some unusual jest configuration where multiple projects reference the same rootDir explicitly. This caused Wallaby to incorrectly use your api environment variables for web tests.
  3. Redwood has a memory leak for API tests that means that Wallaby’s process reuse fails and so a manual configuration file is required to work around this issue ([Bug]: `@redwoodjs/testing/config/jest/api` causes a memory leak on api side tests · Issue #6322 · redwoodjs/redwood · GitHub)
    To fix your problem, please update to Wallaby core v1.0.1381 and use a configuration file (in your project root):
    wallaby.js
module.exports = ({
  autoDetect: true,

  // The following is required to work around memory leak in redwood API tests:
  // https://github.com/redwoodjs/redwood/issues/6322
  workers: {
    restart: true,

    // This limits concurrency to a single thread; may also be required but
    // haven't tested interactions between your tests and test files at scale
    initial: 1,
    regular: 1,
  },

  // This also helps address the memory leak issue and reduces the time Wallaby
  // waits for its worker processes to finish
  globalSetupTeardownTimeout: 100,
});

Please be sure to select to use your configuration file (instructions in our docs, method varies depending on your editor) so that Wallaby uses these settings instead of automatic configuration. Wallaby.js Introduction: Configuration

Thanks for this! I have to check in a recent app . But this his how I used to use Wallaby.

@danny and @Tobbe see the above post for some info on the api test memory leak that may be helpful.