masroorejaz / react-simple-captcha

A very simple and powerful captcha for ReactJS
40 stars 19 forks source link

Unable write test cases using jest #12

Closed nmanthena18 closed 2 years ago

nmanthena18 commented 2 years ago

Unable to run the test cases after importing it into the component getting below error cannot use import statement outside a module react js

nmanthena18 commented 2 years ago

If you add simple test case app component, you will see the below errors.

src/App.test.js ● Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not 

plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
 • 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/en/configuration.html

Details:

D:\captchaapp\node_modules\react-simple-captcha\react-simple-captcha.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { Component } from 'react';
                                                                                         ^^^^^^
SyntaxError: Cannot use import statement outside a module

  1 | import logo from './logo.svg';
  2 | import './App.css';
> 3 | import { loadCaptchaEnginge, LoadCanvasTemplate, LoadCanvasTemplateNoReload, validateCaptcha } from 'react-simple-captcha';
    | ^
  4 | import React, { useEffect } from 'react';
  5 | function App() {
  6 |

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
  at Object.<anonymous> (src/App.js:3:1)
masroorejaz commented 2 years ago

Haven't unit test it with jest. Otherwise it is working absolutely fine in Reactjs app.

Also why are you unit testing it using jest?

nmanthena18 commented 2 years ago

I am writing test for my component which includes react-simple-captcha component, in this case we are not able to proceed.

masroorejaz commented 2 years ago

I will need to test it on jest and then maybe rewrite some code. But currently I am occupied with workload.

nmanthena18 commented 2 years ago

Sure thanks

nmanthena18 commented 2 years ago

this issue is fixed now? any tag or version please.

masroorejaz commented 2 years ago

No it is not fixed as of yet because I don't have time right now to work on jest version.

Will definitely work on it in the future!