I am try to run jest test case in react native and it always fails. Terminal print this error "Jest encountered an unexpected token". I'm really new to jest, I have tried several hours to solve this problem, but couldn't find a way. Please help me to solve this.
Here is my code: -
import 'react-native';
import React from 'react';
import LoginScreen from '../pages/LoginScreen'
import renderer from 'react-test-renderer';
// snapshot testing
// update snapsshot to use npm test -- -u
// test('MainScreen snapShot', ()=>{
// const snap = renderer.create(
// <MainScreen />
// ).toJSON();
// expect(snap).toMatchSnapshot();
// });
it('fucntion test', () =>{
let MainData = renderer.create(<LoginScreen />).getInstance();
MainData.change(2)
expect(MainData.change(2)).toEqual(20)
})
Package.json :
{
"name": "App_name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.3.1",
"react-native": "0.55.4",
"react-native-fcm": "^16.0.0",
"react-native-loading-spinner-overlay": "^0.5.2",
"react-native-looped-carousel": "^0.1.13",
"react-native-navigation": "^1.1.469",
"react-native-pathjs-charts": "0.0.34",
"react-native-swiper": "^1.5.13",
"react-native-vector-icons": "^4.6.0",
"react-redux": "^5.0.7",
"redux": "^4.0.0"
},
"devDependencies": {
"babel-jest": "23.0.1",
"babel-preset-react-native": "4.0.0",
"jest": "^23.1.0",
"react-test-renderer": "16.3.1"
},
"jest": {
"preset": "react-native"
}
}
Error is :
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:
• 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://facebook.github.io/jest/docs/en/configuration.html
Details:
15 |
16 | it('fucntion test', () =>{
> 17 | let MainData = renderer.create(<LoginScreen />).getInstance();
| ^
18 | MainData.change(2)
19 |
20 | expect(MainData.change(2)).toEqual(20)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 6.807s
Ran all test suites.
npm ERR! Test failed. See above for more details.
Thanks.
I saw something similar, try creating a .babelrc in the root of your project with this:
{
"presets": ["react-native"]
}
Related
Stack:
Vue 3 (Options API)
Vite
TailwindCSS
Context:
I've been working on this app for months. If I changed something minor such as a computed property or style, the component would update but the page wouldn't. As of today, suddenly the entire app reloads on every save regardless of what changes, including adding a single whitespace which is removed via auto-format.
New Warning:
Component options are wraped by defineComponent() internally to
support intellisense on IDE for backward compatible, but this is
an hacking which lead to this component type inconsistent with
same script code on .js / .ts. Recommended wrap component options
by Vue.extends() or defineComponent(). Or you can configure
experimentalShamefullySupportOptionsApi: true / false in
vueCompilerOptions property in tsconfig / jsconfig to disable
this warning.
This warning seemingly came out of nowhere and I can't find any information about it online besides a reference to it in the newest Volar relase notes. I've tried downgrading to an older version and then disabling it entirely. This didn't work.
Config
// vite.config.js
import { defineConfig } from "vite";
import vue from "#vitejs/plugin-vue";
import path from "path";
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
"#": path.resolve(__dirname, "./src"),
},
},
});
Note that I use npm run serve which would normally be npm run dev. I swapped them because of muscle memory. This shouldn't be an issue but it's noteworthy.
// package.json
{
...
"scripts": {
"serve": "vite --host",
"build": "vite build",
"dev": "vite preview"
},
"dependencies": {
"#headlessui/vue": "^1.4.1",
"#heroicons/vue": "^1.0.4",
"#popperjs/core": "^2.11.0",
"#tailwindcss/forms": "^0.3.3",
"vue": "^3.2.6",
"vue-router": "^4.0.11",
"vuex": "^4.0.2",
"vuex-persist": "^3.1.3"
},
"devDependencies": {
"#vitejs/plugin-vue": "^1.6.1",
"#vue/compiler-sfc": "^3.2.6",
"autoprefixer": "^10.3.4",
"postcss": "^8.3.6",
"stylelint-config-recommended": "^6.0.0",
"tailwindcss": "^2.2.15",
"vite": "^2.5.4"
}
}
After several hours of debugging, it turns out that I had NODE_ENV=production leftover from testing last night.
you can try Temporarily disable Volar plugin, it won't show waining tip
I also encountered this problem, and then I disabled the volar plug-in so that there is no longer this warning message, but I do not know z there is no harm
Add the following entry to your project's jsconfig.json and you're good to go:
"vueCompilerOptions": {
"experimentalShamefullySupportOptionsApi": true
},
I think it's better than disabling Volar, especially if it's useful for you, because that's not a bug, but a new feature. Check out the changelog for more info:
https://github.com/johnsoncodehk/volar/blob/master/CHANGELOG.md
I'm getting this Postcss warning:
You did not set any plugins, parser, or stringifier. Right now, PostCSS does nothing. Pick plugins for your case on https://www.postcss.parts/ and use them in postcss.config.js. (repeated 19 times)
But I'm not using it. It's very annoying because, as you can see, the message is repeated several times.
I know why I'm getting the error (I don't have a Postcss config file or any plugins, stringifiers, etc, set) but I don't know why is Postcss installed in first place.
This is my package.json
{
"name": "vip-english-website",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start"
},
"engines": {
"node": "16.x"
},
"dependencies": {
"#dzangolab/vue-accordion": "^1.2.0",
"#nuxtjs/axios": "^5.13.6",
"express": "^4.17.1",
"googleapis": "^91.0.0",
"vue-carousel": "^0.18.0",
"vue-check-view": "^0.3.0",
"vue-gapi": "^2.0.0",
"vue-js-modal": "^2.0.1",
"vuelidate": "^0.7.6"
},
"devDependencies": {
"#nuxtjs/google-fonts": "^1.3.0",
"core-js": "^3.19.1",
"nuxt": "^2.15.8",
"nuxt-windicss": "^2.0.12"
}
}
Do anyone have any idea?
Is been 3 days of troubleshooting this error, finally the solution in the github discussion works for me.
I'm using the following dependencies
"vue": "^2.6.14",
"vue-server-renderer": "^2.6.14",
"vue-template-compiler": "^2.6.14",
"vuelidate": "^0.7.7",
"vuetify": "^2.6.1",
"webpack": "^4.46.0"
"axios": "^0.27.2",
"core-js": "^3.19.3",
"nuxt": "^2.15.8",
Github Issue - Allow to disable "You did not set any plugins, parser, or stringifier. Right now, PostCSS does nothing. Pick plugins for your case on https://www.postcss.parts/ and use them in postcss.config.js
In nuxt.config.js, under the build options, add the following as shown below. That worked for me.
build: {
postcss: null,
}
Hope it helps
PostCSS is a dependency of Nuxt. You can use npm ls {package_name} command in your project directory, to view package dependencies tree.
Issue was fixed in recent PostCSS release: https://github.com/postcss/postcss/issues/1375 , but Nuxt probably will update it only on next big release (v3).
just add to nuxt.config.js
build: {
postcss: null,
loaders: {
vue: {
prettify: false
}
}
}
I'm using nuxt 2.15.8 & having the same issue.
The following command & config will supress the warning.
npm i -D #nuxt/postcss8 #nuxtjs/style-resources
In nuxt.config.js, edit/add:
buildModules: [
'#nuxtjs/style-resources',
'#nuxt/postcss8',
],
build: {
postcss: {
plugins: {
},
preset: {
}
}
}
In my case using Nuxt, I not only needed to add the following code to the Nuxt config to disable the warning, but also to actually make the autoprefixer work! (even if the autoprefixer comes by default in Nuxt and a .browserlistrc file exists)
build: {
postcss: {
preset: {
autoprefixer: {
overrideBrowserslist: ['last 3 versions', '> 1%']
}
}
}
}
After a fresh Nuxt install I had the warning, and playing around with newer CSS rules, I noticed that without the above config, filter: grayscale(100%); would not get autoprefixed.
Editing the .browserlistrc file did not help.
For me it solved using npm install inside the project that presented these warnings. Maybe it works for someone else
I'm building a Ethereum ERC20 tokens support wallet in React Native, I have been struggling on this particular issue for the past few days and I hope someone could help.
The package at "node_modules/web3-eth-accounts/src/index.js" attempted
to import the Node standard library module "crypto"
Here is my package.json file.
"dependencies": {
"expo": "^33.0.0",
"react": "16.8.3",
"react-dom": "^16.8.6",
"react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
"react-native-web": "^0.11.4",
"socket.io-client": "^2.2.0",
"web3": "1.0.0-beta.34",
"cryptico": "^1.0.2",
"native-base": "2.8.0",
"node-libs-browser": "2.1.0",
"react-native-crypto": "^2.0.0",
"react-native-randombytes": "^2.0.0"
},
"devDependencies": {
"babel-preset-expo": "^5.1.1",
"babel-cli": "6.26.0",
"babel-preset-es2015": "6.24.1"
},
Please advise!
I have followed this link as well, no luck !
https://gist.github.com/dougbacelar/29e60920d8fa1982535247563eb63766
i have 2 answers for you :)
1) use ethers.js since its rich and have default support for React Native
2) copy shims from link that you provided to your project and import it on App.js file at top so your web3 lib should work
you can use .babelrc file to define optional configuration, First, install the dependencies:
You can try install npm install --save crypto-browserify
Then add plugins config to your .babelrc file:
{
"presets": ["babel-preset-expo"],
"env": {
"development": {
"plugins": ["transform-react-jsx-source"]
}
},
"plugins": [
["module-resolver", {
"root": ["./app"],
"alias": {
"crypto": "crypto-browserify"
}
}]
]
}
and Please import the wallet module.
import crypto from 'crypto'
You can run npm i --save-dev rn-nodeify#latest and rn-nodeify --install "stream"
if not ./node_modules/.bin/rn-nodeify --install "stream"
rn-nodeify will create a shim.js in the project root directory
example
// index.ios.js or index.android.js or App.js
// make sure you use `import` and not `require`!
import './shim.js'
Error
I get error when using react test utils. Following this blog post: http://blog.sodhanalibrary.com/2016/11/reacttestutils-tutorial-with-examples.html#.WoVNkqhl8dU . Can you advise what's wrong?
My test class:
import AzSearch from '../components/AzSearch'
import { render } from 'enzyme';
//import ReactTestUtils from 'react-dom/test-utils';
import TestUtils from 'react-addons-test-utils';
describe('AzSearch', () => {
it('should render wihtout problems', () => {
//define component
//var root = TestUtils.renderIntoDocument(<AzSearch />);
//Check it exists
//expect(root).toExist();
var shallowRenderer = TestUtils.createRenderer();
shallowRenderer.render(<AzSearch/>);
expect(shallowRenderer.getRenderOutput()).toExist();
});
});
My package:
{
"name": "npmsharepoint",
"version": "0.0.1",
"private": true,
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"build": "browserify -t [ babelify --presets [ react ] ] src/app/scripts/main.js -o src/scripts/app.js",
"clean": "gulp clean",
"test": "mocha src/webparts/User Web Parts/src/webparts/azSearch/tests/AzSearch.test.js --require src/webparts/User Web Parts/src/webparts/azSearch/tests/testHelpers.js --require src/webparts/User Web Parts/src/webparts/azSearch/tests/dom.js --recursive"
},
"dependencies": {
"#microsoft/sp-core-library": "~1.1.0",
"#microsoft/sp-lodash-subset": "~1.1.0",
"#microsoft/sp-office-ui-fabric-core": "~1.4.0-0",
"#microsoft/sp-webpart-base": "~1.1.0",
"#types/jquery": "^2.0.48",
"#types/jqueryui": "https://registry.npmjs.org/#types/jqueryui/-/jqueryui-1.11.37.tgz",
"#types/react": "0.14.46",
"#types/react-addons-shallow-compare": "0.14.17",
"#types/react-addons-test-utils": "0.14.15",
"#types/react-addons-update": "0.14.14",
"#types/react-dom": "0.14.18",
"#types/webpack-env": ">=1.12.1 <1.14.0",
"babel-preset-react": "^6.24.1",
"babelify": "^8.0.0",
"bootstrap": "^4.0.0-beta.2",
"browser-router": "^0.2.0",
"jquery": "^2.2.4",
"jqueryui": "https://registry.npmjs.org/jqueryui/-/jqueryui-1.11.1.tgz",
"prop-types": "^15.6.0",
"react": "15.4.2",
"react-autosuggest": "^9.3.2",
"react-bootstrap": "^0.31.5",
"react-bootstrap-table": "^4.3.1",
"react-dom": "15.4.2",
"react-responsive-modal": "^2.0.0",
"react-select": "^1.1.0",
"reactstrap": "^5.0.0-alpha.4",
"sp-pnp-js": "^3.0.3"
},
"devDependencies": {
"#microsoft/sp-build-web": "~1.1.0",
"#microsoft/sp-module-interfaces": "~1.1.0",
"#microsoft/sp-webpart-workbench": "~1.1.0",
"#types/chai": ">=3.4.34 <3.6.0",
"#types/material-ui": "^0.20.0",
"#types/mocha": ">=2.2.33 <2.6.0",
"#types/react-tap-event-plugin": "0.0.30",
"ajv": "~5.2.2",
"gulp": "~3.9.1",
"chai": ">=3.4.34 <3.6.0",
"mocha": ">=2.2.33 <2.6.0"
}
}
Error:
Module parse failed: C:\Users/React Projects/Web Parts/b2\SPWPTest\src\webparts\User Web Parts\lib\webparts\azSearch\tests\AzSearch.test.js Unexpected token (33:25)
You may need an appropriate loader to handle this file type.
|
| var shallowRenderer = TestUtils.createRenderer();
| shallowRenderer.render(<AzSearch/>);
| expect(shallowRenderer.getRenderOutput()).toExist();
| });
at C:/Users/React Projects/Web Parts/b2/SPWPTest/src/webparts/User Web Parts/temp/tests.js:109
Finished in 0.249 secs / 0 secs
SUMMARY:
√ 0 tests completed
15 02 2018 09:39:27.572:DEBUG [karma]: Run complete, exiting.
15 02 2018 09:39:27.573:DEBUG [launcher]: Disconnecting all browsers
Finished in 0.249 secs / 0 secs
SUMMARY:
√ 0 tests completed
15 02 2018 09:39:27.589:DEBUG [karma]: Run complete, exiting.
15 02 2018 09:39:27.590:DEBUG [launcher]: Disconnecting all browsers
Am I missing some import or module or running incorrect version of something?
Your are missing compilers config at mocha.
The possible missing part is:
--compilers js:babel-core/register
See this link.
I'm using the following package.json (according to http://facebook.github.io/jest/docs/tutorial-react-native.html#content):
{
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
},
"dependencies": {
"react": "^15.3.1",
"react-native": "0.31.0",
},
"devDependencies": {
"babel-jest": "^14.1.0",
"babel-preset-react-native": "^1.9.0",
"jest": "^14.1.0",
"jest-cli": "^13.1.0",
"jest-react-native": "^14.1.3",
"react-test-renderer": "^15.3.1"
},
"jest": {
"globals": {
"__DEV__": true,
"__RCTProfileIsProfiling": false
},
"preset": "jest-react-native"
}
}
But I get the error:
Error: Unknown config option "preset" with value "jest-react-native". This is either a typing error or another user mistake and fixing it will remove this message.
Using Jest CLI v13.2.3, jasmine2, babel-jest
FAIL __tests__/AuthorRequest-test.js (0s)
● Runtime Error
- Error: Cannot find module 'throwOnWrongReactAPI' from 'react-native.js'
at Resolver.resolveModule (node_modules/jest-cli/node_modules/jest-resolve/build/index.js:197:17)
at eval (node_modules/react-native/Libraries/react-native/react-native.js:180:26)
at Object.<anonymous> (node_modules/react-native/Libraries/react-native/react-native.js:189:4)
1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 2.238s)
npm ERR! Test failed. See above for more details.
my .babelrc file contains:
{
"presets": ["react-native"]
}
I have a feeling that you are using a wrong version of Jest. You have:
"jest": "^14.1.0",
"jest-cli": "^13.1.0"
But it seems you have 13.2.3installed using npm -g:
Using Jest CLI v13.2.3, jasmine2, babel-jest
First of all, I think you can remove jest-cli and just use jest 14.1.0.
Then you can update your test script like:
"scripts": {
"test": "./node_modules/jest/bin/jest.js"
}
In this way, you make sure you run the local Jest copy of your project, so it should say now:
Using Jest CLI v14.1.0, jasmine2, babel-jest
Doing that and following the official docs you posted, it should be everything you need (Can't say for sure since you did not post the test code).