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

Problem with Jest #11

Closed
desoss opened this issue Mar 18, 2024 · 4 comments
Closed

Problem with Jest #11

desoss opened this issue Mar 18, 2024 · 4 comments

Comments

@desoss
Copy link

desoss commented Mar 18, 2024

I cannot make my unit tests work.

I get this error:

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

If I remove the lines of code that exploit dnum the tests work perfectly.

This is my jest.config.js:

module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node',
    roots: ["<rootDir>/src"],
    testMatch: ['**/*.test.(ts|tsx)'],
};
@bpierre
Copy link
Owner

bpierre commented Mar 19, 2024

Could you please post a reproduction somewhere? Thanks 🙏

@bpierre
Copy link
Owner

bpierre commented Mar 25, 2024

It seems to be an issue with ts-jest, that seems to expect CJS files: kulshekhar/ts-jest#937

Maybe you could try this? kulshekhar/ts-jest#937 (comment)

Closing the issue for now, but please reopen if you can provide a way for me to reproduce it.

@bpierre bpierre closed this as completed Mar 25, 2024
@caffeinum
Copy link

caffeinum commented Oct 16, 2024

I encounter this issue in a private repo. It's a nextjs typescript app that doesn't use ts-jest

jest.config.js:

import type { Config } from "jest";
import nextJest from "next/jest.js";

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: "./",
});

// Add any custom config to be passed to Jest
const config: Config = {
  coverageProvider: "v8",
  testEnvironment: "jsdom",
  // Add more setup options before each test is run
  setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
  moduleNameMapper: {
    "^~/(.*)$": "<rootDir>/src/$1",
  },
  maxWorkers: "50%",
  workerThreads: true,
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(config);

Error:

pnpm test

> [email protected] test /Users/caffeinum/wallet
> jest

 PASS  src/utils/parseTokenAmount.test.ts
 PASS  src/lib/javascript/uniqBy.test.ts
 PASS  src/utils/preprocessAmount.test.ts
 FAIL  src/utils/parseFiatAmount.test.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/caffeinum/wallet/node_modules/.pnpm/[email protected]/node_modules/dnum/dist/dnum.js:267
    export {
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      17 |
      18 | export function fiatToTokenAmount({
    > 19 |   value,
         |                   ^
      20 |   decimals,
      21 |   price = 1,
      22 | }: {

      at Runtime.createScriptFromCode (node_modules/.pnpm/[email protected]/node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (src/utils/parseFiatAmount.ts:19:55)
      at Object.<anonymous> (src/utils/parseFiatAmount.test.ts:5:26)

Test Suites: 2 failed, 4 passed, 6 total
Tests:       43 passed, 43 total
Snapshots:   0 total
Time:        1.017 s
Ran all test suites.
 ELIFECYCLE  Test failed. See above for more details.

@caffeinum
Copy link

caffeinum commented Oct 21, 2024

Update: I was able to solve this issue by switching from jest to vitest. Even though initially it gave the same error, The latter has a workaround that fixed the issue for me:

vi.mock("dnum", () => vi.importActual("dnum"));

I have added this line to the top of my test file and for some reason this imports the library correctly.

I didn't have many tests in my project, so re-writing them was basically finding the right config + adding vitest imports on top of each test file. This might not work for everyone.


Another solution we have found is to literally fork dnum and re-export it using @tanstack/config. See #17, thanks @freeatnet

Published to: https://www.npmjs.com/package/@sloikaxyz/dnum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants