Skip to content

Commit

Permalink
feat: Added Typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
harazdovskiy committed Aug 19, 2022
1 parent c4c1a3c commit 8697583
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 63 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ jobs:
steps:
- checkout
- install_deps
- run: yarn build
- run: yarn test
- run: yarn test:repl
- run: yarn lint:ci
- run: yarn type-check

4 changes: 1 addition & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"@shelf/eslint-config/typescript"
],
"rules": {
"@typescript-eslint/no-var-requires": "off",
"no-console": "off",
"import/order": "off"
"@typescript-eslint/no-var-requires": "off"
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea/
coverage/
node_modules/
lib/
temp
yarn.lock
*.log
Expand Down
8 changes: 2 additions & 6 deletions jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const {resolve} = require('path');
const preset = require('./lib').default;

module.exports = {
globalSetup: resolve(__dirname, './setup.js'),
globalTeardown: resolve(__dirname, './teardown.js'),
testEnvironment: resolve(__dirname, './environment.js'),
};
module.exports = preset;
31 changes: 24 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,32 @@
"email": "[email protected]",
"url": "shelf.io"
},
"files": [
"jest-preset.js",
"lib/"
],
"scripts": {
"lint": "eslint . --fix --ext .js,.json,.ts --quiet",
"build": "rm -rf lib/ && yarn build:types && babel src --out-dir lib --ignore '**/*.test.ts' --extensions '.ts'",
"build:types": "tsc --emitDeclarationOnly --declaration --isolatedModules false --declarationDir lib",
"lint": "eslint . --ext .js,.ts,.json --fix",
"lint:ci": "eslint . --ext .js,.ts,.json",
"prepack": "yarn build",
"test": "jest",
"test:repl": "MONGO_MEMORY_SERVER_FILE=jest-mongodb-config-repl.js jest"
"test:repl": "MONGO_MEMORY_SERVER_FILE=jest-mongodb-config-repl.js jest",
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch"
},
"lint-staged": {
"*.{html,md,yml}": [
"prettier --write",
"git add"
"prettier --write"
],
"*.{js,json}": [
"eslint --fix",
"git add"
"*.{ts,js,json}": [
"eslint --fix"
]
},
"babel": {
"extends": "@shelf/babel-config/backend"
},
"prettier": "@shelf/prettier-config",
"jest": {
"preset": "./jest-preset.js"
Expand All @@ -42,8 +53,14 @@
"uuid": "8.3.2"
},
"devDependencies": {
"@babel/cli": "7.18.10",
"@babel/core": "7.18.10",
"@shelf/babel-config": "1.2.0",
"@shelf/eslint-config": "2.18.0",
"@shelf/prettier-config": "1.0.0",
"@shelf/tsconfig": "0.0.8",
"@types/jest": "28.1.6",
"@types/node": "16",
"eslint": "8.22.0",
"husky": "8.0.1",
"jest": "28.1.3",
Expand Down
24 changes: 15 additions & 9 deletions environment.js → src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
const {TestEnvironment} = require('jest-environment-node');
const path = require('path');
const fs = require('fs');
import {TestEnvironment} from 'jest-environment-node';
import {join as pathJoin} from 'path';
import {readFileSync} from 'fs';
import type {EnvironmentContext} from '@jest/environment';
import type {JestEnvironmentConfig} from '@jest/environment';
import {MongoMemoryReplSet, MongoMemoryServer} from 'mongodb-memory-server';
import {getMongodbMemoryOptions} from './helpers';

const uuid = require('uuid');
const {MongoMemoryServer, MongoMemoryReplSet} = require('mongodb-memory-server');
const {getMongodbMemoryOptions} = require('./helpers');

// eslint-disable-next-line import/order
const debug = require('debug')('jest-mongodb:environment');

const cwd = process.cwd();

const globalConfigPath = path.join(cwd, 'globalConfig.json');
const globalConfigPath = pathJoin(cwd, 'globalConfig.json');
const options = getMongodbMemoryOptions();
const isReplSet = Boolean(options.replSet);

debug(`isReplSet`, isReplSet);

let mongo = isReplSet ? new MongoMemoryReplSet(options) : new MongoMemoryServer(options);
const mongo = isReplSet ? new MongoMemoryReplSet(options) : new MongoMemoryServer(options);

module.exports = class MongoEnvironment extends TestEnvironment {
constructor(config, context) {
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
super(config, context);
}

async setup() {
debug('Setup MongoDB Test Environment');

const globalConfig = JSON.parse(fs.readFileSync(globalConfigPath, 'utf-8'));
const globalConfig = JSON.parse(readFileSync(globalConfigPath, 'utf-8'));

if (globalConfig.mongoUri) {
this.global.__MONGO_URI__ = globalConfig.mongoUri;
Expand All @@ -48,7 +52,9 @@ module.exports = class MongoEnvironment extends TestEnvironment {
await super.teardown();
}

// @ts-ignore
runScript(script) {
// @ts-ignore
return super.runScript(script);
}
};
14 changes: 7 additions & 7 deletions helpers.js → src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const {resolve} = require('path');
import {resolve} from 'path';

const cwd = process.cwd();
const configFile = process.env.MONGO_MEMORY_SERVER_FILE || 'jest-mongodb-config.js';

module.exports.getMongodbMemoryOptions = function () {
export function getMongodbMemoryOptions() {
try {
const {mongodbMemoryServerOptions} = require(resolve(cwd, configFile));

Expand All @@ -17,19 +17,19 @@ module.exports.getMongodbMemoryOptions = function () {
instance: {},
};
}
};
}

module.exports.getMongoURLEnvName = function () {
export function getMongoURLEnvName() {
try {
const {mongoURLEnvName} = require(resolve(cwd, configFile));

return mongoURLEnvName || 'MONGO_URL';
} catch (e) {
return 'MONGO_URL';
}
};
}

module.exports.shouldUseSharedDBForAllJestWorkers = function () {
export function shouldUseSharedDBForAllJestWorkers() {
try {
const {useSharedDBForAllJestWorkers} = require(resolve(cwd, configFile));

Expand All @@ -41,4 +41,4 @@ module.exports.shouldUseSharedDBForAllJestWorkers = function () {
} catch (e) {
return true;
}
};
}
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {resolve} from 'path';

export * from './types';

export default {
globalSetup: resolve(__dirname, './setup.js'),
globalTeardown: resolve(__dirname, './teardown.js'),
testEnvironment: resolve(__dirname, './environment.js'),
};
20 changes: 11 additions & 9 deletions setup.js → src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/* eslint-disable multiline-ternary */
const fs = require('fs');
const {join} = require('path');
const {MongoMemoryServer, MongoMemoryReplSet} = require('mongodb-memory-server');
const {
getMongodbMemoryOptions,
import {writeFileSync} from 'fs';
import {join} from 'path';
import {MongoMemoryReplSet, MongoMemoryServer} from 'mongodb-memory-server';
import {
getMongoURLEnvName,
getMongodbMemoryOptions,
shouldUseSharedDBForAllJestWorkers,
} = require('./helpers');
} from './helpers';
import type {Mongo} from './types';

const debug = require('debug')('jest-mongodb:setup');
const mongoMemoryServerOptions = getMongodbMemoryOptions();
const isReplSet = Boolean(mongoMemoryServerOptions.replSet);

debug(`isReplSet ${isReplSet}`);

const mongo = isReplSet
// @ts-ignore
const mongo: Mongo = isReplSet
? new MongoMemoryReplSet(mongoMemoryServerOptions)
: new MongoMemoryServer(mongoMemoryServerOptions);

Expand All @@ -23,7 +25,7 @@ const globalConfigPath = join(cwd, 'globalConfig.json');

module.exports = async () => {
const options = getMongodbMemoryOptions();
const mongoConfig = {};
const mongoConfig: {mongoUri?: string; mongoDBName?: string} = {};

debug(`shouldUseSharedDBForAllJestWorkers: ${shouldUseSharedDBForAllJestWorkers()}`);

Expand All @@ -46,6 +48,6 @@ module.exports = async () => {
mongoConfig.mongoDBName = options.instance.dbName;

// Write global config to disk because all tests run in different contexts.
fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
debug('Config is written');
};
7 changes: 4 additions & 3 deletions teardown.js → src/teardown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {join} from 'path';
import {unlink} from 'fs';

const debug = require('debug')('jest-mongodb:teardown');
const {join} = require('path');
const fs = require('fs');

const cwd = process.cwd();
const globalConfigPath = join(cwd, 'globalConfig.json');
Expand All @@ -10,7 +11,7 @@ module.exports = async function () {
if (global.__MONGOD__) {
await global.__MONGOD__.stop();
}
fs.unlink(globalConfigPath, err => {
unlink(globalConfigPath, err => {
if (err) {
debug('Config could not be deleted');

Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable */
import { MongoMemoryReplSet, MongoMemoryServer } from "mongodb-memory-server";

declare global {
var __MONGOD__: Mongo;
var __MONGO_URI__: string;
var __MONGO_DB_NAME__: string
}

export type Mongo = (MongoMemoryReplSet | MongoMemoryServer) & {isRunning: boolean}
10 changes: 7 additions & 3 deletions mongo-aggregate.test.js → test/mongo-aggregate.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const {MongoClient} = require('mongodb');
import type {Db} from 'mongodb';
import {MongoClient} from 'mongodb';
import '../src/types';

describe('insert', () => {
const uri = global.__MONGO_URI__;
let connection;
let db;
let connection: MongoClient;
let db: Db;

beforeAll(async () => {
// @ts-ignore
connection = await MongoClient.connect(uri, {
// @ts-ignore
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand Down
12 changes: 8 additions & 4 deletions mongo-insert.test.js → test/mongo-insert.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const {MongoClient} = require('mongodb');
import type {Db} from 'mongodb';
import {MongoClient} from 'mongodb';
import '../src/types';

describe('insert', () => {
const uri = global.__MONGO_URI__;
let connection;
let db;
let connection: MongoClient;
let db: Db;

beforeAll(async () => {
// @ts-ignore
connection = await MongoClient.connect(uri, {
// @ts-ignore
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand All @@ -21,7 +25,7 @@ describe('insert', () => {
const users = db.collection('users');

const mockUser = {_id: 'some-user-id', name: 'John'};
await users.insertOne(mockUser);
await users.insertOne(mockUser as any);

const insertedUser = await users.findOne({_id: 'some-user-id'});

Expand Down
12 changes: 8 additions & 4 deletions mongo-parallelism.test.js → test/mongo-parallelism.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const {MongoClient} = require('mongodb');
const {shouldUseSharedDBForAllJestWorkers} = require('./helpers');
import type {Db} from 'mongodb';
import {MongoClient} from 'mongodb';
import '../src/types';
import {shouldUseSharedDBForAllJestWorkers} from '../src/helpers';

describe('parallelism: first worker', () => {
const uri = global.__MONGO_URI__;
let connection;
let db;
let connection: MongoClient;
let db: Db;

beforeAll(async () => {
// @ts-ignore
connection = await MongoClient.connect(uri, {
// @ts-ignore
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand Down
12 changes: 8 additions & 4 deletions mongo-parallelism2.test.js → test/mongo-parallelism2.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const {MongoClient} = require('mongodb');
const {shouldUseSharedDBForAllJestWorkers} = require('./helpers');
import type {Db} from 'mongodb';
import {MongoClient} from 'mongodb';
import '../src/types';
import {shouldUseSharedDBForAllJestWorkers} from '../src/helpers';

describe('parallelism: second worker', () => {
const uri = global.__MONGO_URI__;
let connection;
let db;
let connection: MongoClient;
let db: Db;

beforeAll(async () => {
// @ts-ignore
connection = await MongoClient.connect(uri, {
// @ts-ignore
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand Down
12 changes: 8 additions & 4 deletions mongo-parallelism3.test.js → test/mongo-parallelism3.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const {MongoClient} = require('mongodb');
const {shouldUseSharedDBForAllJestWorkers} = require('./helpers');
import type {Db} from 'mongodb';
import {MongoClient} from 'mongodb';
import '../src/types';
import {shouldUseSharedDBForAllJestWorkers} from '../src/helpers';

describe('parallelism: third worker', () => {
const uri = global.__MONGO_URI__;
let connection;
let db;
let connection: MongoClient;
let db: Db;

beforeAll(async () => {
// @ts-ignore
connection = await MongoClient.connect(uri, {
// @ts-ignore
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand Down
Loading

0 comments on commit 8697583

Please sign in to comment.