Skip to content

Commit ed62aba

Browse files
committed
test: migrate from FlowType to TypeScript
1 parent 3bee74d commit ed62aba

File tree

85 files changed

+556
-679
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+556
-679
lines changed

jest.config.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
module.exports = {
2-
roots: ['<rootDir>/src'],
3-
testMatch: ['**/__tests__/**/*-test.(ts|js)'],
2+
preset: 'ts-jest',
43
testEnvironment: 'node',
4+
globals: {
5+
'ts-jest': {
6+
tsConfig: '<rootDir>/tsconfig.json',
7+
isolatedModules: true,
8+
diagnostics: false,
9+
},
10+
},
11+
moduleFileExtensions: ['ts', 'js'],
12+
transform: {
13+
'^.+\\.ts$': 'ts-jest',
14+
'^.+\\.js$': 'babel-jest',
15+
},
16+
roots: ['<rootDir>/src'],
17+
testPathIgnorePatterns: ['/node_modules/', '/lib/'],
518
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@babel/preset-env": "7.11.0",
4949
"@babel/preset-flow": "7.10.4",
5050
"@types/graphql": "^14.5.0",
51+
"@types/jest": "26.0.9",
5152
"@types/mongoose": "5.7.36",
5253
"babel-core": "^7.0.0-bridge.0",
5354
"babel-eslint": "10.1.0",
@@ -70,6 +71,7 @@
7071
"request": "2.88.2",
7172
"rimraf": "3.0.2",
7273
"semantic-release": "17.1.1",
74+
"ts-jest": "26.1.4",
7375
"tslint": "6.1.3",
7476
"tslint-config-prettier": "^1.18.0",
7577
"tslint-plugin-prettier": "2.3.0",

src/__mocks__/contactsSchema.js renamed to src/__mocks__/contactsSchema.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* @flow */
2-
31
import type { Schema as SchemaType } from 'mongoose';
42
import { Schema } from './mongooseCommon';
53

src/__mocks__/enumEmployment.js renamed to src/__mocks__/enumEmployment.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* @flow */
2-
31
const enumEmployment = {
42
full: { description: 'Full time' },
53
partial: { description: 'Partial time' },

src/__mocks__/languageSchema.js renamed to src/__mocks__/languageSchema.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* @flow */
2-
31
import type { Schema as SchemaType } from 'mongoose';
42
import { schemaComposer } from 'graphql-compose';
53
import { Schema } from './mongooseCommon';

src/__mocks__/mongooseCommon.js renamed to src/__mocks__/mongooseCommon.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/* @flow */
21
/* eslint-disable no-param-reassign, no-console */
32

43
import mongoose from 'mongoose';
5-
import MongodbMemoryServer from 'mongodb-memory-server';
4+
import { MongoMemoryServer } from 'mongodb-memory-server';
65

76
const { Schema, Types } = mongoose;
87

@@ -11,8 +10,8 @@ mongoose.Promise = Promise;
1110
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
1211

1312
const originalConnect = mongoose.connect;
14-
mongoose.connect = (async () => {
15-
const mongoServer = new MongodbMemoryServer();
13+
mongoose.createConnection = (async () => {
14+
const mongoServer = new MongoMemoryServer();
1615

1716
const mongoUri = await mongoServer.getConnectionString(true);
1817

@@ -35,6 +34,8 @@ mongoose.connect = (async () => {
3534
// console.log('MongoDB disconnected!');
3635
mongoServer.stop();
3736
});
38-
}: any);
37+
}) as any;
38+
39+
mongoose.connect = mongoose.createConnection as any;
3940

4041
export { mongoose, Schema, Types };

src/__mocks__/postModel.js renamed to src/__mocks__/postModel.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/* @flow */
2-
3-
import type { Schema as SchemaType } from 'mongoose';
1+
import type { Schema as SchemaType, Document } from 'mongoose';
42
import { mongoose, Schema } from './mongooseCommon';
53

64
const PostSchema: SchemaType<any> = new Schema({
@@ -16,6 +14,10 @@ const PostSchema: SchemaType<any> = new Schema({
1614
// updatedAt, created via option `timastamp: true` (see bottom)
1715
});
1816

19-
const PostModel = mongoose.model('Post', PostSchema);
17+
export interface IPost extends Document {
18+
title: string;
19+
}
20+
21+
const PostModel = mongoose.model<IPost>('Post', PostSchema);
2022

2123
export { PostSchema, PostModel };

src/__mocks__/userModel.js renamed to src/__mocks__/userModel.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
/* @flow */
2-
3-
import type { Schema as SchemaType } from 'mongoose';
41
import { mongoose, Schema } from './mongooseCommon';
52
import ContactsSchema from './contactsSchema';
63
import enumEmployment from './enumEmployment';
74
import LanguageSchema from './languageSchema';
5+
import { Document } from 'mongoose';
86

9-
const UserSchema: SchemaType<any> = new Schema(
7+
const UserSchema = new Schema(
108
{
119
subDoc: {
1210
field1: String,
@@ -38,7 +36,7 @@ const UserSchema: SchemaType<any> = new Schema(
3836
description: 'Full years',
3937
required() {
4038
// in graphql this field should be Nullable
41-
return this.name === 'Something special';
39+
return (this as any).name === 'Something special';
4240
},
4341
},
4442

@@ -126,10 +124,20 @@ UserSchema.set('autoIndex', false);
126124
UserSchema.index({ name: 1, age: -1 });
127125

128126
// eslint-disable-next-line
129-
UserSchema.virtual('nameVirtual').get(function () {
127+
UserSchema.virtual('nameVirtual').get(function (this: any) {
130128
return `VirtualFieldValue${this._id}`;
131129
});
132130

133-
const UserModel = mongoose.model('User', UserSchema);
131+
export interface IUser extends Document {
132+
_id: any;
133+
name?: string;
134+
age?: number;
135+
gender?: string;
136+
someDynamic?: any;
137+
skills?: string[];
138+
relocation?: boolean;
139+
}
140+
141+
const UserModel = mongoose.model<IUser>('User', UserSchema);
134142

135143
export { UserSchema, UserModel };

src/__tests__/composeWithMongoose-test.js renamed to src/__tests__/composeWithMongoose-test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* @flow */
21
/* eslint-disable no-unused-expressions */
32

43
import mongoose from 'mongoose';
@@ -8,7 +7,7 @@ import { UserModel } from '../__mocks__/userModel';
87
import { composeWithMongoose } from '../composeWithMongoose';
98
import GraphQLMongoID from '../types/mongoid';
109

11-
beforeAll(() => UserModel.base.connect());
10+
beforeAll(() => UserModel.base.createConnection());
1211
afterAll(() => UserModel.base.disconnect());
1312

1413
describe('composeWithMongoose ->', () => {
@@ -164,7 +163,7 @@ describe('composeWithMongoose ->', () => {
164163
findMany: {},
165164
updateOne: {
166165
some: 123,
167-
},
166+
} as any,
168167
},
169168
});
170169
const resolverKeys = Array.from(tc2.getResolvers().keys());

0 commit comments

Comments
 (0)