chapter-three / next-drupal

Next.js for Drupal has everything you need to build a next-generation front-end for your Drupal site: SSG, SSR, and ISR, Multi-site, Authentication, Webforms, Search API, I18n and Preview mode (works with JSON:API and GraphQL).
https://next-drupal.org
MIT License
639 stars 178 forks source link

local Jest testing fails #533

Open JohnAlbin opened 1 year ago

JohnAlbin commented 1 year ago

Jest tests for next-drupal fail when run locally.

Expected behavior

The TESTING.md documentation tells a developer how to run the tests and the local tests run successfully.

Steps to reproduce:

  1. git clone this repo.
  2. Run yarn install
  3. Running yarn test fails all tests with:
FAIL  packages/next-drupal/tests/crud.test.ts
  ● Test suite failed to run

    The 'baseUrl' param is required.

      125 |   constructor(baseUrl: BaseUrl, options: DrupalClientOptions = {}) {
      126 |     if (!baseUrl || typeof baseUrl !== "string") {
    > 127 |       throw new Error("The 'baseUrl' param is required.")
          |             ^
      128 |     }

 FAIL  packages/next-drupal/tests/client.test.ts
  ● Test suite failed to run

    The 'baseUrl' param is required.

      125 |   constructor(baseUrl: BaseUrl, options: DrupalClientOptions = {}) {
      126 |     if (!baseUrl || typeof baseUrl !== "string") {
    > 127 |       throw new Error("The 'baseUrl' param is required.")
          |             ^
      128 |     }

Additional context

It's obvious from the Jest failure messages that there are some missing environment variables. It's also clear from looking at the Jest snapshot file, that the Jest tests are being run against a specific live environment, https://tests.next-drupal.org, and are using specific authentication credentials. So we can't allow developers to have those environment variables.

Developers should be able to have local test fixtures to run tests locally.

JohnAlbin commented 1 year ago

The current tests.nextdrupal.org instance is running Drupal 9.4.x and 99% of the test content is just the Umami demo content. That's good news since it means it might be possible to generate a local test instance (after we figure out what the 1% non-Umami content is). And it means we'll be able to update it to Drupal 10.1 easily too.

One complication is the jest snapshots are generated from the JSON:API data and are polluted with the absolute URL of the test environment and with content UUIDs from that specific instance of the Umami demo.

Alternatively, we could mock a Drupal JSON:API instance and have the Jest tests run against that. Though we'd want to generate the mocks using a live Drupal instance (and then commit the mocks to git) so it might make sense to do both here.

backlineint commented 4 months ago

@JohnAlbin have been poking at these tests a bit. Short term to run these tests against tests.nextdrupal.org the only suite that requires valid auth credentials seems to be NextDrupal/crud-methods.test.ts unsurprisingly. Having just that suite run with mocks would probably go a long way in the short term. In the Drupal API client we did something similar using MSW I'd be wiling to take a shot at that here if that would help.