This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------ | |
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jest.config.js | |
module.exports = { | |
/** | |
* 1. | |
* Add some TypeScript specific Jest configuration (not needed if you use plain JS) | |
*/ | |
transform: { | |
'^.+\\.tsx?$': 'ts-jest', | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
OlderNewer