spencermountain / spacetime

A lightweight javascript timezone library
http://spacetime.how/
Other
3.97k stars 185 forks source link

I'm running into issues testing spacetime #322

Closed naissa12 closed 2 years ago

naissa12 commented 2 years ago

I running jest test file and getting the following error:

```
  ● 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/test/dev/pm/client/node_modules/spacetime/src/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import Spacetime from './spacetime.js'
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

       6 | import search from '../../assets/images/search.svg';
       7 | import Dropdown from 'react-bootstrap/Dropdown';
    >  8 | import spacetime from "spacetime";
         | ^
       9 | import languages from "../../libraries/languages/language-list";
      10 | import { Rating } from 'react-simple-star-rating';
      11 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (src/components/Mentor/MentorList.js:8:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.097 s

I tried to fix it by adding the following to package.json, but that didn't work:

  "jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!spacetime)"
    ]
  },


Can someone please help?
spencermountain commented 2 years ago

hey @naissa12 spacetime uses es-modules on main, but builds common-js files as well. You can see the setup here. I think this mostly follows best-practice. I'm not sure how jest is configured. cheers

svey commented 2 years ago

@naissa12 did you find a solution— I just hit the same issue upgrading from 6.12.3 -> 7.1.2

svey commented 2 years ago

@naissa12 did you find a solution— I just hit the same issue upgrading from 6.12.3 -> 7.1.2

Can confirm that reverting from back to 6.12.3 fixes the issue in jest.

svey commented 2 years ago

@spencermountain probably related to this: [change] - convert to ES-modules, + exports fields

I'm not ready to open a new issue yet but it's odd to me as we just tried to bump the version for the: [change] - update to 2022 DST dates

svey commented 2 years ago

@spencermountain — I fixed it on my end; please disregard ^. And THANK YOU for all your hard work; My feedback for releases would be isolate DST updates from everything else— I assume most people version bump the package for the DST updates (or they're responsible and properly manage their packages 😂 ), which when rolled in with other things might cause some folks 🙋‍♂️ a bit of a headache (especially if they've waited until the last minute like yours truly). But seriously appreciate all that you do.

@naissa12 try updating

package.json:

deps

"spacetime": "^7.1.2",

dev deps

"@babel/core": "^7.17.5",
"@babel/preset-env": "^7.16.11",

jest.config.js:

transformIgnorePatterns: [
   '<rootDir>/node_modules/(?!spacetime)'
 ],

it may be as simple as adding <rootDir>/ for you, or you may need to play around with bumping additional modules surrounding babel and jest— best of luck!

I hope no one else wastes 6 hours on this 🙉 🙊 🙈

Oatelaus commented 1 year ago

I came across this using when using jest@27. Updating both ts-jest and jest to ^28 seems to have fixed this for me.

cjxe commented 10 months ago

For people who couldn't solve by adding the line:

transformIgnorePatterns: [ '/node_modules/(?!spacetime)' ],

This doesn't work if your babel file is saved as .babelrc

Further information: https://stackoverflow.com/a/54656593/12959962

korczu commented 7 months ago

Hi, I tried the above tips, but unfortunately nothing helped and still getting same module issue with spacetime. I am using spacetime with react-timezone-component. Any ideas what I am doing wrong?

  1. jest.config.json
    
    {
    "transformIgnorePatterns": ["<rootDir>/node_modules/(?!spacetime)"]
    }
2. babel.config.json

{ "presets": [ ["@babel/preset-env", { "targets": { "node": "current" } }], "@babel/preset-typescript", "@babel/preset-react" ] }


3. package.json
devDep:

"@babel/core": "^7.23.5", "@babel/preset-env": "^7.23.5", "@babel/preset-react": "^7.22.15", "@babel/preset-typescript": "^7.23.3", "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0",



   deps:
`   "spacetime": "^7.4.8"`