This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testing): adds Jest config and one small test
- Loading branch information
1 parent
54b00a8
commit 249bc44
Showing
9 changed files
with
818 additions
and
25 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"env": { | ||
"test": { | ||
"plugins": ["transform-es2015-modules-commonjs"] | ||
} | ||
} | ||
} |
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
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
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' | ||
}; |
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
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
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 | ||
} | ||
} | ||
`; | ||
|
||
const rootValue = {}; | ||
const result = await graphql(graphqlSchema, query, rootValue); | ||
const { data } = result; | ||
|
||
expect(data.users[0].name).toBe(user.name); | ||
}); |
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
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; |
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
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; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = async function() { | ||
await global.__MONGOD__.stop(); | ||
}; |
Oops, something went wrong.