Skip to content

Commit

Permalink
feat: allow for .ts config
Browse files Browse the repository at this point in the history
  • Loading branch information
freshollie committed Jun 30, 2021
1 parent 3eec397 commit 18d2c5a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ run the database only for suites which use it. Please see [advanced config](###A

## Config

In your jest project root (next to your `jest.config.js`), create a `jest-dynalite-config.js` with the tables schemas,
In your jest project root (next to your `jest.config.js`), create a `jest-dynalite-config.js` (or `.ts`) with the tables schemas,
and an optional `basePort` to run dynalite on:

```js
// use export default for ts based configs
module.exports = {
tables: [
{
Expand Down
31 changes: 23 additions & 8 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@ import { Config, TableConfig } from "./types";
import { isFunction } from "./utils";

export const CONFIG_FILE_NAME = "jest-dynalite-config.js";
export const CONFIG_FILE_NAME_TS = "jest-dynalite-config.ts";

export class NotFoundError extends Error {
constructor(dir: string) {
super(`Could not find '${CONFIG_FILE_NAME}' in dir ${dir}`);
super(
`Could not find '${CONFIG_FILE_NAME}' or '${CONFIG_FILE_NAME_TS}' in dir ${dir}`
);
}
}

let configDir: string =
process.env.JEST_DYNALITE_CONFIG_DIRECTORY || process.cwd();
let configFile: string =
process.env.JEST_DYNALITE_CONFIG_FILE || CONFIG_FILE_NAME;

const readConfig = (): Config => {
const configFile = resolve(configDir, CONFIG_FILE_NAME);
if (fs.existsSync(configFile)) {
const file = resolve(configDir, configFile);
if (fs.existsSync(file)) {
try {
return require(configFile); // eslint-disable-line import/no-dynamic-require, global-require
const importedConfig = require(file); // eslint-disable-line import/no-dynamic-require, global-require
if ("default" in importedConfig) {
return importedConfig.default;
}
return importedConfig;
} catch (e) {
throw new Error(
`Something went wrong reading your ${CONFIG_FILE_NAME}: ${e.message}`
`Something went wrong reading your ${configFile}: ${e.message}`
);
}
}
Expand All @@ -30,12 +39,18 @@ const readConfig = (): Config => {
};

export const setConfigDir = (directory: string): void => {
const configFile = resolve(directory, CONFIG_FILE_NAME);
if (!fs.existsSync(configFile)) {
throw new NotFoundError(resolve(configDir));
const foundFile = [CONFIG_FILE_NAME, CONFIG_FILE_NAME_TS].find((config) => {
const file = resolve(directory, config);
return fs.existsSync(file);
});

if (!foundFile) {
throw new NotFoundError(resolve(directory));
}

process.env.JEST_DYNALITE_CONFIG_FILE = foundFile;
process.env.JEST_DYNALITE_CONFIG_DIRECTORY = directory;
configFile = foundFile;
configDir = directory;
};

Expand Down
6 changes: 6 additions & 0 deletions tests/configs/typescript/jest-dynalite-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import tables from "./tables";

export default {
tables: () => tables,
basePort: 10500,
};
32 changes: 32 additions & 0 deletions tests/configs/typescript/tables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default [
{
TableName: "files",
KeySchema: [{ AttributeName: "id", KeyType: "HASH" }],
AttributeDefinitions: [{ AttributeName: "id", AttributeType: "S" }],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
},
{
TableName: "images",
KeySchema: [{ AttributeName: "url", KeyType: "HASH" }],
AttributeDefinitions: [{ AttributeName: "url", AttributeType: "S" }],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
data: [
{
url: "https://something.com/something/image.jpg",
width: 100,
height: 200,
},
{
url: "https://something.com/something/image2.jpg",
width: 150,
height: 250,
},
],
},
];
12 changes: 12 additions & 0 deletions tests/jest-tables-function-ts.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { join } = require("path");
const base = require("../jest.base");

module.exports = {
...base,
setupFiles: [join(__dirname, "setups/setupTablesFunctionTs.ts")],
setupFilesAfterEnv: [join(__dirname, "setups/setupAdvancedEnv.ts")],
displayName: {
name: "tables-function-ts",
color: "yellow",
},
};
5 changes: 5 additions & 0 deletions tests/setups/setupTablesFunctionTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { join } from "path";
import { setup } from "../../src";

// Setup with the config dir
setup(join(__dirname, "../configs/typescript"));

0 comments on commit 18d2c5a

Please sign in to comment.