redwood: [Bug?]: Scenarios broken with redwood 7

What’s not working?

We have some tests that fail after the update to redwood 7.

It seems, like “sub objects” in scenarios only get created some times and not every time as expected.

How do we reproduce the bug?

What’s your environment? (If it applies)

System:
    OS: Linux 6.7 Fedora Linux 39 (Workstation Edition)
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 20.10.0 - /tmp/xfs-e5f62a34/node
    Yarn: 4.1.0 - /tmp/xfs-e5f62a34/yarn
  Databases:
    SQLite: 3.42.0 - /usr/bin/sqlite3
  npmPackages:
    @redwoodjs/core: 7.0.3 => 7.0.3 
    @redwoodjs/project-config: 7.0.3 => 7.0.3

Are you interested in working on this?

  • I’m interested in working on this

About this issue

  • Original URL
  • State: closed
  • Created 4 months ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

@dac09 Unfortunately, I don’t have a ton of time to work on this now as I’m dealing with a family health crisis, but I noticed the following.

The issue appears to start when there are uncaught exceptions in the test code. This first block of code causes the following tests to fail on a scenario setup.


    scenario(
      'attempts to query a single issue not in the DB',
      async (scenario: StandardScenario) => {
        mockCurrentUser(castCurrentUser(scenario.user.userAdminOrgA))
          // The following code is throwing an uncaught exception right now
          const result = await issue({ id: -1 })
          expect(result).toEqual(null)
      }
    )

This second block of code does not cause the following tests to fail.

    scenario(
      'attempts to query a single issue not in the DB',
      async (scenario: StandardScenario) => {
        mockCurrentUser(castCurrentUser(scenario.user.userAdminOrgA))
        try {
          // The following code is throwing an uncaught exception right now
          const result = await issue({ id: -1 })
          expect(result).toEqual(null)
        } catch (e) {
        }
      }
    )

I haven’t had the chance to test it yet, but do we need a try... catch in the following function of jest.setup.js? The await teardown() used to be in an afterEach() function, but was moved into this test wrapper. We need to make sure that the teardown always happens.

const buildScenario =
  (itFunc, testPath) =>
  (...args) => {
    let scenarioName, testName, testFunc

    if (args.length === 3) {
      ;[scenarioName, testName, testFunc] = args
    } else if (args.length === 2) {
      scenarioName = DEFAULT_SCENARIO
      ;[testName, testFunc] = args
    } else {
      throw new Error('scenario() requires 2 or 3 arguments')
    }

    return itFunc(testName, async () => {
      let { scenario } = loadScenarios(testPath, scenarioName)

      const scenarioData = await seedScenario(scenario)
      const result = await testFunc(scenarioData)

      if (wasDbUsed()) {
        await teardown()
      }

      return result
    })
  }

HTH!

I also noticed that if any test fails, the data in the database is not deleted and recreated, but only an attempt is made to create it again. This leads to a failure if your entity has a unique field like a name column. See my case

defineScenario({
  organization: {
    one: {
      data: {
        name: 'abc',
      },
    }
  }
})

When a single test fails, subsequent tests will also fail with this error:


    PrismaClientKnownRequestError: 
    Invalid `getProjectDb()[model].create()` invocation in
    /home/federico/Documents/projects/platform/node_modules/@redwoodjs/testing/config/jest/api/jest.setup.js:197:64

      194     createArgs(scenarios)
      195   )
      196 } else {
    → 197   scenarios[model][name] = await getProjectDb()[model].create(
    Unique constraint failed on the fields: (`name`)

      at ai.handleRequestError (node_modules/@prisma/client/runtime/library.js:126:6775)
      at ai.handleAndLogRequestError (node_modules/@prisma/client/runtime/library.js:126:6109)
      at ai.request (node_modules/@prisma/client/runtime/library.js:126:5817)
      at l (node_modules/@prisma/client/runtime/library.js:131:9709)
      at seedScenario (node_modules/@redwoodjs/testing/config/jest/api/jest.setup.js:197:36)
      at Object.<anonymous> (node_modules/@redwoodjs/testing/config/jest/api/jest.setup.js:106:28)

This, as I said, is because name is a unique SQL column.

Is anyone experiencing the same thing?

@razzeee Does your experience match mine, that failing tests initiate this, or is your experience different?

No, our tests were running just fine on v6 (or still are, as we haven’t updated due to this yet) But they fail with v7