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

@druidjs/testing

v0.5.4

Published

- [Testing Setup](#app-setup) - [Graphql Testing](#app-setup)

Downloads

3

Readme

druidjs/testing

App Setup

First, install Druid's testing utility:

npm install @testingjs/app

Then, the only requirement is that you have an app.js file in your source directory that exports your Druid app. Our testing utility will require this file to start up your app while testing.

For example:

// src/app.js
import Druid from '@druidjs/app'
import * as knex from 'knex'

const connection = knex(process.env.DATABASE)

const app = new Druid(connection)

export default app

// you would now start up your server in a different file, for example: 
// server.js 
import app from './src/app'

app.listen()

Graphql Testing

Druid makes it easy to test against your Graphql API while keeping your database clean. All you have to do is start our testing server and make sure to use its helper methods to cleanup after each test.

The examples below use the Jest testing framework, but you can use any testing library.

Here's an example of what that may look like:

import { createTestServer } from '@druidjs/testing'

let server

// Start the testing server
beforeAll(async () => { 
  server = await createTestServer()
})

// Rollback any database transactions after each test
afterEach(async () => { 
  await server.rollback() 
})

// Destroy the database connection
afterAll(async () => { 
  await server.destroy() 
})

Then in your tests, you can use server.request to test your graphql resolvers:

 it('can get all users user', async () => {
  const result = await server.request({
     query: `
      query User($username: String) {
        user(username: $username) {
          id
          username
        }
      }
    `,
    variables: {
      username: 'admin'
    }
  })
  expect(result.data.users).toEqual('admin')
})

If you need to test a resolver only intended for authenticated users, you can use our withAuthHeaders helper, which takes a userId and includes the appropriate headers into the response for you:

import { withAuthHeader } from '@druidjs/testing'

 it('can get auth user', async () => {
   const reqData = withAuthHeader(ADMIN_USER.id, {
    query: `
      query AuthUser {
        authUser {
          id
          username
        }
      }
    `
  })
  const result = await server.request(reqData)
  expect(result.data.authUsers).toBeTruthy()
})

The server.request function takes an object with the following properties:

  • headers: Object of response headers sent to client
  • query or mutation: String which contains the the Graphql request
  • variables: Object of variables to pass to the graphql request

The server itself has the following API:

  • app, the instance of the Apollo Server application.
  • db, the instance of database object that's passed to all resolvers (i.e. ctx.db).
  • request, helper Function to test graphql resolver. This method is intended for testing successfull graphql requests since, to make our life easier, it will log any errors contained in graqphql response (via result.data.errors).
  • requestFail, helper Function to test failure edge cases of graphql resolves. Unlike the regular request, this method will not log any failures to the console.
  • rollback, Function to rollback all database transactions.
  • destory, Function to destroy the database connection.