DEV Community

Discussion on: Testing NodeJs/Express API with Jest and Super test 🐧🐧

Collapse
 
aurelkurtula profile image
aurel kurtula

I've started playing with this and do tests run in the order I write them and can/should I rely on the actions of the previous one. For example I need to test whether I can add a post, and whether I can recieve a post, rather than doing what you did (at "Now, let's test the get single post"). Is it bad practice to write the something like this

const data = { id: "" };
describe("adding to the db", () => {
  test("add post", (done) => {
   supertest(app).post("/api/post")
   .end((response) => {
      data.id = payload.id;
      expect(response.body.title).toBe(post.title);
      expect(response.body.content).toBe(post.content);
    done();
    }); 

  });
  test("get one post", (done) => {
    request(app)
      .get(`/api/${data.id}`)
      .end((err, res) => {
        expect("something useful").toEqual("something useful");
        done();
      });
  });
});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mhmdlotfy96 profile image
muhammed Lotfy

Hey Aural
firstly, this is a a base article a bout how to use unit test.
and second one that what you did is just the post (or data) variable shared for creating and fetching steps not a big deal so far.
and last thing is to Mock your data "preparing step" then use it whatever you need
check this lib npmjs.com/package/given2

Collapse
 
aurelkurtula profile image
aurel kurtula

Thanks