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

[BUG] Unable to Run Tests in with-jest Starter Repo #937

Open
3 tasks done
EmilMalanczak opened this issue Apr 11, 2024 · 0 comments
Open
3 tasks done

[BUG] Unable to Run Tests in with-jest Starter Repo #937

EmilMalanczak opened this issue Apr 11, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@EmilMalanczak
Copy link

What happened?

Description:

The with-jest starter repo seems to be encountering an issue where tests are not running as expected. Upon executing the test command, the test suite fails to initiate, halting any further progress.

The issue has 2 problems:

1. verbatimModuleSyntax

The first problem is caused by verbatimModuleSyntax rule set to true inside "plasmo/templates/tsconfig.base" which is extending all tsconfigs of with-* repositories. The following error happens:

> [email protected] test /Users/misit/dev-work/with-jest-issue
> jest

 PASS  tests/contents/index.test.ts
 FAIL  tests/contents/hello.test.tsx
  ● Test suite failed to run

    tests/contents/hello.test.tsx:1:10 - error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.

    1 import { describe, expect, it } from "@jest/globals"
               ~~~~~~~~
    tests/contents/hello.test.tsx:1:20 - error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.

    1 import { describe, expect, it } from "@jest/globals"
                         ~~~~~~
    tests/contents/hello.test.tsx:1:28 - error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.

    1 import { describe, expect, it } from "@jest/globals"
                                 ~~
    tests/contents/hello.test.tsx:2:10 - error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.

    2 import { render } from "@testing-library/react"
               ~~~~~~
    tests/contents/hello.test.tsx:4:8 - error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.

    4 import HelloContent from "~contents/hello"
             ~~~~~~~~~~~~

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

this problem is fixable in 2 ways:

  • set verbatimModuleSyntax to false
  • set type: "module" inside package.json

Not sure about decisions made behind current setup

2. tsx files SyntaxError: Unexpected token '<'

After fixing the first problem we get different error coming from hello.test.tsx file

> [email protected] test /Users/misit/dev-work/with-jest-issue
> jest

 PASS  tests/contents/index.test.ts
 FAIL  tests/contents/hello.test.tsx
  ● 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/misit/dev-work/with-jest-issue/tests/contents/hello.test.tsx:11
            (0, globals_1.expect)((0, react_1.render)(<hello_1.default />).getByText("Hello")).toBeTruthy();
                                                      ^

    SyntaxError: Unexpected token '<'

      at Runtime.createScriptFromCode (node_modules/.pnpm/[email protected]/node_modules/jest-runtime/build/index.js:1505:14)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.489 s, estimated 1 s
Ran all test suites.
 ELIFECYCLE  Test failed. See above for more details.

It seems jest doesn't see the jsx: "react-jsx" rule set for tsx files inside jest.config.mjs. Setting this rule at the tsconfig file fixes the issue but it's not ideal. After removing ? from regexes it seems to work correctly.

Original

transform: {
    "^.+\\.ts?$": [
      "ts-jest",
      {
        isolatedModules: true,
        useESM: true
      }
    ],
    "^.+\\.tsx?$": [
      "ts-jest",
      {
        useESM: true,
        tsconfig: {
          jsx: "react-jsx"
        }
      }
    ]
  }

After these changes I was able to run the tests

> [email protected] test /Users/misit/dev-work/with-jest-issue
> jest

 PASS  tests/contents/index.test.ts
 PASS  tests/contents/hello.test.tsx

Test Suites: 2 passed, 2 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.545 s, estimated 2 s
Ran all test suites.

Steps to Reproduce:

  • Create repo with starter pnpm create plasmo --with-jest with-jest-issue
  • Navigate to the project directory.
  • Run the test command pnpm test

Expected Behavior:

The test suite should execute without errors.

Actual Behavior:

The test suite fails to start, throwing an error message or terminating prematurely.

Additional Information:

This problem persists across different Node.js versions (tested latest v16, v18, v20)

Question:

Should I proceed with opening a Pull Request with minimal working example?

NOTE: I'm an expert at testing so please correct me if my solutions are wrong somehow 😄

Feel free to let me know if you need further clarification or if there's anything else I can assist with!

Version

Latest

What OS are you seeing the problem on?

MacOSX

What browsers are you seeing the problem on?

No response

Relevant log output

No response

(OPTIONAL) Contribution

  • I would like to fix this BUG via a PR

Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I checked the current issues for duplicate problems.
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

1 participant