Skip to content

Instantly share code, notes, and snippets.

View mr-pascal's full-sized avatar

Pascal mr-pascal

View GitHub Profile
//------------------------------------------------
// index.js
//-----------------------------------------------
// Import method to call the external service
const {callExternalService} = require('./externalService');
// Method to test
const main = () => {
// We only call the external service and return its response
return callExternalService();
## Run all tests
npm run test
## Delete `dist` folder
npm run clean
## Trigger a clean build
npm run build
## Start the server using the output from `npm run build`
npm run start
## Directly start the server via the TypeScript files
npm run dev
// src/index.ts
import {gql} from 'mercurius-codegen';
const typeDefs = gql`
type Query {
""" Method to add two integers """
add(
" First integer "
x: Int,
" Second integer "
y: Int
// src/index.ts
const resolvers = {
Query: {
/**
* Simple resolver to add two numbers
* @param {object} _
* @param {number} x First number
* @param {number} y Second number
*/
add: async (_: unknown, {x, y}: { x: number, y: number }): Promise<number> => x + y,
// src/index.ts
import fastify, {FastifyInstance} from 'fastify';
import {Server, IncomingMessage, ServerResponse} from 'http';
/**
* Create instance of our Fastify server
*/
export const app: FastifyInstance<Server, IncomingMessage, ServerResponse> = fastify({logger: false});
// ---------------------------------------------------
import mercurius from 'mercurius';
import {makeExecutableSchema} from '@graphql-tools/schema';
// src/tests/integration.spec.ts
import * as request from 'supertest';
import {app} from '../index';
import {gql} from 'mercurius-codegen';
describe(`Integration`, () => {
// 1. Just some small helper to wrap GraphQL success responses in the 'data' property
const successResponse = (response: object): { data: object } => ({data: response});
beforeEach(async () => {
// 2. Wait for our server to become ready to respond to requests
// jest.config.js
module.exports = {
/**
* 1.
* Add some TypeScript specific Jest configuration (not needed if you use plain JS)
*/
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
// jest.setup.ts
import {MongoMemoryServer} from 'mongodb-memory-server';
import {Collection, Db, MongoClient} from 'mongodb';
import {PERSON_COLLECTION} from './src/constants';
let mongo: MongoMemoryServer;
let mongoClient: MongoClient;
let db: Db;
// jest-mongodb-config.js
/**
* Don't change the name of this file.
* The name is hardcoded inside the '@shelf/jest-mongodb' npm module!
*/
module.exports = {
/**
* Define some defaults to use for the in-memory MongoDB
*/
// src/dbhelper.spec.ts
import {DBHelper} from './dbhelper';
import {IPerson} from './models/person.interface';
/**
* Start our test file where we test our DBHelper class
*/
describe(`DBHelper`, () => {
/**
* 1.