Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
feat(jest-environment-knex): make it a standalone package (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil authored Apr 30, 2019
1 parent 8416967 commit d178651
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM node:10-stretch

COPY ./package.json /app/package.json
COPY ./packages/jest-environment-knex/package.json /app/packages/jest-environment-knex/package.json
COPY ./packages/api/package.json /app/packages/api/package.json
COPY ./packages/app/package.json /app/packages/app/package.json
COPY ./packages/knex/package.json /app/packages/knex/package.json
Expand All @@ -14,6 +15,7 @@ RUN yarn --frozen-lockfile && yarn cache clean
#

COPY ./lerna.json /app/lerna.json
COPY ./packages/jest-environment-knex /app/packages/jest-environment-knex
COPY ./packages/knex /app/packages/knex
COPY ./packages/api /app/packages/api
COPY ./packages/app /app/packages/app
Expand Down
2 changes: 1 addition & 1 deletion packages/api/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
testEnvironment: "@emjpm/knex/jest-environment-knex.js",
testEnvironment: "jest-environment-knex",
testEnvironmentOptions: require("@emjpm/knex/knexfile.js").test,
roots: ["<rootDir>/__test__/"],
collectCoverageFrom: ["auth/**/*.js", "email/**/*.js", "routes/**/*.js"]
Expand Down
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"eslint-config-prettier": "^4.2.0",
"eslint-plugin-prettier": "^3.0.1",
"jest-cli": "^24.7.1",
"jest-environment-knex": "^1.0.0",
"mocha": "^6.1.4",
"nodemailer-mock": "^1.4.1",
"nodemon": "^1.18.11",
Expand Down
1 change: 1 addition & 0 deletions packages/jest-environment-knex/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
27 changes: 27 additions & 0 deletions packages/jest-environment-knex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "jest-environment-knex",
"version": "1.0.0",
"main": "build/jest-environment-knex.js",
"types": "build/jest-environment-knex.d.ts",
"scripts": {
"build": "tsc"
},
"dependencies": {
"jest-environment-node": "^24.7.1",
"knex": "^0.16.5",
"rand-token": "^0.4.0"
},
"devDependencies": {
"@jest/types": "^24.7.0",
"typescript": "^3.4.5"
},
"repository": {
"type": "git",
"url": "https://github.com/SocialGouv/emjpm.git",
"directory": "packages/jest-environment-knex"
},
"license": "Apache-2.0",
"publishConfig": {
"access": "public"
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
const NodeEnvironment = require("jest-environment-node");
//

import { Script, runInContext } from "vm";
import NodeEnvironment from "jest-environment-node";
import { Global, Config } from "@jest/types";
import Knex from "knex";
const parseConnection = require("knex/lib/util/parse-connection");
const debug = require("debug")("jest-environment-knex");
const Knex = require("knex");
const { uid } = require("rand-token");

//

class KnexEnvironment extends NodeEnvironment {
constructor(config) {
debug("constructor");
destroyPromises: Promise<void>[] = [];

global: Global.Global & { databaseName: string; knex: Knex };
options: Knex.Config;

constructor(config: Config.ProjectConfig) {
super(config);
debug("super(config)");

//

const global = (this.global = runInContext(
"this",
Object.assign(this.context, config.testEnvironmentOptions)
));
global.databaseName = `emjpm_test_${uid(16).toLowerCase()}`;

//

this.options = config.testEnvironmentOptions;
this.destroyPromises = [];
}

lazyDestroy(knex) {
lazyDestroy(knex: Knex) {
this.destroyPromises.push(
knex.destroy().then(debug.bind(null, "knex.destroyed"), console.error)
((knex.destroy() as any) as Promise<void>).then(
debug.bind(null, "knex.destroyed"),
console.error
)
);
}

Expand All @@ -23,8 +47,6 @@ class KnexEnvironment extends NodeEnvironment {
await super.setup();
debug("super.setup()");

this.global.databaseName = `emjpm_test_${uid(16).toLowerCase()}`;

//

debug("new Knex");
Expand All @@ -36,10 +58,14 @@ class KnexEnvironment extends NodeEnvironment {

//

const connection: Object = typeof this.options.connection === 'string' ?
parseConnection(this.options.connection) :
this.options.connection;

this.global.knex = Knex({
...this.options,
connection: {
...this.options.connection,
...connection,
database: this.global.databaseName
}
});
Expand All @@ -61,7 +87,7 @@ class KnexEnvironment extends NodeEnvironment {

//

await Promise.all(this.destroyPromises)
await Promise.all(this.destroyPromises);

//

Expand All @@ -70,7 +96,7 @@ class KnexEnvironment extends NodeEnvironment {
debug("teardown");
}

runScript(script) {
runScript(script: Script) {
return super.runScript(script);
}
}
Expand Down
21 changes: 21 additions & 0 deletions packages/jest-environment-knex/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"target": "esnext",
"lib": [ "es2017" ],
"module": "commonjs",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "build",
"resolveJsonModule": true,
"typeRoots": [],
"types": [],
"rootDir": "src",
"strict": true
}
}
2 changes: 1 addition & 1 deletion packages/knex/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
testEnvironment: "./jest-environment-knex.js",
testEnvironment: "jest-environment-knex",
testEnvironmentOptions: require("./knexfile.js").test,
roots: ["<rootDir>/__test__/"]
};
3 changes: 2 additions & 1 deletion packages/knex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"pg": "^7.8.1"
},
"devDependencies": {
"jest": "^24.7.1"
"jest": "^24.7.1",
"jest-environment-knex": "^1.0.0"
}
}
19 changes: 12 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2697,7 +2697,7 @@ body-parser@1.18.3:

body-parser@^1.19.0:
version "1.19.0"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
resolved "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==
dependencies:
bytes "3.1.0"
Expand Down Expand Up @@ -2946,7 +2946,7 @@ bytes@3.0.0:

bytes@3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6"
resolved "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6"
integrity sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==

cacache@^10.0.4:
Expand Down Expand Up @@ -8259,7 +8259,7 @@ miller-rabin@^4.0.0:

mime-db@1.40.0:
version "1.40.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.40.0.tgz#a65057e998db090f732a68f6c276d387d4126c32"
resolved "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz#a65057e998db090f732a68f6c276d387d4126c32"
integrity sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==

mime-db@~1.37.0:
Expand All @@ -8276,7 +8276,7 @@ mime-types@^2.1.12, mime-types@~2.1.18, mime-types@~2.1.19:

mime-types@~2.1.24:
version "2.1.24"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.24.tgz#b6f8d0b3e951efb77dedeca194cff6d16f676f81"
resolved "https://registry.npmjs.org/mime-types/-/mime-types-2.1.24.tgz#b6f8d0b3e951efb77dedeca194cff6d16f676f81"
integrity sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==
dependencies:
mime-db "1.40.0"
Expand Down Expand Up @@ -10377,7 +10377,7 @@ qs@6.5.2, qs@~6.5.2:

qs@6.7.0:
version "6.7.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
resolved "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==

qs@^6.5.1:
Expand Down Expand Up @@ -10488,7 +10488,7 @@ raw-body@2.3.3:

raw-body@2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.4.0.tgz#a1ce6fb9c9bc356ca52e89256ab59059e13d0332"
resolved "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz#a1ce6fb9c9bc356ca52e89256ab59059e13d0332"
integrity sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==
dependencies:
bytes "3.1.0"
Expand Down Expand Up @@ -12491,7 +12491,7 @@ type-is@~1.6.16:

type-is@~1.6.17:
version "1.6.18"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
resolved "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
integrity sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==
dependencies:
media-typer "0.3.0"
Expand All @@ -12502,6 +12502,11 @@ typedarray@^0.0.6:
resolved "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@^3.4.5:
version "3.4.5"
resolved "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz#2d2618d10bb566572b8d7aad5180d84257d70a99"
integrity sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==

ua-parser-js@^0.7.18:
version "0.7.19"
resolved "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.19.tgz#94151be4c0a7fb1d001af7022fdaca4642659e4b"
Expand Down

0 comments on commit d178651

Please sign in to comment.