Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
graphql-mock
Advanced tools
This is a library that helps with the apollo graphql projects testing. Comparing to a vanilla apollo testing setup this enables the following:
npm add -D graphql-mock
thow this somewhere in your testing environment setup
import GraphQLMock from 'graphql-mock';
const schema = `
type Item {
id: ID!
name: String!
}
type Query {
items: [Item]
}
`;
// optional mocks
const mocks = {
... // your regular apollo mocks
};
// optional resolvers
const resolvers = {
... // graphql resolvers
};
export default new GraphQLMock(schema, mocks, resolvers);
If you already have an executable schema, you can pass it as is into the constructor:
export default new GraphQLMock(myExecutableSchema);
Lets assume you have a TodoList component that fires a graphql query to fetch the list from an API end point. Something like this:
const query = gql`
query GetItems {
items {
id
name
}
}
`;
const TodoList = () =>
<Query query={query}>
{({ data: { items = [] } = {}, error, loading }: any) =>
if (loading) { return <div>Loading...</div>; }
if (error) { return <div>{error.message}</div>; }
return (
<ul>
{items.map(item => <li key={item.id} >{item.name}</li>)}
</ul>
);
}
</Query>;
Here is how you can test it with enzyme and graphql-mock:
import { mount } from 'enzyme';
import { ApolloProvider } from 'react-apollo';
import graphqlMock from './graphql';
const render = () =>
<ApolloProvider client={graphqlMock.client}>
<TodoList />
</ApolloProvider>;
describe('TodoList', () => {
it('renders todo items good', () => {
graqhqlMock.expect(query).reply({ // <- query from the above code
items: [
{ id: '1', name: 'one' },
{ id: '2', name: 'two' }
]
});
expect(render().html()).toEqual('<ul><li>one</li><li>two</li></ul>');
});
it('renders loading state too', () => {
graphqlMock.expect(query).loading(true);
expect(render().html()).toEqual('<div>Loading...</div>');
});
it('renders errors when API fails', () => {
graphqlMock.expect(query).fails('everything is terrible');
expect(render().html()).toEqual('<div>everything is terrible</div>');
})
});
The query can be either a GraphQLQuery
object, or a string, or an applo style
{ query, variables }
object. NOTE: if you specify expected variables, the
mock will trigger to that specific query + variables combination only!
Now lets pretend you have a componet that creates new TODO items:
const mutation = gql`
mutation GetItems($name: String!) {
createItem(name: $name) {
id
name
}
}
`;
const CreatorComponent = () =>
<Mutation mutation={mutation} onError={noop}>
{(createItem, { data, loading, error }: any) => {
if (loading) { return <div>Loading...</div>; }
if (error) { return <div>{error.message}</div>; }
if (data) { return <div id={data.createItem.id}>{data.createItem.name}</div>; }
const onClick = () => createItem({ variables: { name: 'new item' } });
return <button onClick={onClick}>click me</button>;
}}
</Mutation>;
Now here how you can test this component through and through with graphql-mock:
const render = () =>
<ApolloProvider client={graphqlMock.client}>
<CreatorComponent />
</ApolloProvider>;
describe('CreatorComponent', () => {
it('renders good by default', () => {
expect(render().html()).toEqual('<button>click me</button>');
});
it('sends mutations and renders responses', () => {
graphqlMock.expect(mutation).reply({
createItem: { id: 1, name: 'new item' }
});
const wrapper = render();
wrapper.find('button').simulate('click');
expect(wrapper.html()).toEqual('<div id="id">new item</div>');
});
it('sends correct variables with the request', () => {
const mock = graphqlMock.expect(mutation).reply({
createItem: { id: 1, name: 'new item' }
});
const wrapper = render();
wrapper.find('button').simulate('click');
expect(mock.calls[0]).toEqual([{ name: 'new item' }]);
});
it('can take a failure and live another day', () => {
graphqlMock.expect(mutation).fail('everything is terrible');
const wrapper = render();
wrapper.find('button').simulate('click');
expect(wrapper.html()).toEqual('<div>everything is terrible</div>');
});
});
#client
-> a reference to the underline ApolloClient instance
#reset()
-> to reset all the mocks and queries history
#history
-> the log of all queries and mutations along with variables
#allowUnmockedRequests(state)
-> switch on/off an option to allow unmocked queries to fall through
All code in this library released under the terms of the MIT license
Copyright (C) 2018 Nikolay Nemshilov
FAQs
GraphQL endpoint mockery library for testing
The npm package graphql-mock receives a total of 105 weekly downloads. As such, graphql-mock popularity was classified as not popular.
We found that graphql-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.