Jest fails with SyntaxError: Unexpected token { - jestjs

I'm integrating jest into my nuxt application using vue-test-utils (following Edd Yerburgh's new book).
The test fails right out of the box with "SyntaxError: Unexpected token {". Similar code builds fine with nuxt and the tests ran with Ava. I'm assuming that I have a problem with my jest configuration.
I've included my package.json, code excerpt and console out.
Thanks for any help,
Dan
npm 6.4.0
package.json
{
"name": "cxl-ui-base",
"version": "1.0.0",
"description": "Base UI for SA and CXL",
"author": "Dan Mahoney <dan.mahoney#contextlabs.com>",
"private": true,
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start",
"generate": "nuxt generate",
"test:coverage": "TEST=unit nyc --report-dir=generated-files/coverage ava --tap | tap-summary",
"test:unit": "NODE_ENV=testing jest --verbose --no-cache",
"test:watch": "NODE_ENV=pro ava --watch",
"lint": "eslint -f node_modules/eslint-detailed-reporter/lib/detailed.js --ext .js,.vue -o generated-files/lint.html .",
"doc": "jsdoc -c doc.conf.js"
},
"dependencies": {
"#nuxtjs/auth": "^4.5.1",
"#nuxtjs/axios": "^5.3.1",
"#nuxtjs/dotenv": "^1.1.1",
"ava-describe": "^2.0.0",
"axios": "^0.18.0",
"body-parser": "^1.18.3",
"dotenv": "^5.0.1",
"eslint-import-resolver-alias": "^1.1.1",
"express": "^4.16.3",
"jsdoc-vue": "^1.0.0",
"jsonwebtoken": "^8.2.1",
"leaflet": "^1.3.1",
"lodash": "^4.17.10",
"moment": "^2.22.1",
"npm": "^6.4.0",
"nuxt": "1.4.1",
"nuxt-leaflet": "0.0.10",
"nuxt-material-design-icons": "^1.0.4",
"oauth-1.0a": "^2.2.4",
"vue": "^2.5.16",
"vue-d3": "^0.1.0",
"vue-i18n": "^7.6.0",
"vue-uuid": "^1.0.0",
"vue2-leaflet": "^1.0.2",
"vuelidate": "^0.7.2",
"vuetify": "^1.0.17",
"vuex": "^3.0.1",
"webpack-node-externals": "^1.7.2"
},
"devDependencies": {
"#babel/core": "^7.0.0-rc.2",
"#vue/test-utils": "^1.0.0-beta.19",
"ajv": "^6.5.0",
"babel-eslint": "^7.2.3",
"babel-jest": "^23.4.2",
"babel-plugin-add-module-exports": "^0.3.3",
"babel-plugin-transform-imports": "^1.4.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-2": "^6.24.1",
"babel-preset-vue-app": "^2.0.0",
"chromedriver": "^2.38.3",
"eslint": "^4.3.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-config-standard": "^10.2.1",
"eslint-detailed-reporter": "^0.7.3",
"eslint-import-resolver-webpack": "^0.9.0",
"eslint-loader": "^1.9.0",
"eslint-plugin-html": "^4.0.3",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-jsdoc": "^3.7.1",
"eslint-plugin-leon-require-jsdoc": "0.0.1",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.7.0",
"eslint-plugin-standard": "^3.1.0",
"eslint-plugin-vue": "^4.5.0",
"eslint-plugin-vue-a11y": "0.0.26",
"jest": "^23.5.0",
"jest-vue-preprocessor": "^1.4.0",
"jsdoc": "^3.5.5",
"jsdom": "^11.11.0",
"jsdom-global": "^3.0.2",
"loglevel": "^1.6.1",
"nightwatch": "^0.9.21",
"npm-merge-driver": "^2.3.5",
"raf": "^3.4.0",
"require-extension-hooks": "^0.3.2",
"require-extension-hooks-babel": "^0.1.1",
"require-extension-hooks-vue": "^1.0.0",
"selenium": "^2.20.0",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.1",
"tap-summary": "^4.0.0",
"vue-jest": "^2.6.0",
"vue-loader": "^13.7.2",
"vue-meta": "^1.5.0",
"vue-template-compiler": "^2.5.16"
},
"jest": {
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.vue$": "vue-jest"
}
},
"eslintConfig": {
"env": {
"browser": true,
"node": true,
"jest": true
}
}
}
Code
import { shallowMount, createLocalVue } from '#vue/test-utils';
import Vuetify from 'vuetify';
import test from 'jest';
import ClientMap from '#/components/Map'; // eslint-disable-line
import { commonAssertions } from '#/plugins/test.utils';
// for mocking
import modal from '#/components/Modal'; // eslint-disable-line
const localVue = createLocalVue();
localVue.use(Vuetify);
test('Sanity Test', () => {});
test('Initial State', (t) => {
const $modal = sinon.mock(modal);
const wrapper = shallowMount(Map, {
mocks: {
$modal,
},
localVue,
});
commonAssertions(Map, t, wrapper);
});
test.todo('Select Layer');
test.todo('Test Modal??');
test.todo('Test Tooltip??');
test.todo('UnSelect Layer');
Relevant Output
FAIL src/test/specs/map.spec.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:
• 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:
/Users/dan.mahoney/Projects/cxl-ui-base/src/test/specs/map.spec.js:10
import { shallowMount, createLocalVue } from '#vue/test-utils';
^
SyntaxError: Unexpected token {
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.045s

The problem is that nuxt puts the babel config into nuxt.config.js. I found an npm package that solves that. It allows you to have a .babelrc file and have it injected into nuxt.config.js. When Jest compiles the files for testing, it uses .babelrc. Kudos to the author.

You should set the NODE_ENV to test then run the jest
you can do it by adding this line to your package.json file
"scripts": {
....
"test": "NODE_ENV=test jest"
},

Related

Support for the experimental syntax 'jsx' isn't currently enabled in monorepo

I currently have 2 packages in my monorepo
web-ui & testing-utils
testing-utils
package.json
{
"name": "#testing-utils",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"lint": "eslint",
"postinstall": "cd ../../ && patch-package",
"test": "jest --passWithNoTests",
"prettier": "prettier ../../.prettierrc.js -c \"./**/*.{ts,tsx,js,jsx}\"",
"prettier:write": "prettier ../../.prettierrc.js --write \"./**/*.{ts,tsx,js,jsx}\""
},
"dependencies": {
"react": "16.14.0",
"react-redux": "7.2.2",
"redux": "4.0.5"
},
"devDependencies": {
"#babel/cli": "^7.2.3",
"#babel/core": "^7.12.10",
"#babel/plugin-proposal-class-properties": "^7.12.1",
"#babel/plugin-syntax-dynamic-import": "^7.2.0",
"#babel/plugin-syntax-jsx": "^7.18.6",
"#babel/plugin-transform-react-jsx": "^7.19.0",
"#babel/plugin-transform-runtime": "^7.12.10",
"#babel/preset-env": "^7.19.4",
"#babel/preset-react": "^7.18.6",
"#babel/preset-typescript": "^7.18.6",
"#testing-library/react": "12.0.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.3",
"babel-loader": "^8.0.5",
"babel-plugin-react-require": "^3.1.3",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-preset-minify": "^0.6.0-alpha.9",
"eslint": "7.21.0",
"eslint-config-airbnb": "18.2.1",
"eslint-config-airbnb-typescript": "12.3.1",
"eslint-config-prettier": "8.1.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jsx-a11y": "6.4.1",
"eslint-plugin-prettier": "3.4.0",
"eslint-plugin-react": "7.22.0",
"eslint-plugin-react-hooks": "4.2.0",
"prettier": "2.3.0",
"react-test-renderer": "16.14.0"
}
}
.babelrc
{
"presets": [["#babel/preset-env"], "#babel/preset-react"],
"ignore": ["node_modules/**"],
"plugins": ["#babel/plugin-transform-runtime", "#babel/plugin-syntax-jsx"]
}
renderWithTheme.js
import { render } from '#testing-library/react';
import { THEME } from '#web-ui/src/theme';
import React from 'react';
import { ThemeProvider } from 'styled-components';
const renderWithTheme = (ui) => {
function Wrapper({ children }) {
return <ThemeProvider theme={THEME}>{children}</ThemeProvider>;
}
return render(ui, { wrapper: Wrapper });
};
export default renderWithTheme;
web-ui
package.json
{
"name": "#web-ui",
"version": "0.0.1",
"license": "MIT",
"scripts": {
"lint": "eslint src --ext .js,.jsx,.ts,.tsx",
"jest": "jest",
"test": "jest --coverage --runInBand",
"typecheck": "npx tsc --noemit",
"prettier": "prettier ../../.prettierrc.js -c \"./src/**/*.{ts,tsx,js,jsx}\"",
"prettier:write": "prettier ../../.prettierrc.js --write \"./src/**/*.{ts,tsx,js,jsx}\""
},
"devDependencies": {
"#babel/plugin-syntax-jsx": "^7.18.6",
"#babel/plugin-transform-react-jsx": "^7.19.0",
"#babel/preset-env": "^7.19.4",
"#babel/preset-react": "^7.18.6",
"#testing-library/jest-dom": "^5.11.6",
"#testing-library/react": "12.0.0",
"#types/react": "16.14.0",
"babel-plugin-syntax-jsx": "^6.18.0",
"es-check": "^5.1.2",
"eslint": "7.21.0",
"eslint-config-airbnb": "18.2.1",
"eslint-config-airbnb-typescript": "12.3.1",
"eslint-config-prettier": "8.1.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jsx-a11y": "6.4.1",
"eslint-plugin-prettier": "3.4.0",
"eslint-plugin-react": "7.22.0",
"eslint-plugin-react-hooks": "4.2.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^26.0.1",
"prettier": "2.3.0",
"react-test-renderer": "^16.13.1",
"typescript": "4.3.5"
},
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.32",
"#fortawesome/free-brands-svg-icons": "^5.15.1",
"#fortawesome/free-regular-svg-icons": "^5.15.2",
"#fortawesome/pro-light-svg-icons": "^5.15.2",
"#fortawesome/pro-regular-svg-icons": "^5.15.1",
"#fortawesome/pro-solid-svg-icons": "^5.15.1",
"#fortawesome/react-fontawesome": "^0.1.13",
"#googlemaps/markerclustererplus": "^1.2.0",
"#juggle/resize-observer": "^3.3.1",
"#uiw/react-textarea-code-editor": "^2.0.3",
"axios": "^0.21.0",
"date-fns": "^1.9.0",
"dinero.js": "^1.6.0",
"formik": "^2.1.4",
"google-libphonenumber": "^3.2.15",
"libphonenumber": "0.0.10",
"lodash": "^4.17.20",
"moment": "^2.29.1",
"rc-slider": "^8.6.7",
"react-dates": "^21.8.0",
"react-number-format": "^4.4.1",
"react-router-dom": "^5.2.0",
"react-select": "^3.0.8",
"react-spinkit": "^3.0.0",
"react-spinners": "^0.10.6",
"react-spring": "^8.0.27",
"react-table": "^7.6.3",
"react-tag-autocomplete": "^6.1.0",
"react-transition-group": "^4.4.1",
"styled-components": "^4.4.1",
"use-resize-observer": "^7.0.0",
"yup": "^0.29.3"
},
"resolutions": {
"#types/react": "16.14.0"
}
}
.babelrc
{
"presets": [["#babel/preset-env"], "#babel/preset-react"],
"ignore": ["node_modules/**"],
"plugins": ["#babel/plugin-transform-runtime", "#babel/plugin-syntax-jsx"]
}
tests/form/form.test.js
import renderWithTheme from '#testing-utils/renderWithTheme';
import React, { useRef, useEffect } from 'react';
import { waitFor } from '#testing-library/react';
import Form from '../../src/form/Form/Form';
const FormDummy = (props) => {
const formRef = useRef();
const initialValues = {
terms: '',
};
useEffect(() => {
if (formRef && formRef.current) formRef.current.submitForm();
}, [formRef]);
return (
<div>
<Form onSubmit={props.onSub} formRef={formRef} initialValues={initialValues} />
</div>
);
};
describe('form', () => {
test('it should render without error', () => {
const initialValues = {
terms: '',
};
const { container } = renderWithTheme(<Form initialValues={initialValues} />);
expect(container).toBeDefined();
});
test('it should render without error when custom handle submit', async () => {
const onSub = jest.fn();
renderWithTheme(<FormDummy onSub={onSub} />);
await waitFor(() => expect(onSub).toBeCalled());
});
});
On the root of the monorepo I also have the following;
babel.config.js
module.exports = {
"presets": [["#babel/preset-env"], "#babel/preset-react"],
"ignore": ["node_modules/**"],
"plugins": ["#babel/plugin-transform-runtime", "#babel/plugin-syntax-jsx"]
}
package.json
{
"private": true,
"name": "monorepo",
"version": "0.1.0",
"license": "MIT",
"scripts": {
"re-install": "rm -rf node_modules packages/node_modules packages/web-ui/node_modules packages/testing-utils/node_modules && yarn",
"lint": "yarn workspaces run lint",
"test": "yarn workspaces run test",
"typecheck": "yarn workspaces run tsc --noemit",
"prettier": "yarn workspaces run prettier",
"prettier:write": "yarn workspaces run prettier:write",
"postinstall": "patch-package",
"prepare": "husky install",
},
"devDependencies": {
"husky": "^6.0.0",
"minimist": "^1.2.5",
"patch-package": "^6.4.4",
"plist": "^3.0.1",
"postinstall-postinstall": "^2.1.0"
},
"workspaces": {
"packages": [
"packages/*"
]
},
"dependencies": {},
"resolutions": {
"#types/react": "16.14.0"
}
}
when trying to run npx jest form I get the following error;
FAIL __tests__/form/form.test.js
● Test suite failed to run
SyntaxError: ~~~/packages/testing-utils/renderWithTheme.js: Support for the experimental syntax 'jsx' isn't currently enabled (8:12):
6 | const renderWithTheme = (ui) => {
7 | function Wrapper({ children }) {
> 8 | return <ThemeProvider theme={THEME}>{children}</ThemeProvider>;
| ^
9 | }
10 |
11 | return render(ui, { wrapper: Wrapper });
Add #babel/preset-react (https://github.com/babel/babel/tree/main/packages/babel-preset-react) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add #babel/plugin-syntax-jsx (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-jsx) to the 'plugins' section to enable parsing.
As you can see from my files above, I have done the suggestion of adding #babel/preset-react to the presets section and added #babel/plugin-syntax-jsx to the plugins section to all my babel configs.
Where am I going wrong here?
Any help would be greatly appreciated.
What ended up actually working for me in the end was changing all the .babelrc files to be babel.config.js instead.
Not fully sure why that worked, but it did.

Cannot mock in Jest with ES modules

I'm trying to reproduce the example given in Jest documentation about mocking module. The code is the following one.
users.js
import axios from 'axios'
class Users {
static all () {
return axios.get('/users.json').then(resp => resp.data)
}
}
export default Users
test.js
import axios from 'axios'
import Users from './users'
import { jest } from '#jest/globals'
jest.mock('axios')
test('should fetch users', () => {
const users = [ { name: 'Bob' } ]
const resp = { data: users }
axios.get.mockResolvedValue(resp)
// or you could use the following depending on your use case:
// axios.get.mockImplementation(() => Promise.resolve(resp))
return Users.all().then(data => expect(data).toEqual(users))
})
The code from test.js is a little bit different from the documentation. I had to add import { jest } from '#jest/globals' because of this issue
package.json
{
...
"type": "module",
"engines": {
"node": ">=14.17.6"
},
"config": {
"nuxt": {
"host": "0.0.0.0",
"port": "8081"
}
},
"scripts": {
"dev": "cross-env NODE_ENV=development nodemon --insecure-http-parser --inspect server/index.js --watch server",
"test-server": "cross-env CYPRESS_MODE=true THIRD_PARTY_ROUTES_ENABLED=false npm run start",
"ci-test-server": "npm run test-server & wait-on http://localhost:8081/fr-FR",
"build-for-cypress": "cross-env NUXT_ENV_CYPRESS_MODE=true npm run build",
"build": "nuxt build",
"start": "cross-env NODE_ENV=production node --insecure-http-parser server/index.js",
"generate": "nuxt generate",
"lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
"precommit": "npm run lint",
"phrase": "node scripts/phraseapp/phraseapp.js",
"cypress:open-mocked": "cypress open --env env=mock",
"cypress:run-mocked": "cypress run --env env=mock",
"cypress:open-unmocked": "cypress open --env=real,\"TAGS=not #mockOnly\"",
"cypress:run-unmocked": "cypress run --env=real,\"TAGS=not #mockOnly\"",
"ci-test": "cross-env TEST_MODE=true jest tests --runInBand --coverage",
"front-unit-tests": "cross-env jest tests/client/unit",
"back-tests": "cross-env TEST_MODE=true jest tests/server --runInBand"
},
"dependencies": {
"#cospired/i18n-iso-languages": "^3.1.0",
"#jest/globals": "^27.2.5",
"#nuxtjs/axios": "^5.13.6",
"#nuxtjs/dotenv": "^1.4.1",
"#nuxtjs/i18n": "^7.0.3",
"#nuxtjs/proxy": "^2.1.0",
"#nuxtjs/style-resources": "^1.2.1",
"#nuxtjs/toast": "^3.3.1",
"#sport-activities/nuxt-di": "^0.1.2",
"#vtmn/css": "^0.22.1",
"#vtmn/icons": "^0.4.0",
"abort-controller": "^3.0.0",
"agentkeepalive": "^4.1.4",
"atob": "^2.1.2",
"axios": "^0.22.0",
"body-parser": "^1.19.0",
"cacheable-lookup": "^6.0.1",
"connect-redis": "^6.0.0",
"cookie-parser": "^1.4.5",
"cookie-universal-nuxt": "^2.1.5",
"cross-env": "^7.0.3",
"csurf": "^1.11.0",
"dd-trace": "^1.4.1",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-session": "^1.17.2",
"express-validator": "^6.12.2",
"google-libphonenumber": "^3.2.24",
"helmet": "^4.6.0",
"ioredis": "^4.27.9",
"lodash": "^4.17.21",
"luxon": "^2.0.2",
"memory-cache": "^0.2.0",
"moment": "^2.29.1",
"morgan": "^1.10.0",
"nock": "^13.1.3",
"node-cron": "^3.0.0",
"node-fetch": "^2.6.1",
"nuxt": "^2.15.8",
"nuxt-lazy-load": "^1.2.7",
"nuxt-purgecss": "^1.0.0",
"oauth": "^0.9.15",
"p-limit": "^3.1.0",
"passport": "^0.4.1",
"passport-oauth2": "^1.6.1",
"redlock": "^4.2.0",
"uuid": "^8.3.2",
"validator": "^13.6.0",
"vue-server-renderer": "^2.6.14",
"vue-toasted": "^1.1.28",
"wait-on": "^6.0.0",
"winston": "^3.3.3",
"world-countries": "^4.0.0"
},
"devDependencies": {
"#babel/core": "^7.15.5",
"#babel/eslint-parser": "^7.15.7",
"#babel/preset-env": "^7.15.6",
"#jest/types": "^27.2.4",
"#types/jest": "^27.0.2",
"#vue/test-utils": "1.2.2",
"axios-mock-adapter": "^1.20.0",
"babel-core": "^7.0.0-bridge.0",
"css-loader": "^5.2.7",
"cypress": "8.5.0",
"cypress-cucumber-preprocessor": "^4.2.0",
"eslint": "^7.32.0",
"eslint-config-standard": "^16.0.3",
"eslint-friendly-formatter": "^4.0.1",
"eslint-loader": "^4.0.2",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jest": "^24.5.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-standard": "^5.0.0",
"eslint-plugin-vue": "^7.18.0",
"file-loader": "^6.2.0",
"ioredis-mock": "^5.6.0",
"jest": "^27.2.4",
"less": "^4.1.1",
"less-loader": "^7.2.1",
"nodemon": "^2.0.13",
"supertest": "^6.1.6",
"vue-i18n": "8.26.5",
"vue-jest": "^3.0.7",
"vue-svg-loader": "^0.16.0"
},
"cypress-cucumber-preprocessor": {
"nonGlobalStepDefinitions": false,
"step_definitions": "tests/client/e2e/integration/steps",
"cucumberJson": {
"generate": true,
"outputFolder": "tests/client/e2e/integration/cucumber-json",
"filePrefix": "",
"fileSuffix": ".cucumber"
}
}
}
jest.config.js
export default {
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/tests/client/unit/__mocks__/fileMock.js',
'\\.(css|less)$': '<rootDir>/tests/client/unit/__mocks__/styleMock.js',
'^#/(.*)$': '<rootDir>/$1'
},
moduleFileExtensions: [
'js',
'json',
'vue',
'svg'
],
transform: {},
testMatch: [
'**/?(*.)+(test).[jt]s?(x)',
'**/tests/routes/**/*.spec.js'
],
testEnvironment : 'jsdom',
setupFilesAfterEnv: [
'./tests/jest-setup/jest.setup.ioredis-mock.js',
'./tests/jest-setup/jest.setup.timers.js',
'./tests/jest-setup/jest.setup.envvar.js',
'./tests/jest-setup/jest.setup.nock.js',
'./tests/jest-setup/jest.setup.redis-cache.js'
]
}
.babelrc
{
"presets": ["#babel/preset-env"],
"plugins": [
["#babel/plugin-proposal-decorators", { "legacy": true }]
]
}
When executing the test, I'm getting this as a result:
axios.get.mockResolvedValue is not a function
TypeError: axios.get.mockResolvedValue is not a function
Looks like the mock won't work. What have I missed to make it work?

SyntaxError: Cannot use import statement outside a module using jest

I'm trying to start tests using Jest, but I'm not able to use "import", so how can I use import in Jest?
plus.js
function addTwo(num) {
return num + 2;
}
export { addTwo };
test.js
import { addTwo } from './plus';
test('adds 1 + 2 to equal 3', () => {
expect(addTwo(4)).toBe(6);
});
Error:
My package.json:
{
"name": "...",
"version": "1.0.0",
"title": "...",
"homepage": "http://...",
"devDependencies": {
"#babel/core": "7.12.3",
"babel-loader": "8.1.0",
"btoa": "1.2.1",
"chalk": "3.0.0",
"circular-dependency-plugin": "5.2.0",
"css-loader": "5.0.0",
"dp-grunt-contrib-copy": "0.4.4",
"eslint": "5.16.0",
"eslint-loader": "4.0.2",
"file-loader": "6.2.0",
"filemanager-plugin": "2.5.2",
"generate-json-webpack-plugin": "2.0.0",
"grunt": "1.1.0",
"grunt-contrib-clean": "0.7.0",
"grunt-contrib-compress": "1.6.0",
"grunt-contrib-concat": "1.0.1",
"grunt-contrib-copy": "1.0.0",
"grunt-contrib-cssmin": "1.0.2",
"grunt-cssjoin": "0.3.0",
"grunt-eslint": "21.1.0",
"grunt-exec": "3.0.0",
"grunt-jsmin-sourcemap": "1.10.0",
"grunt-terser": "0.1.0",
"grunt-writefile": "0.1.4",
"html-loader": "1.3.2",
"jest": "^26.6.3",
"jwt-decode": "3.0.0",
"mini-css-extract-plugin": "1.2.1",
"npm-run-all": "4.1.5",
"prettier": "2.1.2",
"prettier-webpack-plugin": "1.2.0",
"resize-observer-polyfill": "1.5.1",
"sass": "1.25.0",
"sass-loader": "10.0.4",
"sha1": "1.1.1",
"ssh2": "0.8.9",
"style-loader": "2.0.0",
"vue-virtual-scroller": "1.0.10",
"webpack": "5.3.1",
"webpack-cli": "3.3.12",
"xmlhttprequest": "1.8.0"
},
"dependencies": {
"inputmask": "5.0.0",
"jquery": "2.1.3",
"requirejs": "2.3.6",
"vue": "2.6.10",
"vue-i18n": "8.15.3",
"vuedraggable": "2.23.2"
},
"scripts": {
"build": "npm run dev && bash ./build.sh",
"lint": "npx eslint --fix --quiet .",
"prettier": "npx prettier --config ./.prettierrc.json --write ./**/*.{scss,js}",
"prod": "node node/compiler.js prod",
"dev": "node node/compiler.js dev",
"backup-log": "node node/logger.js",
"test": "jest"
}
}
The solution was install #babel/preset-env:
npm install --save-dev #babel/preset-env
Create a file babel.config.js
module.exports = {
presets: [['#babel/preset-env', {targets: {node: 'current'}}]],
};

NodeJS - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

I have a Node/React project in my Webstorm that won't run because of this error. I had to reinstall Windows and start fresh with my development. I got the code back into the IDE, but when I start up the Node server, I am getting the following error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
More context for that error:
[nodemon] starting `babel-node src/node-server/index.js`
internal/validators.js:122
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
at validateString (internal/validators.js:122:11)
at Object.join (path.js:375:7)
at Object.<anonymous> (C:\Projects\Production-Orchestrator\src\node-server\/index.js:17:15)
I went to index.js and here is lines 1-17:
// npm run server
import dotenv from 'dotenv';
import express from 'express';
import path from 'path';
import sql from 'mssql';
import cors from 'cors';
import http from 'http';
import { setupWebSocket } from './ws/setupWebSocket.js';
const useWebSockets = true;
dotenv.config();
const dbConfig = {
user: process.env.DB_USER,
password: process.env.DB_PASS,
server: path.join(process.env.DB_SERVER, process.env.DB_HOST),
I am running npm run server to start up my node server.
And here is my package.json if it helps:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"description": "my app",
"homepage": ".",
"repository": {
"type": "git",
"url": "git+https://github.com/xxxxxxx"
},
"license": "UNLICENSED",
"scripts": {
"buildp": "env-cmd -f .env.production react-scripts build",
"buildt": "env-cmd -f .env.test react-scripts build",
"buildw": "webpack --config ./webpack.config.js --mode production",
"eject": "react-scripts eject",
"eslint-check": "eslint --print-config src/components/search/Search.js | eslint-config-prettier-check",
"server": "nodemon --exec babel-node src/node-server/index.js",
"start": "SET REACT_APP_WS_PORT=3001 & react-scripts start",
"startw": "webpack-dev-server --config ./webpack.config.js --mode development --open",
"stylelint": "stylelint **/*.scss",
"test": "react-scripts test",
"ws": "node --experimental-modules src/node-server/websocket.js",
"ws2": "nodemon --exec babel-node src/node-server/websocket.js"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"dependencies": {
"#babel/preset-react": "^7.8.0",
"#emotion/core": "latest",
"#fortawesome/fontawesome": "^1.1.8",
"#fortawesome/fontawesome-svg-core": "^1.2.27",
"#fortawesome/free-regular-svg-icons": "^5.12.1",
"#fortawesome/free-solid-svg-icons": "^5.12.1",
"#fortawesome/react-fontawesome": "^0.1.8",
"#popperjs/core": "^2.0.6",
"#react-pdf/renderer": "^1.6.8",
"#types/react": "^16.9.19",
"animate.css": "^3.7.2",
"axios": "^0.19.2",
"babel-loader": "^8.0.6",
"bootstrap": "^4.4.1",
"bufferutil": "^4.0.1",
"cors": "^2.8.5",
"dayjs": "^1.8.20",
"device-detector-js": "^2.2.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-ws": "^4.0.0",
"file-saver": "^2.0.2",
"javascript-time-ago": "latest",
"jquery": "^3.4.1",
"jsbarcode": "^3.11.0",
"mssql": "^6.1.0",
"popper.js": "^1.16.1",
"print-js": "^1.0.63",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-animations": "^1.0.0",
"react-beautiful-dnd": "^12.2.0",
"react-beforeunload": "^2.2.1",
"react-collapse": "^5.0.1",
"react-confirm-alert": "^2.6.1",
"react-custom-scrollbars": "^4.2.1",
"react-detect-offline": "^2.4.0",
"react-dnd": "^10.0.2",
"react-dnd-html5-backend": "^10.0.2",
"react-dom": "^16.12.0",
"react-modal": "^3.11.1",
"react-notifications-component": "^2.3.0",
"react-popup": "^0.10.0",
"react-radio-group": "^3.0.3",
"react-router-dom": "^5.1.2",
"react-scripts": "^3.4.3",
"react-select": "^3.0.8",
"react-spinners": "^0.8.0",
"react-spring": "^8.0.27",
"react-time-ago": "^5.0.7",
"react-transition-group": "^4.3.0",
"sort-package-json": "^1.40.0",
"styled-components": "^4.4.1",
"typescript": "^3.7.5",
"utf-8-validate": "^5.0.2",
"ws": "^7.2.5"
},
"devDependencies": {
"#babel/cli": "^7.8.4",
"#babel/core": "^7.8.4",
"#babel/node": "^7.10.5",
"#babel/preset-env": "^7.8.4",
"#welldone-software/why-did-you-render": "^4.2.5",
"css-loader": "^3.4.2",
"dotenv-cli": "^3.2.0",
"env-cmd": "^10.1.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-loader": "^3.0.3",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-prettier": "^3.1.2",
"file-loader": "^5.0.2",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.13.1",
"nodemon": "^2.0.4",
"prettier": "^1.19.1",
"react-hot-loader": "^4.12.19",
"sass-loader": "^8.0.2",
"stylelint": "^13.2.0",
"stylelint-config-rational-order": "^0.1.2",
"stylelint-config-standard": "^19.0.0",
"stylelint-order": "^4.0.0",
"stylelint-scss": "^3.14.2",
"url-loader": "^3.0.0",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.11"
},
"proxy": "http://localhost:3000"
}
OK, I figured out the issue. I thought the error was telling me that path was undefined. When it fact it was saying the variables passed into path.join() were undefined. And that was because I forgot to add in my .env file to the root so it could grab those variables. Whoops!
In my case, the issue was the missing of an env variable. I did not have SECRET. So the SECRET env variable was missing and my test was trying to access it and as a result it was returning undefined
TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be one of type string, TypedArray, or DataView. Received type undefined
For me was the Knex configuration file had a typo.
For testing I had
testing{
...
migrations: {
connection{
directory: './src/db/migrations',
tableName: 'knex_migrations',
}
},
...
}
when I needed
testing{
...
migrations: {
directory: './src/db/migrations',
tableName: 'knex_migrations',
},
...
}
My issue was that I added && after the env-cmd command, so that for my start script I got
"start": "env-cmd -e development && react-scripts start",
instead of
"start": "env-cmd -e development react-scripts start",
When I removed the && everything was fine.
This worked for me.
https://reactgo.com/typeerror-err-invalid-arg-type-react/
It says, that this error occurs when the react-scripts version is old.
So, delete all your current node modules, install the latest react-scripts using: 'npm install react-scripts#latest' and then install all the other dependencies.
I did the exact same thing, it worked.

vue-cli project source map not working/how to get working

I created a new project using vue-cli(#vue/cli 4.3.1). When there is an error in one of my component files(.vue file), in chrome console i do not see the actual file/line number. Instead i see a file reference called vue.runtime.esm.js?2b0e:619. Did some googling on how to enable source map. I added a vue.config.js file on my project root and added below lines:
module.exports = {
configureWebpack: (config) => {
config.devtool = 'eval-source-map'
},
productionSourceMap: true
};
Did npm run server. But still i do not see the actual file reference.
How do i enable source map in a vue-cli-project??
\\File: packages.json
{
"name": "real-world-vue",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build --mode development",
"lint": "vue-cli-service lint",
"build-watch": "vue-cli-service build-watch --mode development"
},
"dependencies": {
"axios": "^0.19.2",
"core-js": "^3.6.4",
"json-server": "^0.16.1",
"vue": "^2.6.11",
"vue-router": "^3.1.6",
"vuejs-datepicker": "^1.6.2",
"vuex": "^3.1.3"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.3.0",
"#vue/cli-plugin-eslint": "~4.3.0",
"#vue/cli-plugin-router": "~4.3.0",
"#vue/cli-plugin-vuex": "~4.3.0",
"#vue/cli-service": "~4.3.0",
"#vue/eslint-config-prettier": "^6.0.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-vue": "^6.2.2",
"node-sass": "^4.14.0",
"prettier": "^1.19.1",
"sass-loader": "^8.0.2",
"vue-cli-plugin-build-watch": "~1.0.0",
"vue-template-compiler": "^2.6.11"
}
}
Please help.
In my case I got it to work using devtool right under configureWebpack, so no config field there.
I also had to use 'cheap-module-source-map' instead of 'eval-source-map'. In the end it would look like this:
module.exports = {
configureWebpack: {
devtool: "cheap-module-source-map",
},
productionSourceMap: true
};

Resources