Skip to content

HstarStudio/jest-api-test

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jest-api-test

mocking api test with jest

mock data

// user.data.js
export default {
  "GET 200": {
    code: 0,
    msg: "ok",
    data: {
      username: "Kodo",
      age: 18
    }
  },
  "POST 200": {
    code: 0,
    msg: "xxx"
  },
  "GET 400": {
    msg: "invald params",
    code: -1
  },
  "GET 401": {}
};

Test

// user.test.js
describe("user api test", () => {
  const url = "xxx.yyy.com/user";
  it("user GET should be 200", async () => {
    const result = await http({
      url,
      method: "get",
      statusCode: 200
    });
    expect(result.data.username).toBe("Kodo");
  });

  it("user POST should be 200", async () => {
    const result = await http({
      url,
      method: "post",
      statusCode: 200
    });
    expect(result.code).toBe(0);
  });

  it("user GET should be 200", async () => {
    const result = await http({
      url,
      method: "get",
      statusCode: 400
    });
    expect(result.msg).toBe("invald params");
  });
});

About

mocking api test with jest

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%