When I run `npm test` inside the `frontend` folder...
# help
p
When I run
npm test
inside the
frontend
folder, which is a React project, it works and the test pass. But when I run it in the root of my project, I get an error when running the same tests.
The error looks like this:
Copy code
Preparing your SST app
 FAIL  frontend/src/App.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:

    C:\Users\pupeno\Code\ww3\frontend\src\App.test.tsx:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import React from "react"
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest/node_modules/jest-runtime/build/index.js:1728:14)

 PASS  test/StorageStack.test.ts (6.791 s)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        8.474 s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
f
Hey @Pablo Fernandez, it seems the SST test succeeded, but the frontend test failed.
Can you try running
jest
directly and see if both tests pass?
p
I was expecting it to be the same, but with
jest
both tests fail:
Copy code
PS C:\Users\pupeno\Code\ww3> jest
 FAIL  test/StorageStack.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:

    C:\Users\pupeno\Code\ww3\test\StorageStack.test.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { Template } from "aws-cdk-lib/assertions";
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest/node_modules/jest-runtime/build/index.js:1728:14)

 FAIL  frontend/src/App.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:

    SyntaxError: C:\Users\pupeno\Code\ww3\frontend\src\App.test.tsx: Support for the experimental syntax 'jsx' isn't currently enabled (6:10):

      4 |
      5 | test("renders learn react link", () => {
    > 6 |   render(<App/>)
        |          ^
      7 |   const linkElement = screen.getByText(/Has WW3 Started Yet/i)
      8 |   expect(linkElement).toBeInTheDocument()
      9 | })

    Add @babel/preset-react (<https://git.io/JfeDR>) to the 'presets' section of your Babel config to enable transformation.
    If you want to leave it as-is, add @babel/plugin-syntax-jsx (<https://git.io/vb4yA>) to the 'plugins' section to enable parsing.

      at Parser._raise (node_modules/@babel/parser/src/parser/error.js:150:45)
      at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:145:17)
      at Parser.expectOnePlugin (node_modules/@babel/parser/src/parser/util.js:214:18)
      at Parser.parseExprAtom (node_modules/@babel/parser/src/parser/expression.js:1238:16)
      at Parser.parseExprSubscripts (node_modules/@babel/parser/src/parser/expression.js:682:23)
      at Parser.parseUpdate (node_modules/@babel/parser/src/parser/expression.js:662:21)
      at Parser.parseMaybeUnary (node_modules/@babel/parser/src/parser/expression.js:633:23)
      at Parser.parseMaybeUnaryOrPrivate (node_modules/@babel/parser/src/parser/expression.js:388:14)
      at Parser.parseExprOps (node_modules/@babel/parser/src/parser/expression.js:398:23)
      at Parser.parseMaybeConditional (node_modules/@babel/parser/src/parser/expression.js:356:23)

Test Suites: 2 failed, 2 total
Tests:       0 total
Snapshots:   0 total
Time:        1.495 s
Ran all test suites.
PS C:\Users\pupeno\Code\ww3>
f
Ah ur tests are in TS.
@thdxr how do u run jest with TS tests again from the cli again?
t
I use esbuild-runner
But if it's ESM it needs more config
Running tests in the root isn't expected to work afaik
f
@Pablo Fernandez it makes more sense to ignore the
frontend
tests when running
npm test
in the root. You can do that by adding this
jest
config in your package.json.
Copy code
"jest": {
    "testPathIgnorePatterns": [
      "<rootDir>/frontend"
    ]
  },
And run the
frontend
tests separately.