question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

improve generated Service test template

See original GitHub issue

@RobertBroersma @peterp

The current generated Service test is not actually testing the generated Service code (i.e. not even if it exists):

// api/src/services/posts/posts.test.js

/*
import { posts } from './posts'
*/

describe('posts', () => {
  it('returns true', () => {
    expect(true).toBe(true)
  })
})

Given the recent MSW integration with test DB, could we update this to at least be meaningful?

And we do have two cases to consider if we’d like to handle both distinctly:

  1. yarn rw g service post (no CRUD)
// api/src/services/posts/posts.js

import { db } from 'src/lib/db'

export const posts = () => {
  return db.post.findMany()
}

  1. yarn rw g service post --crud
// api/src/services/posts/posts.js

import { db } from 'src/lib/db'

export const posts = () => {
  return db.post.findMany()
}

export const post = ({ id }) => {
  return db.post.findOne({
    where: { id },
  })
}

export const createPost = ({ input }) => {
  return db.post.create({
    data: input,
  })
}

export const updatePost = ({ id, input }) => {
  return db.post.update({
    data: input,
    where: { id },
  })
}

export const deletePost = ({ id }) => {
  return db.post.delete({
    where: { id },
  })
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
cannikincommented, Nov 10, 2020

Worked on some stuff with @mojombo this afternoon and I’m recording this here for posterity… another possible way to define fixtures. Uses the target model as the root key in the object (comment) and then gives each fixture a name with the second level key:

export const FIXTURES = {
  comment: {
    rob: {
      name: 'Rob',
      body: 'This is my comment',
      post: {
        create: {
          title: 'First Post',
          body: 'Lorem ipsum',
        },
      },
    },
    tom: {
      name: 'Tom',
      body: 'Also a great comment',
      post: {
        create: {
          title: 'Second Post',
          body: 'Dolar sit amet',
        },
      },
    },
  },
}

This would allow us to:

  1. automate creating the fixtures because we can derive the model name from the object
  2. reference the fixtures by name in the tests
it('retrieves comments', async () => {
  const data = await comments()
  expect(data[0].name).toEqual(FIXTURES.comment.rob.name)
})

Maybe rather than referencing the FIXTURES directly we can create a fixtures function which will reference the fixture data after being added to the database so that we can access any fields that were created by defaults in the database:

it('retrieve a single comment`, async () => {
  const data = await comment({ id: fixtures().comment.rob.id })
  // id is not present the fixture but `fixtures()` returns the result of the record being added to the database
  expect(data.id).toEqual(fixtures().comment.rob.id)
  // same with createdAt, it's defaulted to "now" by prisma in the DB
  expect(data.createdAt).toEqual(fixtures().comment.rob.createdAt)
})

Oh snap, now we’re on to something…

0reactions
cannikincommented, Nov 10, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Test templates - Product Documentation | ServiceNow
The Test Templates module opens a list of available templates. From this module, you create, view, and edit test templates.
Read more >
How To Write Test Strategy Document (With Sample Test ...
Process to Develop a Good Test Strategy Document. Don't just follow the templates without understanding what works best for your project.
Read more >
Create a Web Service Test - Visual Studio (Windows)
In Visual Studio, create a new project using the ASP.NET Web Application (.NET Framework) template, and select the Empty template when prompted.
Read more >
Free, Easy Test Maker—Create Multiple Choice Tests, Fast
The easy test maker that engages your colleagues, pupils, or friends. Use this test maker to create a multiple choice test or online...
Read more >
How to Write Test Cases for Software: Examples & Tutorial
Writing excellent test cases is just one more way to enhance team ... However, the test case template would likely vary from company...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found