-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(cli) Generated services use Date objects for DateTime types in the tests #6394
Conversation
packages/cli/src/commands/generate/service/__tests__/scenario.test.js
Outdated
Show resolved
Hide resolved
"Everything" is passing now! I think this PR will greatly improve the dev experience, as it will make the generated tests pass right off the bat when adding @Tobbe do the comments in Is there anything else I can do to move this forward? Thanks for looking into it |
I've played around with this a bit now, and I think we should only use To do that I only updated the template
|
Yes, thats so much cleaner @Tobbe! Thank you so much, This helped me understand an aspect of how the scenarios work that I hadn't grasped yet. Updated the PR to only have I also kept:
Added a validity test to the dates and updated the solution in the first PR comment, to reflect the new solution |
I'm sorry @Tobbe, my bad, I've fixed this behavior. Instead of using Using Invalid `db.notification.create()` invocation in
/Users/.../notifications.js:14:26
11 }
12
13 export const createNotification = ({ input }) => {
→ 14 return db.notification.create({
data: {
date: '2022-09-24T18:29:34.928Z',
title: 'String',
body: 'String',
bool: true,
intNumber: 1289246,
bigIntNumber: '448927n'
~~~~~~~~~
}
})
Argument bigIntNumber: Got invalid value '448927n' on prisma.createOneNotification. Provided String, expected BigInt. scenario('creates a notification', async () => {
const result = await createNotification({
input: {
date: '2022-09-24T18:29:34.928Z',
title: 'String',
body: 'String',
bool: true,
intNumber: 1289246,
// This needs to be converted into a BigInt. The stringifyValue function takes care of it
bigIntNumber: '448927n',
},
})
expect(result.date).toEqual(new Date('2022-09-24T18:29:34.928Z'))
expect(result.title).toEqual('String')
expect(result.body).toEqual('String')
expect(result.bool).toEqual(true)
expect(result.intNumber).toEqual(1289246)
expect(result.bigIntNumber).toEqual(448927n)
}) this is the schema: model Notification {
id Int @id @default(autoincrement())
date DateTime
title String
body String
bool Boolean
intNumber Int
bigIntNumber BigInt
createdAt DateTime @default(now())
} |
packages/cli/src/commands/generate/service/templates/test.ts.template
Outdated
Show resolved
Hide resolved
packages/cli/src/commands/generate/service/templates/test.ts.template
Outdated
Show resolved
Hide resolved
packages/cli/src/commands/generate/service/templates/test.ts.template
Outdated
Show resolved
Hide resolved
That's a great catch! Can you please make sure there are tests for this, so no one messes that up in the future 🙂 |
I tried multiple approaches but couldn't get it quite right, because what i need to test is the actual rendered test file. I guess the ideal would be to use a snapshot. I'll give it a go tomorrow. again, thanks for all the guidance @Tobbe |
packages/cli/src/commands/generate/service/templates/test.ts.template
Outdated
Show resolved
Hide resolved
packages/cli/src/commands/generate/service/templates/test.ts.template
Outdated
Show resolved
Hide resolved
Thanks for your patience on this one Esteban. I'll try to get to it later today. |
thanks for the help and guidance @Tobbe |
This PR fixes the issue #3493 and replaces #6330
Problem
The main problem being fixed is that the generated service tests with a
DateTime
type fail one the first test run because they are being treated as strings and compared to date objects. 🚨Read the detailed explanation in the original issue #3493 or a slightly briefer explanation down below
Solution
I modified as little as possible in the following files:
packages/cli/src/commands/generate/service/service.js
buildStringifiedScenario
function now has more readable code.scenarioFieldValue
andfieldsToUpdate
functions useDate
objects instead of string values.packages/cli/src/commands/generate/service/templates/test.ts.template
new Date(...)
declarations.packages/cli/src/commands/generate/service/__tests__/scenario.test.js
Date
instance.✨ All the tests now pass right out of the box ✨
The generated test files now look like this:
notifications.test.js
Detailed problem explanation
Read the detailed explanation
when you add a model such as this:
Generate an
sdl
Run the tests
yarn rw test
😫 the tests fail 😫
They fail because the generated test compare string values to date objects.
The generated files look like this (I added the comments):
notifications.test.js
Edits
new Date(...)
declarations in the assertions.