tatethurston / nextjs-routes

Type safe routing for Next.js
MIT License
557 stars 21 forks source link

SyntaxError: Unexpected token 'export' when test with jest #127

Closed panudetjt closed 1 year ago

panudetjt commented 1 year ago

Hello,

I have Nextjs 13.1.6 and Jest for building my app.

but while testing I use function route() from nextjs-routes it cause error and make test suite failed.

this is some sample error I got

/node_modules/.pnpm/nextjs-routes@1.0.8_next@13.1.6/node_modules/nextjs-routes/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export function route(r) {
                                                                                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

How I can solve this problem thank you :)

tatethurston commented 1 year ago

@panudetjt This should already work. Could you compare your project setup to the typescript example to see what is different?

panudetjt commented 1 year ago

@panudetjt This should already work. Could you compare your project setup to the typescript example to see what is different?

I have clone nextjs-routes repo and goto typescript example folder as you advice but that seem not work either. I have tried both pnpm and npm that I guess maybe because of package manager. both of them not work.

This following is npm:

➜ npm run test

> typescript@0.0.1 test
> jest

 FAIL  __test__/index.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/panudet/Desktop/projects/nextjs-routes/examples/typescript/node_modules/nextjs-routes/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export function route(r) {
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      10 |       .then((r) => r.json())
      11 |       .then(console.log);
    > 12 |   }, []);
         |                       ^
      13 |
      14 |   return (
      15 |     <>

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1495:14)
      at Object.<anonymous> (pages/index.tsx:12:23)
      at Object.<anonymous> (__test__/index.test.tsx:7:53)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.55 s
Ran all test suites.

this one this pnpm

➜ pnpm test    

> typescript@0.0.1 test /Users/panudet/Desktop/projects/nextjs-routes/examples/typescript
> jest

 FAIL  __test__/index.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/panudet/Desktop/projects/nextjs-routes/node_modules/.pnpm/nextjs-routes@1.0.8_next@13.2.4/node_modules/nextjs-routes/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export function route(r) {
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      10 |       .then((r) => r.json())
      11 |       .then(console.log);
    > 12 |   }, []);
         |                       ^
      13 |
      14 |   return (
      15 |     <>

      at Runtime.createScriptFromCode (../../node_modules/.pnpm/jest-runtime@29.5.0/node_modules/jest-runtime/build/index.js:1495:14)
      at Object.<anonymous> (pages/index.tsx:12:23)
      at Object.<anonymous> (__test__/index.test.tsx:7:53)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.593 s
Ran all test suites.
 ELIFECYCLE  Test failed. See above for more details.
tatethurston commented 1 year ago

Thanks @panudetjt I think I see what's going on here. I'll publish a commonjs export and then this will "just work" under jest.

panudetjt commented 1 year ago

Thanks @panudetjt I think I see what's going on here. I'll publish a commonjs export and then this will "just work" under jest.

I sincerely appreciate your hard work and dedication in helping me. 🙏

tatethurston commented 1 year ago

@panudetjt I’ve published 1.0.9 with the fix let me know if you run into any issues