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

Typescript based configuration doesn't work when using simple preset setup #73

Open
derolf opened this issue Nov 4, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@derolf
Copy link

derolf commented Nov 4, 2021

I am trying to load a typescript config with jest-dynalite.

    Something went wrong reading your jest-dynalite-config.ts: Unexpected token 'export'

      at readConfig (../../node_modules/.pnpm/jest-dynalite@3.4.4_c6ac6979dbeefcbbac9c0114ae514571/node_modules/jest-dynalite/dist/config.js:42:15)
      at async TestScheduler.scheduleTests (../../node_modules/.pnpm/@jest+core@27.3.1/node_modules/@jest/core/build/TestScheduler.js:333:13)

Everything else in my project is TypeScript and works fine. I am having a babel.config.json.

@freshollie
Copy link
Owner

What's your jest setup, are you using babel jest? Can I see you babel config?

@freshollie
Copy link
Owner

Also, I assume you are using the preset? It may be that the .ts version of the config only works with the more advanced setups (with setupEnv's). Can you try that?

@derolf
Copy link
Author

derolf commented Nov 4, 2021

Thanks for your quick response. Meanwhile, I am using dynalite directly (without jest-dynalite).

I have a setup.ts:

import dynalite from "dynalite";
import * as dynamodb from "@aws-sdk/client-dynamodb";
import * as db from "../src/db";

const dynaliteServer = dynalite({ createTableMs: 0, deleteTableMs: 0, updateTableMs: 0 });

process.env.AWS_REGION = "test";
process.env.AWS_ACCESS_KEY_ID = "test";
process.env.AWS_SECRET_ACCESS_KEY = "test";

beforeAll(async () => {
  await new Promise<void>((resolve) => dynaliteServer.listen(0, () => resolve()));
  const addr = dynaliteServer.address();
  if (!addr || typeof addr === "string") {
    // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
    throw new Error(`${addr}`);
  }

  db.setDdb(new dynamodb.DynamoDB({ endpoint: `http://localhost:${addr.port}` }));
});

beforeEach(async () => {
  await db.ddb.deleteTable({ TableName: "table" }).catch((_) => false);
  await db.ddb.createTable({
    TableName: "table",
    KeySchema: [
      { AttributeName: "pk", KeyType: "HASH" },
      { AttributeName: "id", KeyType: "RANGE" },
    ],
    AttributeDefinitions: [
      { AttributeName: "pk", AttributeType: "S" },
      { AttributeName: "id", AttributeType: "S" },
    ],
    BillingMode: "PAY_PER_REQUEST",
  });
});

afterAll(async () => {
  await new Promise<void>((resolve) => dynaliteServer.close(() => resolve()));
});

and just link it with: setupFilesAfterEnv: [path.join(__dirname, "test-helpers/setup.ts")], in my jest.config.js.

@derolf derolf closed this as completed Nov 4, 2021
@freshollie
Copy link
Owner

Cool, you can recreate this with the advanced setup.

I'm going to leave this open whilst I work out what to do with .ts configs

@freshollie freshollie reopened this Nov 4, 2021
@freshollie freshollie changed the title Something went wrong reading your jest-dynalite-config.ts: Unexpected token 'export' TS config doesn't work when using simple preset setup Nov 4, 2021
@freshollie freshollie changed the title TS config doesn't work when using simple preset setup Typescript based configuration doesn't work when using simple preset setup Nov 4, 2021
@freshollie freshollie added the bug Something isn't working label Nov 5, 2021
@mikemaccana
Copy link

As a suggestion it might be better to allow ES6 module configuration in the 'Simple' setup (and deprecate CommonJS) in the next major release.

@freshollie
Copy link
Owner

freshollie commented May 4, 2022

I'm not sure that will help? I think the issue is jest doesn't run the modules it imports, when setting up environment, through the transpiler? I think even if I allowed es6 modules jest wouldn't remove any type information from the file so would fail to run.

@revmischa
Copy link
Contributor

revmischa commented May 27, 2022

I get this error too using ES modules and jest-dynalite-config.ts:
Something went wrong reading your jest-dynalite-config.ts: Unexpected token 'export'

I tried renaming the file to jest-dynalite-config.js -
I get the error:

    Something went wrong reading your jest-dynalite-config.js: require() of ES Module /Users/cyber/dev/tombo/sequencer/jest-dynalite-config.js from /Users/cyber/dev/sequencer/node_modules/jest-dynalite/dist/config.js not supported.
    Instead change the require of jest-dynalite-config.js in /Users/cyber/dev/sequencer/node_modules/jest-dynalite/dist/config.js to a dynamic import() which is available in all CommonJS modules.

      at readConfig (node_modules/jest-dynalite/dist/config.js:42:15)
      at getDynalitePort (node_modules/jest-dynalite/dist/config.js:52:33)
      at exports.default (node_modules/jest-dynalite/dist/setup.js:6:47)
      at new DynaliteEnvironment (node_modules/jest-dynalite/dist/environment.js:22:33)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants