Skip to content

Commit

Permalink
fix: improve jest test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Oct 29, 2021
1 parent aea68cc commit 303f15e
Show file tree
Hide file tree
Showing 86 changed files with 451 additions and 718 deletions.
63 changes: 58 additions & 5 deletions jest.config.base.js
@@ -1,13 +1,10 @@
const findUp = require("find-up");
const { basename } = require("path");
const merge = require("merge");
const tsPreset = require("ts-jest/presets/js-with-babel/jest-preset");
const { version } = require("@webiny/cli/package.json");

process.env.DB_TABLE = "DynamoDB";
process.env.DB_TABLE_ELASTICSEARCH = "ElasticSearchStream";
process.env.WEBINY_VERSION = version;

module.exports = ({ path }, presets = []) => {
module.exports = function ({ path }, presets = []) {
const name = basename(path);

// Enables us to run tests of only a specific type (for example "integration" or "e2e").
Expand Down Expand Up @@ -43,3 +40,59 @@ module.exports = ({ path }, presets = []) => {
coverageReporters: ["html"]
});
};

process.env.DB_TABLE = "DynamoDB";
process.env.DB_TABLE_ELASTICSEARCH = "ElasticsearchStream";
process.env.WEBINY_VERSION = version;

const createDynaliteTables = () => {
return {
tables: [
{
TableName: process.env.DB_TABLE,
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" },
{ AttributeName: "SK", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" },
{ AttributeName: "GSI1_PK", AttributeType: "S" },
{ AttributeName: "GSI1_SK", AttributeType: "S" }
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
GlobalSecondaryIndexes: [
{
IndexName: "GSI1",
KeySchema: [
{ AttributeName: "GSI1_PK", KeyType: "HASH" },
{ AttributeName: "GSI1_SK", KeyType: "RANGE" }
],
Projection: {
ProjectionType: "ALL"
},
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
}
]
},
{
TableName: process.env.DB_TABLE_ELASTICSEARCH,
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" },
{ AttributeName: "SK", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" }
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 }
}
],
basePort: 8000
};
};

module.exports.createDynaliteTables = createDynaliteTables;
Expand Up @@ -10,15 +10,27 @@ jestDynalite.setup(path.resolve(__dirname, "../../"));
*/
beforeAll(async () => {
await jestDynalite.startDb();
if (typeof __beforeAll === "function") {
await __beforeAll();
}
});

beforeEach(async () => {
await jestDynalite.createTables();
if (typeof __beforeEach === "function") {
await __beforeEach();
}
});
afterEach(async () => {
await jestDynalite.deleteTables();
if (typeof __afterEach === "function") {
await __afterEach();
}
});

afterAll(async () => {
await jestDynalite.stopDb();
if (typeof __afterAll === "function") {
await __afterAll();
}
});
37 changes: 2 additions & 35 deletions packages/api-admin-users-cognito-so-ddb/jest-dynalite-config.js
@@ -1,35 +1,2 @@
module.exports = {
tables: [
{
TableName: process.env.DB_TABLE,
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" },
{ AttributeName: "SK", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" },
{ AttributeName: "GSI1_PK", AttributeType: "S" },
{ AttributeName: "GSI1_SK", AttributeType: "S" }
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
GlobalSecondaryIndexes: [
{
IndexName: "GSI1",
KeySchema: [
{ AttributeName: "GSI1_PK", KeyType: "HASH" },
{ AttributeName: "GSI1_SK", KeyType: "RANGE" }
],
Projection: {
ProjectionType: "ALL"
},
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
}
]
}
],
basePort: 8000
};
const { createDynaliteTables } = require("../../jest.config.base");
module.exports = createDynaliteTables();
56 changes: 20 additions & 36 deletions packages/api-file-manager-ddb-es/__tests__/__api__/environment.js
Expand Up @@ -10,6 +10,9 @@ const NodeEnvironment = require("jest-environment-node");
const elasticsearchDataGzipCompression =
require("@webiny/api-elasticsearch/plugins/GzipCompression").default;
const { ContextPlugin } = require("@webiny/handler/plugins/ContextPlugin");
const {
elasticIndexManager
} = require("@webiny/project-utils/testing/helpers/elasticIndexManager");
/**
* For this to work it must load plugins that have already been built
*/
Expand All @@ -21,24 +24,6 @@ if (typeof plugins !== "function") {

const ELASTICSEARCH_PORT = process.env.ELASTICSEARCH_PORT || "9200";

const getStorageOperationsPlugins = ({ documentClient, elasticsearchClientContext }) => {
return () => {
const pluginsValue = plugins();
const dbPluginsValue = dbPlugins({
table: process.env.DB_TABLE,
driver: new DynamoDbDriver({
documentClient
})
});
return [
elasticsearchDataGzipCompression(),
...pluginsValue,
...dbPluginsValue,
elasticsearchClientContext
];
};
};

class FileManagerTestEnvironment extends NodeEnvironment {
async setup() {
await super.setup();
Expand Down Expand Up @@ -68,29 +53,28 @@ class FileManagerTestEnvironment extends NodeEnvironment {
});
simulateStream(documentClient, createHandler(simulationContext, dynamoToElastic()));

const clearEsIndices = async () => {
return elasticsearchClient.indices.delete({
index: "_all"
});
};
/**
* This is a global function that will be called inside the tests to get all relevant plugins, methods and objects.
*/
this.global.__getStorageOperationsPlugins = () => {
return getStorageOperationsPlugins({
elasticsearchClientContext,
documentClient
});
return () => {
const pluginsValue = plugins();
const dbPluginsValue = dbPlugins({
table: process.env.DB_TABLE,
driver: new DynamoDbDriver({
documentClient
})
});
return [
elasticsearchDataGzipCompression(),
...pluginsValue,
...dbPluginsValue,
elasticsearchClientContext
];
};
};
this.global.__beforeEach = async () => {
await clearEsIndices();
return elasticsearchClient.indices.create({
index: "root-file-manager"
});
};
this.global.__afterEach = clearEsIndices;
this.global.__beforeAll = clearEsIndices;
this.global.__afterAll = clearEsIndices;

elasticIndexManager(this.global, elasticsearchClient);
}
}

Expand Down
Expand Up @@ -10,19 +10,27 @@ jestDynalite.setup(path.resolve(__dirname, "../../"));
*/
beforeAll(async () => {
await jestDynalite.startDb();
//await __beforeAll();
if (typeof __beforeAll === "function") {
await __beforeAll();
}
});

beforeEach(async () => {
await jestDynalite.createTables();
await __beforeEach();
if (typeof __beforeEach === "function") {
await __beforeEach();
}
});
afterEach(async () => {
await jestDynalite.deleteTables();
await __afterEach();
if (typeof __afterEach === "function") {
await __afterEach();
}
});

afterAll(async () => {
await jestDynalite.stopDb();
//await __afterAll();
if (typeof __afterAll === "function") {
await __afterAll();
}
});
32 changes: 3 additions & 29 deletions packages/api-file-manager-ddb-es/jest-dynalite-config.js
@@ -1,29 +1,3 @@
module.exports = {
tables: [
{
TableName: process.env.DB_TABLE,
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" },
{ AttributeName: "SK", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" }
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 }
},
{
TableName: process.env.DB_TABLE_ELASTICSEARCH,
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" },
{ AttributeName: "SK", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" }
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 }
}
],
basePort: 8000
};
const { createDynaliteTables } = require("../../jest.config.base");

module.exports = createDynaliteTables();
3 changes: 1 addition & 2 deletions packages/api-file-manager-ddb-es/src/definitions/table.ts
@@ -1,12 +1,11 @@
import { Table } from "dynamodb-toolbox";
import configurations from "~/operations/configurations";
import { getDocumentClient, getTable } from "~/operations/utils";
import { FileManagerContext } from "@webiny/api-file-manager/types";

export default (params: { context: FileManagerContext }): Table => {
const { context } = params;
return new Table({
name: configurations.db().table || getTable(context),
name: process.env.DB_TABLE || getTable(context),
partitionKey: "PK",
sortKey: "SK",
DocumentClient: getDocumentClient(context)
Expand Down
Expand Up @@ -2,7 +2,7 @@ import { FileManagerContext } from "@webiny/api-file-manager/types";

export default {
db: () => ({
table: process.env.DB_TABLE_FILE_MANGER || process.env.DB_TABLE,
table: process.env.DB_TABLE,
keys: [
{
primary: true,
Expand Down
31 changes: 13 additions & 18 deletions packages/api-file-manager-ddb/__tests__/__api__/environment.js
Expand Up @@ -3,6 +3,7 @@ const { DynamoDbDriver } = require("@webiny/db-dynamodb");
const dynamoDbPlugins = require("@webiny/db-dynamodb/plugins").default;
const { DocumentClient } = require("aws-sdk/clients/dynamodb");
const NodeEnvironment = require("jest-environment-node");

/**
* For this to work it must load plugins that have already been built
*/
Expand All @@ -12,21 +13,6 @@ if (typeof plugins !== "function") {
throw new Error(`Loaded plugins file must export a function that returns an array of plugins.`);
}

const getStorageOperationsPlugins = ({ documentClient }) => {
return () => {
return [
...dynamoDbPlugins(),
...plugins(),
...dbPlugins({
table: process.env.DB_TABLE,
driver: new DynamoDbDriver({
documentClient
})
})
];
};
};

class FileManagerTestEnvironment extends NodeEnvironment {
async setup() {
await super.setup();
Expand All @@ -43,9 +29,18 @@ class FileManagerTestEnvironment extends NodeEnvironment {
* This is a global function that will be called inside the tests to get all relevant plugins, methods and objects.
*/
this.global.__getStorageOperationsPlugins = () => {
return getStorageOperationsPlugins({
documentClient
});
return () => {
return [
...dynamoDbPlugins(),
...plugins(),
...dbPlugins({
table: process.env.DB_TABLE,
driver: new DynamoDbDriver({
documentClient
})
})
];
};
};
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/api-file-manager-ddb/__tests__/__api__/setupAfterEnv.js
Expand Up @@ -10,15 +10,27 @@ jestDynalite.setup(path.resolve(__dirname, "../../"));
*/
beforeAll(async () => {
await jestDynalite.startDb();
if (typeof __beforeAll === "function") {
await __beforeAll();
}
});

beforeEach(async () => {
await jestDynalite.createTables();
if (typeof __beforeEach === "function") {
await __beforeEach();
}
});
afterEach(async () => {
await jestDynalite.deleteTables();
if (typeof __afterEach === "function") {
await __afterEach();
}
});

afterAll(async () => {
await jestDynalite.stopDb();
if (typeof __afterAll === "function") {
await __afterAll();
}
});

0 comments on commit 303f15e

Please sign in to comment.