npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wechat-mini-program-api-mock

v0.0.17

Published

微信小程序接口mock

Downloads

3

Readme

wechat-mini-program-api-mock

微信小程序接口 mock

Features

  • 用于在 jest 下模拟微信小程序 api 返回结果

Install

npm install --save-dev wechat-mini-program-api-mock

Example

import Mock from "wechat-mini-program-api-mock";

// 模拟wx.getStorage
describe("Test: getStorage", () => {
  it("success", () => {
    new Mock().mock("getStorage").success(true);
    wx.getStorage({
      success(res) {
        // res === true
      },
    });
  });

  it("success", () => {
    new Mock().mock("getStorage").fail(false);
    wx.getStorage({
      fail(err) {
        // err === false
      },
    });
  });

  it("throw", () => {
    new Mock().mock("getStorage").fail(() => {
      throw newError("test error");
    });

    expect(() => {
      wx.getStorage({
        fail(err) {
          // err === false
        },
      });
    }).toThrow();
  });
});

API

创建 Mock 实例

import Mock from "wechat-mini-program-api-mock";

const mockIns = new Mock();

实例 API

mock(apiName[,options])

apiName

Type: String

需要模拟的接口名称

Example:需要模拟wx.loginmockIns.mock("login")

options

Type: Object

| key | 类型 | 说明 | | ---------- | ------ | ---------------------------------------------- | | aliasName | String | 接口别名,用于标识 Mock 的唯一性 | | apiOptions | Object | Mock 接口的参数,如wx.setStorage(apiOptions) |

Example:需要模拟wx.setStorage

// Mock
mockIns.mock("setStorage", {
  apiOptions: {
    key: "demo",
    data: "demoValue",
  },
});

// 真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
});
  • 同一个接口 Mock 多次(mockIns.mock("setStorage")),会覆盖之前 Mock 的接口,为了避免这种请求,可以在 Mock 的时候传入aliasName参数做区分,aliasName也是需要保证全局唯一,不然也会覆盖之前 Mock 的接口。

传入aliasName参数:

mockIns.mock("setStorage", {
  aliasName: "setStorage_mock1",
  apiOptions: {
    key: "demo",
    data: "demoValue",
  },
});

success(res)

模拟success方法执行,用success模拟返回时,Mock 的接口多次调用会执行相同的逻辑。

res

Type: Object | Function

// Mock
mockIns
  .mock("setStorage", {
    apiOptions: {
      key: "demo",
      data: "demoValue",
    },
  })
  .success({
    status: "ok",
  });

// 第一次真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
  success(res) {
    // 这里的res就是上面Mock的数据{status: "ok"}
    res.status === "ok";
  },
});

// 第二次真实调用,res.status还是ok
wx.setStorage({
  key: "demo",
  data: "demoValue",
  success(res) {
    // 这里的res就是上面Mock的数据{status: "ok"}
    res.status === "ok";
  },
});

successOnce(res)

模拟success方法执行,用successOnce模拟返回时,res只会执行一次,之后再次执行会返回jest.Mock实例。

res

Type: Object | Function

// Mock
mockIns
  .mock("setStorage", {
    apiOptions: {
      key: "demo",
      data: "demoValue",
    },
  })
  .successOnce({
    status: "ok",
  });

// 第一次真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
  success(res) {
    // 这里的res就是上面Mock的数据{status: "ok"}
    res.status === "ok";
  },
});

// 第二次真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
  success(res) {
    res instanceof jest.Mock === true;
  },
});

实现多次调用返回不同结果

// Mock
mockIns
  .mock("setStorage", {
    apiOptions: {
      key: "demo",
      data: "demoValue",
    },
  })
  .successOnce({
    status: "ok",
  })
  .successOnce({
    status: "cancel",
  });

// 第一次真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
  success(res) {
    // 这里的res就是上面Mock的数据{status: "ok"}
    res.status === "ok";
  },
});

// 第二次真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
  success(res) {
    // 这里的res就是上面Mock的数据{status: "cancel"}
    res.status === "cancel";
  },
});

fail(err)

模拟fail方法执行,用fail模拟返回时,Mock 的接口多次调用会执行相同的逻辑。

err

Type: Object | Function

// Mock
mockIns
  .mock("setStorage", {
    apiOptions: {
      key: "demo",
      data: "demoValue",
    },
  })
  .fail({
    status: "fail",
  });

// 第一次真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
  fail(err) {
    // 这里的err就是上面Mock的数据{status: "fail"}
    err.status === "fail";
  },
});

// 第二次真实调用,err.status还是fail
wx.setStorage({
  key: "demo",
  data: "demoValue",
  fail(err) {
    // 这里的err就是上面Mock的数据{status: "fail"}
    err.status === "fail";
  },
});

failOnce(err)

模拟fail方法执行,用failOnce模拟返回时,err只会执行一次,之后再次执行会返回jest.Mock实例。

err

Type: Object | Function

// Mock
mockIns
  .mock("setStorage", {
    apiOptions: {
      key: "demo",
      data: "demoValue",
    },
  })
  .failOnce({
    status: "fail",
  });

// 第一次真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
  fail(err) {
    // 这里的err就是上面Mock的数据{status: "ok"}
    err.status === "ok";
  },
});

// 第二次真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
  fail(err) {
    err instanceof jest.Mock === true;
  },
});

实现多次调用返回不同结果

// Mock
mockIns
  .mock("setStorage", {
    apiOptions: {
      key: "demo",
      data: "demoValue",
    },
  })
  .failOnce({
    status: "ok",
  })
  .failOnce({
    status: "cancel",
  });

// 第一次真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
  fail(err) {
    // 这里的err就是上面Mock的数据{status: "ok"}
    err.status === "ok";
  },
});

// 第二次真实调用
wx.setStorage({
  key: "demo",
  data: "demoValue",
  fail(err) {
    // 这里的err就是上面Mock的数据{status: "cancel"}
    err.status === "cancel";
  },
});