Security News
CISA Brings KEV Data to GitHub
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
@druidjs/testing
Advanced tools
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()
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 clientquery
or mutation
: String which contains the the Graphql requestvariables
: Object of variables to pass to the graphql requestThe 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.FAQs
- [Testing Setup](#app-setup) - [Graphql Testing](#app-setup)
We found that @druidjs/testing demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.