Running Tests using React Test Utils and error - phantomjs

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.

Related

Vue2 run serve syntax error ... Unexpected token

I created a small test app using
vue-diagram-editor
from https://github.com/max-kut/vue-diagram-editor.
That worked no problem so wanted to install it into my main vue app, which was running fine beforehand.
$ npm install --save vue-diagram-editor ... ok installed.
$ npm run serve
INFO Starting development server...
98% after emitting CopyPlugin
ERROR Failed to compile with 2 errors 08:10:04
error in ./src/views/InstructorReadings.vue?vue&type=script&lang=js&
Syntax Error: Unexpected token (95:34)
93 | for (var i2 = 0; i2 < AllRTEvents[i].length; i2++) {
94 | var obj = {
> 95 | CreatedAt: AllRTEvents[i].[i2].createdAt,
| ^
96 | Comments: AllRTEvents[i].[i2].eventComments,
97 | PageNumber: AllRTEvents[i].[i2].eventPage.page_number,
98 | PageTitle: AllRTEvents[i].[i2].eventPage.page_title,
# ./src/views/InstructorReadings.vue?vue&type=script&lang=js& 1:0-287 1:303-306 1:308-592 1:308-592
# ./src/views/InstructorReadings.vue
# ./src/router/index.js
# ./src/main.js
# multi (webpack)-dev-server/client?http://192.168.0.120:8080&sockPath=/sockjs-node (webpack)/hot/dev-server.js ./src/main.js
error in ./src/views/Reading.vue?vue&type=script&lang=js&
Syntax Error: Unexpected token (84:34)
82 | for (var i2 = 0; i2 < AllRTEvents[i].length; i2++) {
83 | var obj = {
> 84 | CreatedAt: AllRTEvents[i].[i2].createdAt,
| ^
85 | Comments: AllRTEvents[i].[i2].eventComments,
86 | PageNumber: AllRTEvents[i].[i2].eventPage.page_number,
87 | PageTitle: AllRTEvents[i].[i2].eventPage.page_title,
# ./src/views/Reading.vue?vue&type=script&lang=js& 1:0-276 1:292-295 1:297-570 1:297-570
# ./src/views/Reading.vue
# ./src/router/index.js
# ./src/main.js
# multi (webpack)-dev-server/client?http://192.168.0.120:8080&sockPath=/sockjs-node (webpack)/hot/dev-server.js ./src/main.js
package.json
{
"name": "blah",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"#aws-amplify/ui-vue": "^1.0.13",
"#tinymce/tinymce-vue": "^3.2.8",
"aws-amplify": "^3.4.3",
"core-js": "^3.6.5",
"moment": "^2.29.1",
"nanoid": "^3.1.23",
"vue": "^2.6.11",
"vue-diagram-editor": "^1.2.1",
"vue-router": "^3.2.0",
"vuetify": "^2.4.0",
"vuex": "^3.4.0",
"vuex-persistedstate": "^3.2.0"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-plugin-eslint": "~4.5.0",
"#vue/cli-plugin-router": "~4.5.0",
"#vue/cli-plugin-vuex": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"sass": "~1.32.0",
"sass-loader": "^10.0.0",
"vue-cli-plugin-vuetify": "~2.4.1",
"vue-template-compiler": "^2.6.11",
"vuetify-loader": "^1.7.0"
}
}
Tried npm update and npm update -g
After looking around I found it may be worth modifying vue.config.js but no didnt fix.
vue.config.js
module.exports = {
transpileDependencies: ["vuetify", "vue-diagram-editor"],
};
Any ideas wha has happened and how to resolve?
Thanks
As mentioned in the comment the problem is that the access should not be done by path / dot syntax.
Changing
AllRTEvents[i].[i2].createdAt
To
AllRTEvents[i][i2].createdAt
Will fix the problem (if also done for all subsequent accesses).

Different svelte variable value for npm run dev and npm run build

I'm developing a svelte+tailwind+PHP site with rollup.js. How can I set a variable in the svelte source files depending on if I'm running npm run dev or npm run build? I'd like the different builds to connect to different back-end servers.
This is my package.json in case that's relevant. I'm new to most of these tools, so please bear with me and correct me if I've misunderstood too much. After running npm run build, I run a script that scp's the build folder to the production server.
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"watch:tailwind": "postcss public/tailwind.css -o public/index.css -w",
"build:tailwind": "NODE_ENV=production postcss public/tailwind.css -o public/index.css",
"dev": "run-p autobuild watch:tailwind",
"build": "npm run build:tailwind && rollup -c",
"start": "sirv public --single --host",
"start:dev": "sirv public --single --dev",
"autobuild": "rollup -c -w"
},
"devDependencies": {
"#rollup/plugin-commonjs": "^16.0.0",
"#rollup/plugin-node-resolve": "^10.0.0",
"autoprefixer": "^10.0.4",
"d3-interpolate": "^2.0.1",
"npm-run-all": "^4.1.5",
"postcss": "^8.1.10",
"postcss-cli": "^8.3.0",
"postcss-nested": "^5.0.1",
"postcss-reporter": "^7.0.2",
"rollup": "^2.3.4",
"rollup-plugin-css-only": "^3.0.0",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-svelte": "^7.0.0",
"rollup-plugin-terser": "^7.0.0",
"svelte": "^3.0.0",
"svelte-dnd-action": "^0.6.22",
"svelte-loading-spinners": "^0.1.1",
"tailwindcss": "^2.0.1"
},
"dependencies": {
"sirv-cli": "^1.0.0"
}
}
You can use #rollup/plugin-replace:
A Rollup plugin which replaces strings in files while bundling.
plugins: [
replace({
// alternatively, one could pass process.env.NODE_ENV or 'development` to stringify
'process.env.NODE_ENV': JSON.stringify('production')
})
]

Installing locally and globally a npm depency produces an error while executing the command line

I am trying to install locally and globally a npm depency by using npm install -g ..
The package.json seems to be ok :
{
"private": true,
...
"main": "dist/main.js",
"bin": {
"gbts": "./dist/main.js"
},
"scripts": {
"build": "tsc"
},
...
"dependencies": {
"minimist": "^1.2.5",
"ora": "^5.1.0",
"ts2c": "^2.2.7",
"yargs": "^16.1.1"
},
"devDependencies": {
"#types/node": "^14.14.10",
"eslint": "6.8.0",
"install": "^0.13.0",
"npm": "^6.14.9",
"tslint": "^5.12.1",
"typescript": "^3.3.3"
}
}
This works perfetly, but when I invoke the command gbts, I still have this error pop in error on windows:
It occurs on the following line :
const parse = require("minimist");
I do not understand why this produce a syntax error, project's dependencies are missing?
Any solution in my case?

VueJS + Karma + Webpack 4 : no test are run

recently i decided to migrate from webpack 3 to webpack 4. It almost works well until i started to run tests. If my application really works well, in fact, no tests are detected by Karma.
I use webpack throught a wrapper (symfony encore) which works well. I upgraded all packages to latest version, but it doesn't worked. I added a test file that doesn't rely on Vue, and then this test is detected and run.
So i can say that Karma try to load the VueJS spec files, but when the test try to import the SingleFileComponent (Movie.component.vue per exemple) then it stops without any warning. So maybe the problem is related to the preprocessor that might not work.
here is the karma config and the webpack encore config:
const ManifestPlugin = require('webpack-manifest-plugin');
const webpackConfig = require('./karma.webpack.config.babel');
const webpack = require('webpack');
// Configure specific plugin
for (const plugin of webpackConfig.plugins) {
// Set writeToFileEmit option of the ManifestPlugin to false
if ((plugin instanceof ManifestPlugin) && plugin.opts) {
plugin.opts.writeToFileEmit = false
}
}
// Remove entry property (handled by Karma)
delete webpackConfig.entry;
// Karma options
module.exports = function (config) {
config.set({
frameworks: ['jasmine'],
files: [
'./assets/js/vuejs/tests/DebugKarma.spec.js',
'./assets/js/vuejs/tests/Movie.spec.js'
],
preprocessors: {
'./assets/js/vuejs/tests/DebugKarma.spec.js': ['webpack'],
'./assets/js/vuejs/tests/Movie.spec.js': ['webpack']
},
browsers: ['Chrome'],
webpack: webpackConfig,
webpackMiddleware: {
stats: 'errors-only',
noInfo: true,
},
plugins: [
require('karma-webpack'),
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-spec-reporter'),
require('karma-junit-reporter'),
require('karma-jasmine-html-reporter'),
],
chromeLauncher: {
exitOnResourceError: false
},
reporters: ['kjhtml', 'spec', 'junit'],
junitReporter: {
outputDir: './var/report', // results will be saved as $outputDir/$browserName.xml
outputFile: 'karma.xml', // if included, results will be saved as $outputDir/$browserName/$outputFile
suite: '', // suite will become the package name attribute in xml testsuite element
useBrowserName: false, // add browser name to report and classes names
nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
properties: {}, // key value pair of properties to add to the <properties> section of the report
xmlVersion: 1 // use '1' if reporting to be per SonarQube 6.2 XML format
},
autoWatch: true,
});
}
const Encore = require('#symfony/webpack-encore')
Encore
.configureRuntimeEnvironment('dev-server')
// the project directory where compiled assets will be stored
.setOutputPath('./var/tests/karma/')
.setPublicPath('/')
.cleanupOutputBeforeBuild()
.disableSingleRuntimeChunk()
.addEntry()
.enableSassLoader(function(sassOptions) {}, {
resolveUrlLoader: false,
})
.enableVueLoader()
let config = Encore.getWebpackConfig();
module.exports = config
and here is a part of my package.json
"dependencies": {
"#api-platform/admin": "^0.6.2",
"#rebolon/json-reviver": "0.0.7",
"apollo-cache-inmemory": "^1.5.1",
"apollo-client": "^2.5.1",
"apollo-link": "^1.2.9",
"apollo-link-error": "^1.1.8",
"apollo-link-http": "^1.5.12",
"axios": "^0.18.0",
"devextreme": "^18.2.6",
"devextreme-vue": "^18.2.6",
"graphql": "^14.1.1",
"graphql-tag": "^2.10.1",
"offline-plugin": "^5.0.6",
"prop-types": "^15.7.2",
"quasar-extras": "^1.0.3",
"quasar-framework": "^0.15.15",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"rxjs": "^6.4.0",
"uglifyjs-webpack-plugin": "^2.1.2",
"vue": "^2.6.8",
"vue-apollo": "^3.0.0-beta.28",
"vue-router": "^3.0.2",
"vuelidate": "^0.7.4"
},
"devDependencies": {
"#angular/cli": "^7.3.4",
"#babel/preset-env": "^7.3.4",
"#babel/preset-react": "^7.0.0",
"#symfony/webpack-encore": "^0.24.0",
"bootstrap": "^4.3.1",
"create-react-app": "^1.5.2",
"jasmine": "^3.3.1",
"karma": "^4.0.1",
"karma-chrome-launcher": "^2.2.0",
"karma-jasmine": "^2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"karma-junit-reporter": "^1.2.0",
"karma-spec-reporter": "0.0.32",
"karma-webpack": "^3.0.5",
"node-sass": "^4.11.0",
"prettier": "^1.16.4",
"rxjs-tslint": "^0.1.7",
"sass-loader": "^7.1.0",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.2",
"ts-loader": "^5.3.3",
"vue-loader": "^15.7.0",
"vue-template-compiler": "^2.6.8"
},
"browserslist": "> 1%"
And finally the karma output wher we can only see DebugKarma result, but not the Movie result:
npm run test-karma
> sf-flex-encore-vuejs#0.0.1 test-karma php-sf-flex-webpack-encore-vuejs
> karma start karma.conf.js --single-run
(node:20628) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
i 「wdm」: wait until bundle finished: noop
i 「wdm」: wait until bundle finished: noop
DONE Compiled successfully in 1235ms 10:10:06
i 「wdm」:
i 「wdm」: Compiled successfully.
08 03 2019 10:10:06.850:INFO [karma-server]: Karma v4.0.1 server started at http://0.0.0.0:9876/
08 03 2019 10:10:06.853:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
08 03 2019 10:10:06.872:INFO [launcher]: Starting browser Chrome
08 03 2019 10:10:08.582:INFO [Chrome 72.0.3626 ]: Connected on socket hy9Imp7g7Qgdq0WuAAAA with id 25037598
Chrome 72.0.3626 INFO: 'This log message must be displayed in devTools of Karma browser'
Chrome 72.0.3626 INFO LOG: 'This log message must be displayed in devTools of Karma browser'
Debug Karma conf
√ config is ok
TOTAL: 1 SUCCESS
Chrome 72.0.3626 : Executed 1 of 1 SUCCESS (0.007 secs / 0.001 secs)
TOTAL: 1 SUCCESS
You can get the whole project here: https://github.com/Rebolon/php-sf-flex-webpack-encore-vuejs/tree/feature/move-on-webpack4
Any clue is welcome, i really don't know how to fix this (neither how to debug it).
Thanks

testing React Native Application with Jest - Jest encountered an unexpected token

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"]
}