Testing scenarios with relations

I have the following schema

model Tenant {                                                                                                           
   id        Int                @id @default(autoincrement())                                                             
   name      String                                                                                                                                                                              
   users     User[]                                                                                                       
   createdAt DateTime           @default(now())                                                                           
   updatedAt DateTime           @updatedAt                                                                                
 }                                                                                                                        
                                                                                                                          
 model User {                                                                                                             
   id                  Int       @id @default(autoincrement())                                                            
   tenants             Tenant[]                                                                                                
   email               String    @unique                                                                                                                                                         
   hashedPassword      String                                                                                             
   salt                String                                                                                                                                                                                
   createdAt           DateTime  @default(now())                                                                          
   updatedAt           DateTime  @updatedAt                                                                               
}                                       

Where a user can have 1 or more tenants and a tenant can have 1 or more users. This is working fine so far, up until my test started failing. I updated my user and tenant querys to start returning the relations like so:

export const user = async ({ id }) => {                                                                                
    const oneUser = await db.user.findUnique({                                                                           
      where: { id },                                                                                                     
    });                                                                                                                  
    const tenants = await db.user                                                                                        
      .findUnique({                                                                                                      
        where: { id },                                                                                                   
      })                                                                                                                 
      .tenants();                                                                                                        
                                                                                                                        
    return {                                                                                                             
      ...oneUser,                                                                                                        
      tenants,                                                                                                           
    };                                                                                                                   
 };

I have a scenerio setup like this

user: {                                                                                                                      
    one: {                                                                                                                     
      data: {                                                                                                                  
        email: 'test-user-1@example.com',                                                                                      
        hashedPassword: 'my-super-top-secret-passw0rd',                                                                        
        salt: '02EDGWDWOKJH842NZZ',                                                                                            
        updatedAt: new Date('2022-06-18T19:08:06Z'),                                                                           
        tenants: {                                                                                                             
          create: [                                                                                                            
            {                                                                                                                  
              name: 'test-tenant-1',                                                                                                                                                                               
              updatedAt: new Date('2022-06-18T19:08:06Z'),                                                                     
            },                                                                                                                 
          ],                                                                                                    
        },                                                                                                                     
      },                                                                                                                       
    },                                                                                                                         
  },

Here is my test

 scenario('returns a single user', async (scenario) => {                                                                      
    const result = await user({ id: scenario.user.one.id });                                                                   
                                                                                                                               
    expect(result).toEqual(scenario.user.one);                                                                                 
  });                                                                                                                          

result contains a tenant but scenario does not. Any idea why or how to work-around this? I get this error

 ● users › returns a single user

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

    - Expected  - 0
    + Received  + 1

    @@ -5,8 +5,9 @@
        "id": 14,
        "salt": "02EDGWDWOKJH842NZZ",
    +   "tenants": Array [],
        "updatedAt": 2022-06-18T19:08:06.000Z,
        "verification": "test-verification-c0de",
      }

      25 |     const result = await user({ id: scenario.user.one.id });
      26 |
    > 27 |     expect(result).toEqual(scenario.user.one);
         |                    ^
      28 |   });
      29 |
      30 |   scenario('creates a user', async () => {

      at api/src/services/users/users.test.js:27:20
      at Object.<anonymous> (node_modules/@redwoodjs/testing/config/jest/api/jest.setup.js:163:16)

You need to include the tenant relationship in your scenario. When you include it will be available in the scenario. Testing | RedwoodJS Docs

From the docs.

You can also include the post object (or select specific fields from it):

export const standard = defineScenario({
  comment: {
    first: {
      data: {
        name: 'Rob',
        body: 'Something really interesting'
        post: {
          create: {
            title: 'Brand new post',
            body: 'Introducing dbAuth'
          }
        }
      },
      include: {
        post: true
      }
    }
  }
})

Ah, I saw that include and didn’t understand it. That solved my problem, thank you very much. Here is the fix.

export const standard = defineScenario({                                                                                       
  user: {                                                                                                                      
    one: {                                                                                                                     
      data: {                                                                                                                  
        email: 'test-user-1@example.com',                                                                                      
        hashedPassword: 'my-super-top-secret-passw0rd',                                                                        
        salt: '02EDGWDWOKJH842NZZ',                                                                                            
        verification: 'test-verification-c0de',                                                                                
        isVerified: false,                                                                                                     
        updatedAt: new Date('2022-06-18T19:08:06Z'),                                                                           
        tenants: {                                                                                                             
          create: [],                                                                                                          
        },                                                                                                                     
      },                                                                                                                       
      include: {                                                                                                               
        tenants: true,                                                                                                         
      },                                                                                                                       
    },                                                                                                                         
  },                                                                                                                           
});