Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testの準備 #11

Merged
merged 6 commits into from
Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2
jobs:
build:
dependencies:
docker:
- image: circleci/node:8.11
working_directory: ~/repo
Expand All @@ -17,9 +17,36 @@ jobs:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- persist_to_workspace:
root: .
paths: node_modules
build:
docker:
- image: circleci/node:8.11
working_directory: ~/repo
steps:
- checkout
- attach_workspace:
at: ~/repo
- run: yarn build
test:
docker:
- image: circleci/node:8.11
working_directory: ~/repo
steps:
- checkout
- attach_workspace:
at: ~/repo
- run: yarn test-ci
workflows:
version: 2
build:
jobs:
- build
- dependencies
- build:
requires:
- dependencies
- test:
requires:
- dependencies

1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"extends": [
"eslint:recommended",
"plugin:jest/recommended",
"airbnb-base"
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
lib/
coverage/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SyncPod-BFF
# SyncPod-BFF | [![CircleCI](https://circleci.com/gh/cyder/SyncPod-BFF.svg?style=svg)](https://circleci.com/gh/cyder/SyncPod-BFF) [![codecov](https://codecov.io/gh/cyder/SyncPod-BFF/branch/master/graph/badge.svg)](https://codecov.io/gh/cyder/SyncPod-BFF)

```
------------------ ----------------- -------------------
Expand Down
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
transform: {
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
'.*\\.(ts)$': '<rootDir>/node_modules/ts-jest',
},
preset: 'ts-jest',
testEnvironment: 'node',
};
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"start": "NODE_ENV=development nodemon --exec babel-node src/index.ts --extensions '.ts'",
"serve": "NODE_ENV=production node lib/index.js",
"build": "babel ./src --out-dir lib --extensions '.ts'",
"test": "jest",
"test-ci": "jest --coverage && codecov",
"tsc": "tsc -p . --noEmit",
"lint": "eslint -c ./.eslintrc.json 'src/**/*.ts'",
"lint-fix": "eslint --fix -c ./.eslintrc.json 'src/**/*.ts'"
},
"dependencies": {
"apollo-server": "^2.3.3",
"apollo-server-express": "^2.3.3",
"apollo-server": "^2.4.8",
"apollo-server-express": "^2.4.8",
"express": "^4.16.4",
"graphql": "^14.1.1",
"graphql-iso-date": "^3.6.1"
Expand All @@ -28,16 +30,23 @@
"@babel/preset-typescript": "^7.1.0",
"@types/graphql": "^14.0.5",
"@types/graphql-iso-date": "^3.3.1",
"@types/jest": "^24.0.11",
"@typescript-eslint/eslint-plugin": "^1.4.2",
"@typescript-eslint/parser": "^1.4.2",
"@typescript-eslint/typescript-estree": "^1.4.2",
"apollo-server-testing": "^2.4.8",
"babel-jest": "^24.5.0",
"codecov": "^3.2.0",
"eslint": "^5.15.1",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jest": "^22.4.1",
"eslint-plugin-prettier": "^3.0.1",
"jest": "^24.5.0",
"nodemon": "^1.18.9",
"prettier": "^1.16.4",
"ts-jest": "^24.0.0",
"typescript": "^3.2.4"
}
}
8 changes: 8 additions & 0 deletions src/__test__/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
rules: {
'import/no-extraneous-dependencies': ['error', {
devDependencies: true,
optionalDependencies: false,
}],
}
};
40 changes: 40 additions & 0 deletions src/__test__/__snapshots__/integration.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Mutations returns user 1`] = `
Object {
"data": Object {
"login": Object {
"accessToken": "69:q1aXhT-onz4sWejxcV1s",
"createdAt": "2018-03-31T07:35:05.000Z",
"email": "[email protected]",
"icon": null,
"id": 69,
"name": "MyString",
"selfIntroduction": null,
"updatedAt": "2018-03-31T07:35:05.000Z",
},
},
"errors": undefined,
"extensions": undefined,
"http": Object {
"headers": Headers {
Symbol(map): Object {},
},
},
}
`;

exports[`Queries returns pong 1`] = `
Object {
"data": Object {
"ping": "pong",
},
"errors": undefined,
"extensions": undefined,
"http": Object {
"headers": Headers {
Symbol(map): Object {},
},
},
}
`;
45 changes: 45 additions & 0 deletions src/__test__/integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { createTestClient } from 'apollo-server-testing';
import { gql } from 'apollo-server-express';

import server from '..';

const PING = gql`
query {
ping
}
`;

const LOGIN = gql`
mutation login($email: String!, $password: String!) {
login(email: $email, password: $password) {
id
icon
email
name
accessToken
createdAt
updatedAt
selfIntroduction
}
}
`;

describe('Queries', () => {
it('returns pong ', async () => {
const { query } = createTestClient(server);
const res = await query({ query: PING });
expect(res).toMatchSnapshot();
});
});

describe('Mutations', () => {
it('returns user', async () => {
const { mutate } = createTestClient(server);
const res = await mutate({
query: undefined,
mutation: LOGIN,
variables: { email: '[email protected]', password: 'password' },
} as any);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typescriptの型情報が間違ってるっぽいので、とりあえずanyで突っ込んでます。(良い解決策求む)
apollographql/apollo-server#2172

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ねえ、型って知ってる?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

別PRで対応

expect(res).toMatchSnapshot();
});
});
17 changes: 17 additions & 0 deletions src/__test__/resolvers/mutation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { resolvers } from '../..';

describe('[Query.ping]', () => {
it('returns user', async () => {
const res = await resolvers.Mutation.login();
expect(res).toEqual({
id: 69,
icon: null,
email: '[email protected]',
name: 'MyString',
accessToken: '69:q1aXhT-onz4sWejxcV1s',
createdAt: '2018-03-31T07:35:05.000Z',
updatedAt: '2018-03-31T07:35:05.000Z',
selfIntroduction: null,
});
});
});
8 changes: 8 additions & 0 deletions src/__test__/resolvers/query.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { resolvers } from '../..';

describe('[Query.ping]', () => {
it('returns pong', async () => {
const res = await resolvers.Query.ping();
expect(res).toEqual('pong');
});
});
17 changes: 13 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,16 @@ const server = new ApolloServer({

server.applyMiddleware({ app, cors: { credentials: true, origin: CLIENT_HOST } });

app.listen({ port: PORT }, () => {
// eslint-disable-next-line no-console
console.log(`🚀 Server ready at localhost:${PORT}${server.graphqlPath}`);
});
if (process.env.NODE_ENV !== 'test') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 fileを分けた時にこの分岐をなくす

app.listen({ port: PORT }, () => {
// eslint-disable-next-line no-console
console.log(`🚀 Server ready at localhost:${PORT}${server.graphqlPath}`);
});
}

export default server;

export {
typeDefs,
resolvers,
};
Loading