Skip to content

Commit

Permalink
Merge 23aebdd into b3b71d9
Browse files Browse the repository at this point in the history
  • Loading branch information
revmischa committed May 31, 2022
2 parents b3b71d9 + 23aebdd commit a3fa4fb
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 12 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
],
parser: "@typescript-eslint/parser",
parserOptions: {
extraFileExtensions: [".cjs"],
warnOnUnsupportedTypeScriptVersion: false,
project: "tsconfig.eslint.json",
},
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ 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` (or `.ts`) with the tables schemas,
In your jest project root (next to your `jest.config.js`), create a `jest-dynalite-config.js` (or `.cjs` or `.ts`) with the tables schemas,
and an optional `basePort` to run dynalite on:

```js
Expand Down
2 changes: 1 addition & 1 deletion src/__snapshots__/environment.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`Environment should throw an error if config file could not be located 1`] = `
"
jest-dynalite could not find \\"jest-dynalite-config.js\\" or \\"jest-dynalite-config.ts\\" in the jest <rootDir> (somebaddirectory).
jest-dynalite could not find \\"jest-dynalite-config.js\\" or \\"jest-dynalite-config.cjs\\" or \\"jest-dynalite-config.ts\\" in the jest <rootDir> (somebaddirectory).
If you didn't intend to be using this directory for the config, please specify a custom
directory: https://github.com/freshollie/jest-dynalite/#advanced-setup
Expand Down
18 changes: 11 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Config, TableConfig } from "./types";
import { isFunction } from "./utils";

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

export class NotFoundError extends Error {
Expand All @@ -20,13 +21,16 @@ if (!process.env.JEST_DYNALITE_CONFIG_DIRECTORY) {

const findConfigOrError = (
directory: string
): typeof CONFIG_FILE_NAME | typeof CONFIG_FILE_NAME_TS => {
const foundFile = ([CONFIG_FILE_NAME, CONFIG_FILE_NAME_TS] as const).find(
(config) => {
const file = resolve(directory, config);
return fs.existsSync(file);
}
);
):
| typeof CONFIG_FILE_NAME
| typeof CONFIG_FILE_NAME_CJS
| typeof CONFIG_FILE_NAME_TS => {
const foundFile = (
[CONFIG_FILE_NAME, CONFIG_FILE_NAME_CJS, CONFIG_FILE_NAME_TS] as const
).find((config) => {
const file = resolve(directory, config);
return fs.existsSync(file);
});

if (!foundFile) {
throw new NotFoundError(resolve(directory));
Expand Down
9 changes: 7 additions & 2 deletions src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import type {
} from "@jest/environment";
import setup from "./setup";
import { start, stop } from "./db";
import { CONFIG_FILE_NAME, CONFIG_FILE_NAME_TS, NotFoundError } from "./config";
import {
CONFIG_FILE_NAME,
CONFIG_FILE_NAME_CJS,
CONFIG_FILE_NAME_TS,
NotFoundError,
} from "./config";

class DynaliteEnvironment extends NodeEnvironment {
constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) {
Expand All @@ -27,7 +32,7 @@ class DynaliteEnvironment extends NodeEnvironment {
} catch (e) {
if (e instanceof NotFoundError) {
throw new Error(`
jest-dynalite could not find "${CONFIG_FILE_NAME}" or "${CONFIG_FILE_NAME_TS}" in the jest <rootDir> (${rootDir}).
jest-dynalite could not find "${CONFIG_FILE_NAME}" or "${CONFIG_FILE_NAME_CJS}" or "${CONFIG_FILE_NAME_TS}" in the jest <rootDir> (${rootDir}).
If you didn't intend to be using this directory for the config, please specify a custom
directory: https://github.com/freshollie/jest-dynalite/#advanced-setup
Expand Down
6 changes: 6 additions & 0 deletions tests/configs/cjs/jest-dynalite-config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const tables = require("./tables");

module.exports = {
tables: () => tables,
basePort: 10500,
};
32 changes: 32 additions & 0 deletions tests/configs/cjs/tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = [
{
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-cjs.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { join } from "path";
import base from "../jest.base";

export default {
...base,
setupFiles: [join(__dirname, "setups/setupCjs.ts")],
setupFilesAfterEnv: [join(__dirname, "setups/setupAdvancedEnv.ts")],
displayName: {
name: "cjs",
color: "yellow",
},
};
5 changes: 5 additions & 0 deletions tests/setups/setupCjs.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/cjs"));
2 changes: 1 addition & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["**/*.js", "**/*.ts", ".jest/**/*.js", ".eslintrc.js"]
"include": ["**/*.js", "**/*.ts", "**/*.cjs", ".jest/**/*.js", ".eslintrc.js"]
}

0 comments on commit a3fa4fb

Please sign in to comment.