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

simple-ql-testing

v1.0.5

Published

Testing library for simple-ql applications

Downloads

4

Readme

Simple Ql Testing

This library helps writing tests for Simple QL applications

Overview

You can test your application with this code:

const Testing = require('simple-ql-testing')
const test = Testing(url) //url can be "http://localhost:3000/" for instance
function createTest (positive, name, query, expected) {
  return { positive, name, query, expected }
}
test([
  createTest(true, 'good request', { User: { login: 'login', password: 'password', create: true }}),
  createTest(false, 'bad request', 'dummy content'),
]).catch(err => {
  console.error(err.message)
}).then(process.exit)

Getting started

Create the test function

The first thing you will need is the test fuction. For this, you need to import the Testing function and provide it with the url of the application you wish to test.

const Testing = require('simple-ql-testing')
const test = Testing(url) //url can be "http://localhost:3000/" for instance

You need to know the following things about the url:

  • Simple QL centralizes all the requests on a single url, so there is usually no need to provide different urls. But if you really need to, you can instanciate a new test function.
  • You can omit the origin part of the url and keep only the path. In this case, the default will be http://localhost:80. You still need to provide the path, though.

A Test

The test function takes a single Test or an array of Tests as single argument.

A Test is an object containing the following properties:

  • name {string} The name that will identify the test.
  • query {object} The simple-ql request
  • positive {boolean} (optionnal) Tell if the request should succeed (status<400) or fail (status>400).Defaults: true.
  • expected {any} (optionnal) If provided, the request result will match the server response to the object provided.

You can use the function Testing.createTest to make writing tests easier:

const Testing = require('simple-ql-testing')
const createTest = Testing.createTest

This is actually just a shortcut for the following function:

function createTest (positive, name, query, expected) {
  return { positive, name, query, expected }
}

Run a test

To run a test, just run execute the test function.

test(createTest(false, 'bad request', 'dummy content'))

Note: The result of such a call is a Promise that resolves or fails with the request response.

Run a test suite

This is how you can run a test suite.

test([
  createTest(true, 'good request', { User: { login: 'login', password: 'password', create: true }}),
  createTest(false, 'bad request', 'dummy content'),
]).then(() => console.log('SUCCESS'), err => console.error('FAILED:', err.message))

Note: The tests will run sequentially, and in order. The test function returns a Promise resolved with the response of the last request if all the tests were successful, and rejected as soon as one test fails. In such a case, the next tests in the suite will not be run.

Changing headers

JWT Token

To set the jwt token used for making requests, use the Testing.setJWT function:

const Testing = require('simple-ql-testing')
const jwt = "<your jwt>"
Testing.setJWT(jwt)

Other headers

You can retrieve the axios object from the Testing object:

const Testing = require('simple-ql-testing')
const axios = Testing.axios

You can check there to see what you can and cannot do.