Skip to content

Commit 696a056

Browse files
authored
Merge pull request #373 from shelfio/feature/Added-typescript-support
Added Typescript support
2 parents c4c1a3c + 6151153 commit 696a056

18 files changed

+139
-65
lines changed

.circleci/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@ jobs:
2828
steps:
2929
- checkout
3030
- install_deps
31+
- run: yarn build
3132
- run: yarn test
3233
- run: yarn test:repl
34+
- run: yarn lint:ci
35+
- run: yarn type-check
36+

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
coverage/
22
lib/
33
renovate.json
4+
tsconfig.json

.eslintrc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
"@shelf/eslint-config/typescript"
55
],
66
"rules": {
7-
"@typescript-eslint/no-var-requires": "off",
8-
"no-console": "off",
9-
"import/order": "off"
7+
"@typescript-eslint/no-var-requires": "off"
108
}
119
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.idea/
22
coverage/
33
node_modules/
4+
lib/
45
temp
56
yarn.lock
67
*.log

jest-preset.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
const {resolve} = require('path');
1+
const preset = require('./lib').default;
22

3-
module.exports = {
4-
globalSetup: resolve(__dirname, './setup.js'),
5-
globalTeardown: resolve(__dirname, './teardown.js'),
6-
testEnvironment: resolve(__dirname, './environment.js'),
7-
};
3+
module.exports = preset;

package.json

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,55 @@
1717
"email": "[email protected]",
1818
"url": "shelf.io"
1919
},
20+
"files": [
21+
"jest-preset.js",
22+
"lib/"
23+
],
2024
"scripts": {
21-
"lint": "eslint . --fix --ext .js,.json,.ts --quiet",
25+
"build": "rm -rf lib/ && yarn build:types && babel src --out-dir lib --ignore '**/*.test.ts' --extensions '.ts'",
26+
"build:types": "tsc --emitDeclarationOnly --declaration --isolatedModules false --declarationDir lib",
27+
"lint": "eslint . --ext .js,.ts,.json --fix",
28+
"lint:ci": "eslint . --ext .js,.ts,.json",
29+
"prepack": "yarn build",
2230
"test": "jest",
23-
"test:repl": "MONGO_MEMORY_SERVER_FILE=jest-mongodb-config-repl.js jest"
31+
"test:repl": "MONGO_MEMORY_SERVER_FILE=jest-mongodb-config-repl.js jest",
32+
"type-check": "tsc --noEmit",
33+
"type-check:watch": "npm run type-check -- --watch"
2434
},
2535
"lint-staged": {
2636
"*.{html,md,yml}": [
27-
"prettier --write",
28-
"git add"
37+
"prettier --write"
2938
],
30-
"*.{js,json}": [
31-
"eslint --fix",
32-
"git add"
39+
"*.{ts,js,json}": [
40+
"eslint --fix"
3341
]
3442
},
43+
"babel": {
44+
"extends": "@shelf/babel-config/backend"
45+
},
3546
"prettier": "@shelf/prettier-config",
3647
"jest": {
3748
"preset": "./jest-preset.js"
3849
},
3950
"dependencies": {
4051
"debug": "4.3.4",
41-
"mongodb-memory-server": "7.6.3",
52+
"mongodb-memory-server": "8.8.0",
4253
"uuid": "8.3.2"
4354
},
4455
"devDependencies": {
45-
"@shelf/eslint-config": "2.18.0",
56+
"@babel/cli": "7.18.10",
57+
"@babel/core": "7.18.10",
58+
"@shelf/babel-config": "1.2.0",
59+
"@shelf/eslint-config": "2.22.0",
4660
"@shelf/prettier-config": "1.0.0",
61+
"@shelf/tsconfig": "0.0.8",
62+
"@types/jest": "28.1.6",
63+
"@types/node": "16",
4764
"eslint": "8.22.0",
4865
"husky": "8.0.1",
4966
"jest": "28.1.3",
5067
"lint-staged": "13.0.3",
51-
"mongodb": "4.7.0",
68+
"mongodb": "4.9.0",
5269
"prettier": "2.7.1",
5370
"typescript": "4.7.4"
5471
},

environment.js renamed to src/environment.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
const {TestEnvironment} = require('jest-environment-node');
2-
const path = require('path');
3-
const fs = require('fs');
1+
import {TestEnvironment} from 'jest-environment-node';
2+
import {join as pathJoin} from 'path';
3+
import {readFileSync} from 'fs';
4+
import type {EnvironmentContext} from '@jest/environment';
5+
import type {JestEnvironmentConfig} from '@jest/environment';
6+
import {MongoMemoryReplSet, MongoMemoryServer} from 'mongodb-memory-server';
7+
import {getMongodbMemoryOptions} from './helpers';
8+
49
const uuid = require('uuid');
5-
const {MongoMemoryServer, MongoMemoryReplSet} = require('mongodb-memory-server');
6-
const {getMongodbMemoryOptions} = require('./helpers');
710

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

1014
const cwd = process.cwd();
1115

12-
const globalConfigPath = path.join(cwd, 'globalConfig.json');
16+
const globalConfigPath = pathJoin(cwd, 'globalConfig.json');
1317
const options = getMongodbMemoryOptions();
1418
const isReplSet = Boolean(options.replSet);
1519

1620
debug(`isReplSet`, isReplSet);
1721

18-
let mongo = isReplSet ? new MongoMemoryReplSet(options) : new MongoMemoryServer(options);
22+
const mongo = isReplSet ? new MongoMemoryReplSet(options) : new MongoMemoryServer(options);
1923

2024
module.exports = class MongoEnvironment extends TestEnvironment {
21-
constructor(config, context) {
25+
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
2226
super(config, context);
2327
}
2428

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

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

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

55+
// @ts-ignore
5156
runScript(script) {
57+
// @ts-ignore
5258
return super.runScript(script);
5359
}
5460
};

helpers.js renamed to src/helpers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const {resolve} = require('path');
1+
import {resolve} from 'path';
22

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

6-
module.exports.getMongodbMemoryOptions = function () {
6+
export function getMongodbMemoryOptions() {
77
try {
88
const {mongodbMemoryServerOptions} = require(resolve(cwd, configFile));
99

@@ -17,19 +17,19 @@ module.exports.getMongodbMemoryOptions = function () {
1717
instance: {},
1818
};
1919
}
20-
};
20+
}
2121

22-
module.exports.getMongoURLEnvName = function () {
22+
export function getMongoURLEnvName() {
2323
try {
2424
const {mongoURLEnvName} = require(resolve(cwd, configFile));
2525

2626
return mongoURLEnvName || 'MONGO_URL';
2727
} catch (e) {
2828
return 'MONGO_URL';
2929
}
30-
};
30+
}
3131

32-
module.exports.shouldUseSharedDBForAllJestWorkers = function () {
32+
export function shouldUseSharedDBForAllJestWorkers() {
3333
try {
3434
const {useSharedDBForAllJestWorkers} = require(resolve(cwd, configFile));
3535

@@ -41,4 +41,4 @@ module.exports.shouldUseSharedDBForAllJestWorkers = function () {
4141
} catch (e) {
4242
return true;
4343
}
44-
};
44+
}

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {resolve} from 'path';
2+
3+
export * from './types';
4+
5+
export default {
6+
globalSetup: resolve(__dirname, './setup.js'),
7+
globalTeardown: resolve(__dirname, './teardown.js'),
8+
testEnvironment: resolve(__dirname, './environment.js'),
9+
};

setup.js renamed to src/setup.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
/* eslint-disable multiline-ternary */
2-
const fs = require('fs');
3-
const {join} = require('path');
4-
const {MongoMemoryServer, MongoMemoryReplSet} = require('mongodb-memory-server');
5-
const {
6-
getMongodbMemoryOptions,
2+
import {writeFileSync} from 'fs';
3+
import {join} from 'path';
4+
import {MongoMemoryReplSet, MongoMemoryServer} from 'mongodb-memory-server';
5+
import {
76
getMongoURLEnvName,
7+
getMongodbMemoryOptions,
88
shouldUseSharedDBForAllJestWorkers,
9-
} = require('./helpers');
9+
} from './helpers';
10+
import type {Mongo} from './types';
1011

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

1516
debug(`isReplSet ${isReplSet}`);
1617

17-
const mongo = isReplSet
18+
// @ts-ignore
19+
const mongo: Mongo = isReplSet
1820
? new MongoMemoryReplSet(mongoMemoryServerOptions)
1921
: new MongoMemoryServer(mongoMemoryServerOptions);
2022

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

2426
module.exports = async () => {
2527
const options = getMongodbMemoryOptions();
26-
const mongoConfig = {};
28+
const mongoConfig: {mongoUri?: string; mongoDBName?: string} = {};
2729

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

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

4850
// Write global config to disk because all tests run in different contexts.
49-
fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
51+
writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
5052
debug('Config is written');
5153
};

0 commit comments

Comments
 (0)