redwood: Generated Test Cases Failing Due to Formatting of Compared Values

I get the following when running a test case on a model that has a field defined as a float. The value expected and the value received appear to be almost the same, but reporting back with a different number of decimal places.

 FAIL   api  api/src/services/sites/sites.test.ts (9.202 s)
  ● sites › creates a site

    expect(received).toEqual(expected) // deep equality

    Expected: 1498892.0256940164
    Received: 1498892.025694016

      35 |     expect(result.country).toEqual('String')
      36 |     expect(result.state).toEqual('String')
    > 37 |     expect(result.latitude).toEqual(1498892.0256940164)
         |                             ^
      38 |     expect(result.latitudeMin).toEqual(7235317.275571931)
      39 |     expect(result.latitudeSec).toEqual(3107701.7253994453)
      40 |     expect(result.longitude).toEqual(9815030.723403068)

      at src/services/sites/sites.test.ts:37:29
      at Object.<anonymous> (../node_modules/@redwoodjs/testing/config/jest/api/jest.setup.js:84:16)

model

model Site {
  id           Int    @id @default(autoincrement())
  name         String
  description  String
  country      String
  state        String
  latitude     Float
  latitudeMin  Float
  latitudeSec  Float
  longitude    Float
  longitudeMin Float
  longitudeSec Float

  events EventsOnSite[]
}

I experience a similar issue on Decimal fields. But in that case, its comparing a string to a decimal value.

 FAIL   api  api/src/services/roles/roles.test.ts (8.072 s)
  ● roles › creates a role
                                                                                                                                                                                                                         
    expect(received).toEqual(expected) // deep equality

    Expected: 7420440.088194787
    Received: "7420440.088194787"

      26 |     expect(result.name).toEqual('String')
      27 |     expect(result.description).toEqual('String')
    > 28 |     expect(result.expectedPay).toEqual(7420440.088194787)
         |                                ^
      29 |   })
      30 |
      31 |   scenario('updates a role', async (scenario: StandardScenario) => {

      at src/services/roles/roles.test.ts:28:32
          at runMicrotasks (<anonymous>)
      at Object.<anonymous> (../node_modules/@redwoodjs/testing/config/jest/api/jest.setup.js:84:16)

I haven’t made any edits to the generated files, which were created via yarn rw generate scaffold <model>

Windows 11 Redwood 0.37.4

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (15 by maintainers)

Most upvoted comments

redwood_info We can add the exceptions like this as it needed attention by developers and it will be easy to find.

re: DateTime

Primsa returns DateTime as ISO 8601-formatted strings. So, you can convert the date to ISO String in JavaScript:

const isoString = new Date("2021-10-15T19:40:33Z").toISOString() //  Output: '2021-10-15T19:40:33.000Z'

The given output is the same received from Prisma.

My first step would be to reproduce the issue on my end and will try to think of a solution for a better developer experience.

Looks like I get this problem on some DateTime fields too.

 FAIL   api  api/src/services/events/events.test.ts (8.034 s)
  ● events › creates a event
                                                                                                                                                                                                                         
    expect(received).toEqual(expected) // deep equality

    Expected: "2021-10-15T19:40:33Z"
    Received: 2021-10-15T19:40:33.000Z

      25 |
      26 |     expect(result.name).toEqual('String')
    > 27 |     expect(result.startAt).toEqual('2021-10-15T19:40:33Z')
         |                            ^
      28 |     expect(result.endAt).toEqual('2021-10-15T19:40:33Z')
      29 |   })
      30 |

      at src/services/events/events.test.ts:27:28
      at Object.<anonymous> (../node_modules/@redwoodjs/testing/config/jest/api/jest.setup.js:84:16)

model

model Event {
  id                 Int       @id @default(autoincrement())
  name               String
  openAt             DateTime?
  startAt            DateTime
  endAt              DateTime
  infilSuspendAt     DateTime?
  exfilSuspendAt     DateTime?
  poSuspendAt        DateTime?
  finalSuspendAt     DateTime?
  allowOverrideDates Boolean   @default(false)

  sites EventsOnSite[]
}