From 772707e85ef309c54201eec0f339c3303be2cf38 Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Fri, 23 Dec 2022 01:26:44 +0200 Subject: [PATCH 01/13] Initial commit with raw setup --- .circleci/config.yml | 12 +++-- .eslintignore | 0 .eslintrc | 5 +- .gitignore | 31 +++--------- .husky/pre-commit | 3 +- .husky/validate-circleci-config.sh | 11 ----- README.md | 30 ------------ jest-postgres-config.js | 6 +++ jest-preset.js | 3 ++ package.json | 79 ++++++++++++++++++------------ readme.md | 22 +++++++++ renovate.json | 6 +-- src/index.test.ts | 5 -- src/index.ts | 9 ++-- src/setup.ts | 9 ++++ src/teardown.ts | 5 ++ test/client.test.ts | 33 +++++++++++++ test/client.ts | 39 +++++++++++++++ test/seed.sql | 16 ++++++ wallaby.config.js | 12 ----- 20 files changed, 208 insertions(+), 128 deletions(-) mode change 100755 => 100644 .eslintignore delete mode 100755 .husky/validate-circleci-config.sh delete mode 100644 README.md create mode 100644 jest-postgres-config.js create mode 100644 jest-preset.js create mode 100644 readme.md delete mode 100644 src/index.test.ts create mode 100644 src/setup.ts create mode 100644 src/teardown.ts create mode 100644 test/client.test.ts create mode 100644 test/client.ts create mode 100644 test/seed.sql delete mode 100644 wallaby.config.js diff --git a/.circleci/config.yml b/.circleci/config.yml index 6ad6af0..0b026b2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,8 +1,8 @@ -orbs: - node: circleci/node@5.0.3 - version: 2.1 +orbs: + node: circleci/node@5.0.2 + parameters: node_version: type: string @@ -11,6 +11,7 @@ parameters: commands: install_deps: steps: + - run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc - node/install-packages: pkg-manager: yarn cache-version: v1-all @@ -27,6 +28,9 @@ jobs: steps: - checkout - install_deps + - run: yarn build - run: yarn test - - run: yarn type-check + - run: yarn test:repl - run: yarn lint:ci + - run: yarn type-check + diff --git a/.eslintignore b/.eslintignore old mode 100755 new mode 100644 diff --git a/.eslintrc b/.eslintrc index 472eed7..731c6be 100644 --- a/.eslintrc +++ b/.eslintrc @@ -2,5 +2,8 @@ "root": true, "extends": [ "@shelf/eslint-config/typescript" - ] + ], + "rules": { + "@typescript-eslint/no-var-requires": "off" + } } diff --git a/.gitignore b/.gitignore index 656d610..c725d7d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,30 +1,11 @@ -.vscode/ -.serverless/ +.idea/ +.idea/aws.xml coverage/ -lib/ node_modules/ - -*.log -junit.xml -draft.js -*.draft.js -draft.ts -*.draft.ts +lib/ +temp yarn.lock - -.env +*.log .DS_Store - -# User-specific stuff -.idea/**/workspace.xml -.idea/**/tasks.xml -.idea/**/usage.statistics.xml -.idea/**/shelf - -# File-based project format -*.iws - -.idea/$CACHE_FILE$ -.idea/$PRODUCT_WORKSPACE_FILE$ -.idea/.gitignore +globalConfig.json !.husky/_/husky.sh diff --git a/.husky/pre-commit b/.husky/pre-commit index bf6c97c..7b0aca4 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,3 @@ #!/bin/sh . "$(dirname "$0")/_/husky.sh" -yarn lint-staged --shell "/bin/sh" --verbose - +yarn lint-staged diff --git a/.husky/validate-circleci-config.sh b/.husky/validate-circleci-config.sh deleted file mode 100755 index 27348d6..0000000 --- a/.husky/validate-circleci-config.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -if ! command -v circleci > /dev/null 2>&1; then - echo "\033[0;31m" \ - "Please, install CircleCI CLI to validate config:" \ - "https://circleci.com/docs/2.0/local-cli/" - - exit 0 -fi - -circleci config validate diff --git a/README.md b/README.md deleted file mode 100644 index 05be0ee..0000000 --- a/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# xxxxxx [![CircleCI](https://circleci.com/gh/shelfio/xxxxxx/tree/master.svg?style=svg)](https://circleci.com/gh/shelfio/xxxxxx/tree/master)![](https://img.shields.io/badge/code_style-prettier-ff69b4.svg) - -> xxxxxx description - -## Install - -``` -$ yarn add @shelf/xxxxxx -``` - -## Usage - -```js -const {getFoo} = require('@shelf/xxxxxx'); - -getFoo(); -``` - -## Publish - -```sh -$ git checkout master -$ yarn version -$ yarn publish -$ git push origin master --tags -``` - -## License - -MIT © [Shelf](https://shelf.io) diff --git a/jest-postgres-config.js b/jest-postgres-config.js new file mode 100644 index 0000000..fe70ffe --- /dev/null +++ b/jest-postgres-config.js @@ -0,0 +1,6 @@ +import cwd from 'cwd'; + +module.exports = { + seedPath: `${cwd()}/test/seed.sql`, + version: 14, +}; diff --git a/jest-preset.js b/jest-preset.js new file mode 100644 index 0000000..5102ee9 --- /dev/null +++ b/jest-preset.js @@ -0,0 +1,3 @@ +const preset = require('./lib'); + +module.exports = preset; diff --git a/package.json b/package.json index 18a938d..b54f4e6 100644 --- a/package.json +++ b/package.json @@ -1,27 +1,33 @@ { - "name": "@shelf/xxxxxx", - "version": "0.0.0", - "description": "xxxxxx", + "name": "@shelf/jest-postgres", + "version": "0.1.0", + "private": false, + "description": "Run your tests using Jest & Postgres", + "keywords": [ + "jest", + "jest environment", + "jest preset", + "postgres", + "postgres local" + ], + "repository": "shelfio/jest-postgres", "license": "MIT", "author": { - "name": "Vlad Holubiev", - "email": "vlad@shelf.io", - "url": "https://shelf.io" + "name": "Dmytro Harazdovskyi", + "email": "dmytro.harazdovskyi@shelf.io", + "url": "shelf.io" }, - "main": "lib", - "types": "lib/index.d.ts", "files": [ - "lib" + "jest-preset.js", + "lib/" ], "scripts": { - "build": "rm -rf lib/ && yarn build:types && yarn build:code", - "build:code": "babel src --out-dir lib --ignore '**/*.test.ts' --extensions '.ts' && find ./lib -name '*.test.d.ts' -delete", + "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", - "coverage": "yarn test --coverage", - "lint": "yarn lint:ci --fix", + "lint": "eslint . --ext .js,.ts,.json --fix", "lint:ci": "eslint . --ext .js,.ts,.json", "prepack": "yarn build", - "test": "TZ=UTC jest src", + "test": "jest", "type-check": "tsc --noEmit", "type-check:watch": "npm run type-check -- --watch" }, @@ -29,33 +35,46 @@ "*.{html,md,yml}": [ "prettier --write" ], - "*.{js,ts,json}": [ + "*.{ts,js,json}": [ "eslint --fix" - ], - ".circleci/config.yml": [ - ".husky/validate-circleci-config.sh" ] }, "babel": { "extends": "@shelf/babel-config/backend" }, "prettier": "@shelf/prettier-config", - "dependencies": {}, + "jest": { + "preset": "./jest-preset.js" + }, + "dependencies": { + "@shelf/postgres-local": "0.1.1", + "cwd": "0.10.0", + "debug": "4.3.4", + "knex": "2.3.0", + "pg": "8.8.0", + "uuid": "8.3.2" + }, "devDependencies": { - "@babel/cli": "7.19.3", - "@babel/core": "7.20.5", + "@babel/cli": "7.18.10", + "@babel/core": "7.18.10", "@shelf/babel-config": "1.2.0", - "@shelf/eslint-config": "2.26.0", + "@shelf/eslint-config": "2.22.3", "@shelf/prettier-config": "1.0.0", - "@shelf/tsconfig": "0.0.9", - "@types/jest": "29.2.4", + "@shelf/tsconfig": "0.0.8", + "@types/cwd": "0.10.0", + "@types/jest": "29.2.0", "@types/node": "16", - "eslint": "8.29.0", - "husky": "8.0.2", - "jest": "29.3.1", - "lint-staged": "13.1.0", - "prettier": "2.8.1", - "typescript": "4.9.4" + "cwd": "0.10.0", + "eslint": "8.26.0", + "husky": "8.0.1", + "jest": "29.2.1", + "lint-staged": "13.0.3", + "prettier": "2.7.1", + "typescript": "4.8.4" + }, + "peerDependencies": { + "jest-environment-node": "27.x.x || 28.x || 29.x", + "pg": "*" }, "engines": { "node": ">=16" diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..16ac9ae --- /dev/null +++ b/readme.md @@ -0,0 +1,22 @@ +# jest-postgres [![CircleCI](https://circleci.com/gh/shelfio/jest-postgres/tree/master.svg?style=svg)](https://circleci.com/gh/shelfio/jest-postgres/tree/master) ![](https://img.shields.io/badge/code_style-prettier-ff69b4.svg) [![npm (scoped)](https://img.shields.io/npm/v/@shelf/jest-postgres.svg)](https://www.npmjs.com/package/@shelf/jest-postgres) + +> Jest preset to run Postgres server + +## TO BE DONE + +## See Also + +- [jest-dynamodb](https://github.com/shelfio/jest-dynamodb) + +## Publish + +```sh +$ git checkout master +$ yarn version +$ yarn publish +$ git push origin master --tags +``` + +## License + +MIT © [Shelf](https://shelf.io) diff --git a/renovate.json b/renovate.json index 25594b1..0c35e29 100644 --- a/renovate.json +++ b/renovate.json @@ -1,8 +1,4 @@ { "extends": ["config:base"], - "labels": ["dependencies", "backend"], - "ignoreDeps": [ - "cimg/node", - "@types/node" - ] + "labels": ["dependencies", "backend"] } diff --git a/src/index.test.ts b/src/index.test.ts deleted file mode 100644 index 7fe80c6..0000000 --- a/src/index.test.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {getFoo} from './index'; - -it('should return bar', () => { - expect(getFoo()).toEqual('bar'); -}); diff --git a/src/index.ts b/src/index.ts index 494c403..3da5542 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,6 @@ -export function getFoo(): string { - return 'bar'; -} +import {resolve} from 'path'; + +module.exports = { + globalSetup: resolve(__dirname, './setup.js'), + globalTeardown: resolve(__dirname, './teardown.js'), +}; diff --git a/src/setup.ts b/src/setup.ts new file mode 100644 index 0000000..2135d1e --- /dev/null +++ b/src/setup.ts @@ -0,0 +1,9 @@ +import {start} from '@shelf/postgres-local'; +import cwd from 'cwd'; + +module.exports = async function startPostgres() { + const config = require(`${cwd()}/jest-postgres-config.js`); + await start(config); + + return true; +}; diff --git a/src/teardown.ts b/src/teardown.ts new file mode 100644 index 0000000..bc24ffe --- /dev/null +++ b/src/teardown.ts @@ -0,0 +1,5 @@ +import {stop} from '@shelf/postgres-local'; + +module.exports = async function stopPostgres() { + await stop(); +}; diff --git a/test/client.test.ts b/test/client.test.ts new file mode 100644 index 0000000..51cfa57 --- /dev/null +++ b/test/client.test.ts @@ -0,0 +1,33 @@ +import {getClient} from './client'; + +describe('#getClient', () => { + const client = getClient({ + host: 'localhost', + port: 5432, + username: '', + password: '', + database: 'postgres', + }); + + it('should insert records', async () => { + await client.withSchema('test').table('model1').insert({ + id: 'unique', + type: 'some text', + text: 'should be text', + }); + + const res = await client.withSchema('test').table('model1').select('*').where({id: 'unique'}); + + expect(res).toEqual([ + { + id: 'unique', + json: null, + somebool: null, + text: 'should be text', + type: 'some text', + updated_at: null, + vector: null, + }, + ]); + }); +}); diff --git a/test/client.ts b/test/client.ts new file mode 100644 index 0000000..5b8c359 --- /dev/null +++ b/test/client.ts @@ -0,0 +1,39 @@ +import knex from 'knex'; +import type {Knex} from 'knex'; + +let client: Knex; +let connected = false; + +export function getClient(params: { + host: string; + port: number; + username: string; + password: string; + database: string; +}): Knex { + if (client && connected) { + return client; + } + + client = knex({ + client: 'pg', + connection: { + host: params.host, + port: params.port, + user: params.username, + password: params.password, + database: params.database, + }, + pool: { + min: 2, + }, + }); + connected = true; + + return client; +} + +export const closeConnection = async () => { + connected = false; + await client.destroy(); +}; diff --git a/test/seed.sql b/test/seed.sql new file mode 100644 index 0000000..b8124a4 --- /dev/null +++ b/test/seed.sql @@ -0,0 +1,16 @@ +create schema test; + +create table test.model1 +( + id varchar(36) not null, + type text, + text text, + vector double precision[], + json jsonb, + updated_at timestamp, + someBool bool, + constraint message + primary key (id) +); + +create index some_cool_index on test.model1 using btree (id); diff --git a/wallaby.config.js b/wallaby.config.js deleted file mode 100644 index 881fb4d..0000000 --- a/wallaby.config.js +++ /dev/null @@ -1,12 +0,0 @@ -module.exports = () => { - return { - autoDetect: true, - files: ['package.json', 'src/**/*.ts', '!src/**/*.test.ts'], - tests: ['src/**/*.test.ts'], - env: { - params: { - env: 'TZ=UTC', - }, - }, - }; -}; From 33b33c32a1b7a2ee778b692f7a2742c385db2422 Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Fri, 23 Dec 2022 13:36:43 +0200 Subject: [PATCH 02/13] fix: fixed teardown --- src/teardown.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/teardown.ts b/src/teardown.ts index bc24ffe..d458559 100644 --- a/src/teardown.ts +++ b/src/teardown.ts @@ -1,5 +1,7 @@ import {stop} from '@shelf/postgres-local'; +import cwd from 'cwd'; module.exports = async function stopPostgres() { - await stop(); + const config = require(`${cwd()}/jest-postgres-config.js`); + await stop(config?.version); }; From 714704958f6c7775fb4ba0150bf6ed6332e701e5 Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Sat, 24 Dec 2022 20:30:27 +0200 Subject: [PATCH 03/13] feat: Added run as root config --- package.json | 2 +- src/teardown.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b54f4e6..e3519f7 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "preset": "./jest-preset.js" }, "dependencies": { - "@shelf/postgres-local": "0.1.1", + "@shelf/postgres-local": "0.1.3", "cwd": "0.10.0", "debug": "4.3.4", "knex": "2.3.0", diff --git a/src/teardown.ts b/src/teardown.ts index d458559..8591a7e 100644 --- a/src/teardown.ts +++ b/src/teardown.ts @@ -3,5 +3,5 @@ import cwd from 'cwd'; module.exports = async function stopPostgres() { const config = require(`${cwd()}/jest-postgres-config.js`); - await stop(config?.version); + await stop(config); }; From 2824648f6baf66d5dcef13e4f04418411339876a Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Sat, 24 Dec 2022 20:30:57 +0200 Subject: [PATCH 04/13] v0.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e3519f7..15bbebb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@shelf/jest-postgres", - "version": "0.1.0", + "version": "0.1.1", "private": false, "description": "Run your tests using Jest & Postgres", "keywords": [ From 5eda2fb9f2d2bc3dc3fe58e3108a0d6f9c03cdf0 Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Mon, 26 Dec 2022 18:43:54 +0200 Subject: [PATCH 05/13] fix: Updated `postgres-local`, fixed configs --- jest-postgres-config.js | 1 + package.json | 2 +- src/teardown.ts | 4 ++-- test/client.test.ts | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/jest-postgres-config.js b/jest-postgres-config.js index fe70ffe..3146b72 100644 --- a/jest-postgres-config.js +++ b/jest-postgres-config.js @@ -3,4 +3,5 @@ import cwd from 'cwd'; module.exports = { seedPath: `${cwd()}/test/seed.sql`, version: 14, + port: 5555, }; diff --git a/package.json b/package.json index 15bbebb..cbc6e5f 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "preset": "./jest-preset.js" }, "dependencies": { - "@shelf/postgres-local": "0.1.3", + "@shelf/postgres-local": "0.1.4", "cwd": "0.10.0", "debug": "4.3.4", "knex": "2.3.0", diff --git a/src/teardown.ts b/src/teardown.ts index 8591a7e..43ac659 100644 --- a/src/teardown.ts +++ b/src/teardown.ts @@ -1,7 +1,7 @@ import {stop} from '@shelf/postgres-local'; import cwd from 'cwd'; -module.exports = async function stopPostgres() { +module.exports = function stopPostgres() { const config = require(`${cwd()}/jest-postgres-config.js`); - await stop(config); + stop(config); }; diff --git a/test/client.test.ts b/test/client.test.ts index 51cfa57..efe35f3 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -3,13 +3,13 @@ import {getClient} from './client'; describe('#getClient', () => { const client = getClient({ host: 'localhost', - port: 5432, + port: 5555, username: '', password: '', database: 'postgres', }); - it('should insert records', async () => { + it('should use 5555 port and insert records', async () => { await client.withSchema('test').table('model1').insert({ id: 'unique', type: 'some text', From ca9db5d738f44cdfdd70cfecdcbf6c715325f597 Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Mon, 26 Dec 2022 18:44:21 +0200 Subject: [PATCH 06/13] v0.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cbc6e5f..f532d17 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@shelf/jest-postgres", - "version": "0.1.1", + "version": "0.1.2", "private": false, "description": "Run your tests using Jest & Postgres", "keywords": [ From d07d4b6513727387a49aa54866e90981b793d744 Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Mon, 26 Dec 2022 19:14:42 +0200 Subject: [PATCH 07/13] v0.1.3 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f532d17..a719833 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@shelf/jest-postgres", - "version": "0.1.2", + "version": "0.1.3", "private": false, "description": "Run your tests using Jest & Postgres", "keywords": [ @@ -47,7 +47,7 @@ "preset": "./jest-preset.js" }, "dependencies": { - "@shelf/postgres-local": "0.1.4", + "@shelf/postgres-local": "0.1.5", "cwd": "0.10.0", "debug": "4.3.4", "knex": "2.3.0", From cfb65981059926efd1830a53379c51a41290548e Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Mon, 26 Dec 2022 19:21:44 +0200 Subject: [PATCH 08/13] feat: Updated `postgres-local` --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a719833..8553f92 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "preset": "./jest-preset.js" }, "dependencies": { - "@shelf/postgres-local": "0.1.5", + "@shelf/postgres-local": "0.1.6", "cwd": "0.10.0", "debug": "4.3.4", "knex": "2.3.0", From dcb9430b8f68555fb2010613b7028035dec8fc0d Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Mon, 26 Dec 2022 19:22:07 +0200 Subject: [PATCH 09/13] v0.1.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8553f92..59282bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@shelf/jest-postgres", - "version": "0.1.3", + "version": "0.1.4", "private": false, "description": "Run your tests using Jest & Postgres", "keywords": [ From 930ea2ec2d1e00d491d0f9a42447445607ee9857 Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Tue, 27 Dec 2022 13:45:27 +0200 Subject: [PATCH 10/13] feat: Added doc, updated circle ci `config.yml` --- .circleci/config.yml | 17 +++++++---------- package.json | 12 +++++------- readme.md | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 0b026b2..1c738f4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,36 +1,33 @@ -version: 2.1 - orbs: node: circleci/node@5.0.2 +version: 2.1 + parameters: node_version: type: string - default: '16.15.1' + default: '16.17.0' commands: install_deps: steps: - - run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc - node/install-packages: pkg-manager: yarn cache-version: v1-all cache-only-lockfile: true app-dir: ~/repo override-ci-command: yarn install --pure-lockfile --no-progress + - run: sudo apt update -q && sudo apt install postgresql-12 jobs: build: executor: name: node/default - tag: << pipeline.parameters.node_version >> + tag: <> working_directory: ~/repo steps: - checkout - install_deps - - run: yarn build - - run: yarn test - - run: yarn test:repl - - run: yarn lint:ci + - run: sudo yarn test - run: yarn type-check - + - run: yarn lint:ci diff --git a/package.json b/package.json index 59282bc..434ec68 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@shelf/jest-postgres", - "version": "0.1.4", + "version": "1.0.0", "private": false, "description": "Run your tests using Jest & Postgres", "keywords": [ @@ -47,12 +47,8 @@ "preset": "./jest-preset.js" }, "dependencies": { - "@shelf/postgres-local": "0.1.6", - "cwd": "0.10.0", - "debug": "4.3.4", - "knex": "2.3.0", - "pg": "8.8.0", - "uuid": "8.3.2" + "@shelf/postgres-local": "1.0.2", + "cwd": "0.10.0" }, "devDependencies": { "@babel/cli": "7.18.10", @@ -68,7 +64,9 @@ "eslint": "8.26.0", "husky": "8.0.1", "jest": "29.2.1", + "knex": "2.3.0", "lint-staged": "13.0.3", + "pg": "8.8.0", "prettier": "2.7.1", "typescript": "4.8.4" }, diff --git a/readme.md b/readme.md index 16ac9ae..4ac3a38 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,41 @@ > Jest preset to run Postgres server -## TO BE DONE +## Usage + +### 0. Install + +``` +$ yarn add @shelf/jest-postgres --dev +``` + +### 1. Create `jest.config.js` + +```js +module.exports = { + preset: '@shelf/jest-postgres' +}; +``` + +If you have a custom `jest.config.js` make sure you remove `testEnvironment` property, otherwise it will conflict with the preset. + +### 2. Create `jest-postgres-config.js` + +```js +import cwd from 'cwd'; + +module.exports = { + seedPath: `${cwd()}/test/seed.sql`, + version: 14, + port: 5555, +}; +``` +Find `seed.sql` example in `./test` folder of this repo +### 4. PROFIT! Write tests + +```js +it(); +``` ## See Also From 60096518edeaa7fad030617c4ef69405c5162880 Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Tue, 27 Dec 2022 13:49:21 +0200 Subject: [PATCH 11/13] feat: Updated deps, added circle ci step --- .circleci/config.yml | 1 + jest-postgres-config.js | 2 +- package.json | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 1c738f4..c33bc2a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -28,6 +28,7 @@ jobs: steps: - checkout - install_deps + - run: yarn build - run: sudo yarn test - run: yarn type-check - run: yarn lint:ci diff --git a/jest-postgres-config.js b/jest-postgres-config.js index 3146b72..bf4982d 100644 --- a/jest-postgres-config.js +++ b/jest-postgres-config.js @@ -2,6 +2,6 @@ import cwd from 'cwd'; module.exports = { seedPath: `${cwd()}/test/seed.sql`, - version: 14, + version: 12, port: 5555, }; diff --git a/package.json b/package.json index 434ec68..a0a7aa6 100644 --- a/package.json +++ b/package.json @@ -51,8 +51,8 @@ "cwd": "0.10.0" }, "devDependencies": { - "@babel/cli": "7.18.10", - "@babel/core": "7.18.10", + "@babel/cli": "7.20.7", + "@babel/core": "7.20.7", "@shelf/babel-config": "1.2.0", "@shelf/eslint-config": "2.22.3", "@shelf/prettier-config": "1.0.0", @@ -61,7 +61,7 @@ "@types/jest": "29.2.0", "@types/node": "16", "cwd": "0.10.0", - "eslint": "8.26.0", + "eslint": "8.30.0", "husky": "8.0.1", "jest": "29.2.1", "knex": "2.3.0", From 8205e11db83d36a046045e9177102d0af8d663fb Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Tue, 27 Dec 2022 13:53:16 +0200 Subject: [PATCH 12/13] feat: Added `return` --- src/teardown.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/teardown.ts b/src/teardown.ts index 43ac659..04309c6 100644 --- a/src/teardown.ts +++ b/src/teardown.ts @@ -3,5 +3,6 @@ import cwd from 'cwd'; module.exports = function stopPostgres() { const config = require(`${cwd()}/jest-postgres-config.js`); - stop(config); + + return stop(config); }; From 79269923e237f25033f37a4ce4676a608b52e08b Mon Sep 17 00:00:00 2001 From: harazdovskiy Date: Tue, 27 Dec 2022 18:04:52 +0200 Subject: [PATCH 13/13] feat: Fixed tests timeout --- package.json | 2 +- src/setup.ts | 3 ++- src/teardown.ts | 6 +++--- test/client.test.ts | 4 +++- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index a0a7aa6..363bc8d 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "preset": "./jest-preset.js" }, "dependencies": { - "@shelf/postgres-local": "1.0.2", + "@shelf/postgres-local": "1.0.3", "cwd": "0.10.0" }, "devDependencies": { diff --git a/src/setup.ts b/src/setup.ts index 2135d1e..0213b44 100644 --- a/src/setup.ts +++ b/src/setup.ts @@ -1,8 +1,9 @@ import {start} from '@shelf/postgres-local'; import cwd from 'cwd'; +const config = require(`${cwd()}/jest-postgres-config.js`); + module.exports = async function startPostgres() { - const config = require(`${cwd()}/jest-postgres-config.js`); await start(config); return true; diff --git a/src/teardown.ts b/src/teardown.ts index 04309c6..5e0556d 100644 --- a/src/teardown.ts +++ b/src/teardown.ts @@ -1,8 +1,8 @@ import {stop} from '@shelf/postgres-local'; import cwd from 'cwd'; -module.exports = function stopPostgres() { - const config = require(`${cwd()}/jest-postgres-config.js`); +const config = require(`${cwd()}/jest-postgres-config.js`); - return stop(config); +module.exports = async function stopPostgres() { + await stop(config); }; diff --git a/test/client.test.ts b/test/client.test.ts index efe35f3..b35c2f1 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -1,4 +1,4 @@ -import {getClient} from './client'; +import {closeConnection, getClient} from './client'; describe('#getClient', () => { const client = getClient({ @@ -29,5 +29,7 @@ describe('#getClient', () => { vector: null, }, ]); + + await closeConnection(); }); });