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

@dellstorage/mock-server

v1.1.2

Published

Mock server is used to response on queries with mocked real schema that was received from the real graph backend server.

Downloads

17

Readme

Mock server

Mock server is used to response on queries with mocked real schema that was received from the real graph backend server.

Usage

| Command | Description | | :---------------- | :--------------------------------------------------------------------------------------------------------------------- | | --update | will update the schema from the real graphql backend. Server should have the next format http://localhost:8080/graphql |

Without flags will run the mocked server.

Under mocks folder mock files can be found. Each file represents a field of the graphql schema. The value of them should be a function the returns data(object/array/fields with functions).

schema.graphql - should be commited, it provides opportunities to see backend changes and run the mock server at CI without running the real graphql server.

Contribution

It has faker library. If you need to generate some kind of data then don't forget to use faker.seek to preset random. It is required when you cover some tests for UI it will allow you to get the same values between different launches.

Also be aware you are able to generate amount of data instead of write it manually to test UI with more data.

Arguments such as first, count and so on are passed as the sencond arguments and can be handled to prepare results.

For example:

const faker = require("faker");

// Generate data
// See that faker is used to fill data
const edges = new Array(50).fill(0).map(() => ({
    node: () => ({
        symptomID: () => faker.random.number(),
        type: () => faker.random.arrayElement(["NormalEvent", "WarningEvent"]),
        message: () => faker.lorem.words(),
        createdOn: () => faker.date.past(10, new Date(2020, 0, 1)).toISOString(),
    }),
}));

module.exports = (_, {first}) => {
    // <- accept first argument
    faker.seed(1); // <- here we set seed(1) to have the same random results between queries
    return {
        edges: () => edges.slice(0, first), // use first to get first results
    };
};

To add mocks for a mutation you need to place your resolvers to src/mocks/mutations.js.

For example:

module.exports = () => ({
    acknowledgeEvent: () => ({
        description: "my-description",
    }),
});

This mock will be used as RootMutation field in the schema. To see all available mocks you need to search RootMutation in src/schema.graphql or in Playground.

State of server

To be able to share data between mutations and queries you have to use a storage. The storage is passed through whole schema from the main executable function.

Example:

module.exports = store => ({
    RootMutation: RootMutation(store),
    Viewer: Viewer(store),
    String: () => "Apollo-mock",
});

To use that you have to keep the store in a clojure function, fill the store and use that one to modify and return the data. The idea behind the store is to have separated data for different types. If you use managerConfig query then use data at { managerConfig: [data] }

For example of using in a mutation and a query:

// A mutation
// Be aware we keep the store in clojure.
module.exports = store => () => ({
    createEventRule: (_, {input}) => {
        const {eventrule} = input;
        // Here we update the store to save the incoming data.
        store.update({
            managerConfig: {
                eventRules: [...store.getStore().managerConfig.eventRules, eventrule],
            },
        });
        return eventrule;
    },
});

// A query
// Pay attention we accept the store in query mock function.
module.exports = store => {
    faker.seed(1);

    // this function will be invoked when the schema will be initialized
    // and this store also is already shared with the mutation.
    // Just fill the structure with preset data.
    store.update({
        managerConfig: {
            version: faker.system.semver(),
            eventRules: [], //...,
            defaultEventRules: [], ...,
        },
    });
    // Return a function that will return the managerConfig from the store.
    return () => store.getStore().managerConfig;
};

See more examples at

  • Mutation: src/mocks/mutations.js
  • Query: src/mocks/managerConfig.js

Packaging mock-server

$ # to create tarball that can be referenced as dependency from other project
$ # move to directory where Makefile exists and run
$ make pack
$ # you can reference in other project like: "mock-server": "http://my_package_server/packages/mock-server-0.17.0.tgz"
$ # you can also package mock server as docker image
$ make image
$ # run docker image with
$ sudo docker run -d --rm --name mock-server harbor.lss.emc.com/ecs/mock-server:latest
$ # note: using --name flag allows container to be linked to vsphere plugin if you desire

Example of usage

$ # install utils package
$ yarn install ecs-flex-ui-utils # if this package is not registered then use <path to ecs-flex-ui-utils>/ecs-flex-ui-utils
$ # if you want to install just mock-server without additional packages
$ # yarn install ../ecs-flex-ui-utils/packages/mock-server
$ # run the mock-server
$ yarn mock-server
$ # run your tests, you have the running server like the real backend
$ yarn test

For development purpose you also are free to use yarn link.

$ # go to the mock-server package
$ yarn link
$ # go to the needed project
$ yarn link mock-server
$ yarn mock-server
$ # modify mock-server package and get the changes without reinstalling package
$ # don't forget to unlink the mock-server package when you don't need it
$ yarn unlink mock-server
$ # reinstall dependencies after linking
$ yarn install --force
$ # go to the mock-server package
$ yarn unlink