Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to load .cjs config file #83

Merged
merged 8 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = [
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably name this .cjs to actually test the CJS feature?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok
I don't really understand how the tests are being run
Eslint is yelling at me:

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: tests/configs/cjs/jest-dynalite-config.cjs.
The file must be included in at least one of the projects provided.

I couldn't find what to change to make it happy

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add "*.cjs" to the tsconfig.esllint.json at the root of the repo. Atm I think it only includes .js and .ts

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand how the tests are being run
If you run yarn test you will see, they are effectively integration tests which ensure dynalite functions with an actual jest setup rather than unit.

The idea here is we want to ensure that .cjs configs do actually work and don't regress

{
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/javascript"));
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"]
}
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"sourceMap": true,
"declaration": true
},
"include": ["src/**/*.ts", "tests/**/*.ts", "types/**/*.d.ts"],
"include": [
"src/**/*.ts",
"tests/**/*.ts",
"tests/**/*.cjs",
freshollie marked this conversation as resolved.
Show resolved Hide resolved
"types/**/*.d.ts"
],
"exclude": ["dist"]
}