Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Commit

Permalink
feat(testing): adds Jest config and one small test
Browse files Browse the repository at this point in the history
  • Loading branch information
ojongerius committed Apr 25, 2018
1 parent 54b00a8 commit 249bc44
Show file tree
Hide file tree
Showing 9 changed files with 818 additions and 25 deletions.
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ node_js:
- "lts/*"
cache:
yarn: true
directories:
- "node_modules"
- $HOME/.mongodb-binaries # For mongodb-memory-server

env:
global:
Expand All @@ -18,5 +21,6 @@ before_install:

script:
- yarn lint
- yarn test-ci # Running inband is recommended for CI see https://github.com/facebook/jest/issues/3765
- commitlint-travis
- 'if [ "${TRAVIS_PULL_REQUEST}" = "false" ] ; then ./scripts/deploy.sh ; fi'
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
globalSetup: './test/utils/setup.js',
globalTeardown: './test/utils/teardown.js',
testEnvironment: './test/utils/mongo-environment.js'
};
18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
"format": "prettier --write es5 './**/*.js' && yarn lint",
"lint": "eslint ./**/*.js --fix",
"prepare-production": "snyk protect",
"snyk-test": "snyk test",
"start":
"nodemon ./node_modules/serverless/bin/serverless offline start --skipCacheInvalidation",
"test": "snyk test"
"test": "jest --runInBand",
"test-ci": "node_modules/.bin/jest --runInBand"
},
"config": {
"commitizen": {
Expand Down Expand Up @@ -60,6 +62,7 @@
"@commitlint/cli": "^6.1.3",
"@commitlint/config-conventional": "^6.1.3",
"@commitlint/travis-cli": "^6.1.3",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"commitizen": "^2.9.6",
"cz-customizable": "^5.2.0",
"eslint": "^4.19.1",
Expand All @@ -68,14 +71,27 @@
"eslint-plugin-prefer-object-spread": "^1.2.1",
"eslint-plugin-react": "^7.7.0",
"husky": "^0.14.3",
"jest": "^22.4.3",
"jest-cli": "^22.4.3",
"lint-staged": "^7.0.4",
"mongodb-memory-server": "^1.7.3",
"nodemon": "^1.17.3",
"prettier": "^1.11.1",
"prettier-package-json": "^1.5.1",
"snyk": "^1.71.0",
"source-map-support": "^0.5.4"
},
"keywords": ["open-api"],
"jest": {
"testEnvironment": "node",
"testPathIgnorePatterns": ["/node_modules/", "./dist"],
"coverageReporters": ["lcov", "html"],
"coverageDirectory": "./coverage/",
"collectCoverage": true,
"moduleNameMapper": {
"^mongoose$": "<rootDir>/node_modules/mongoose"
}
},
"lint-staged": {
"*.{js,json,css}": ["prettier --write", "git add"],
"package.json": ["prettier-package-json --write", "git add"]
Expand Down
48 changes: 48 additions & 0 deletions test/user.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const mongoose = require('mongoose');
const { graphql } = require('graphql');
const { makeExecutableSchema } = require('graphql-tools');
import typeDefs from '../graphql/typeDefs';
import resolvers from '../graphql/resolvers';
const graphqlSchema = makeExecutableSchema({
typeDefs,
resolvers,
logger: console
});

const UserModel = require('../dataLayer/model/user.js');

let connection;
let db;

beforeAll(async () => {
connection = await mongoose.connect(global.__MONGO_URI__);
});

afterAll(async () => {
await mongoose.disconnect();
});

it('should return a user after one has been created', async () => {
const user = new UserModel({
name: 'user',
email: 'user@example.com'
});

const newUser = await user.save();

//language=GraphQL
const query = `
query {
users {
name
email
}
}
`;

const rootValue = {};
const result = await graphql(graphqlSchema, query, rootValue);
const { data } = result;

expect(data.users[0].name).toBe(user.name);
});
24 changes: 24 additions & 0 deletions test/utils/mongo-environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const NodeEnvironment = require('jest-environment-node');

class MongoEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}

async setup() {
this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString();
this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__;

await super.setup();
}

async teardown() {
await super.teardown();
}

runScript(script) {
return super.runScript(script);
}
}

module.exports = MongoEnvironment;
16 changes: 16 additions & 0 deletions test/utils/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const MongodbMemoryServer = require('mongodb-memory-server');

const MONGO_DB_NAME = 'jest';
const mongod = new MongodbMemoryServer.default({
instance: {
dbName: MONGO_DB_NAME
},
binary: {
version: '3.2.19'
}
});

module.exports = function() {
global.__MONGOD__ = mongod;
global.__MONGO_DB_NAME__ = MONGO_DB_NAME;
};
3 changes: 3 additions & 0 deletions test/utils/teardown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async function() {
await global.__MONGOD__.stop();
};
Loading

0 comments on commit 249bc44

Please sign in to comment.