I have a folder of code that functions as a plugin, so it is a subdirectory of a main directory. The main directory correctly runs the tests that use import statements, but the subfolder does not. The subfolder has its own .babelrc they are posted below.
.babelrc
{
"presets": [
"#babel/preset-env"
],
"plugins": [
["#babel/plugin-proposal-decorators", { "legacy": true }],
"#babel/plugin-proposal-function-sent",
"#babel/plugin-proposal-export-namespace-from",
"#babel/plugin-proposal-numeric-separator",
"#babel/plugin-proposal-throw-expressions",
"#babel/plugin-proposal-class-properties",
"#babel/plugin-transform-runtime",
"#babel/plugin-transform-modules-commonjs",
[
"module-resolver",
{
"alias": {
"example-lib": "./src"
}
}
]
],
"env": {
"test": {
"presets": [
[
"#babel/preset-env",
{
"modules": "commonjs"
}
]
]
}
}
}
package.json
"jest": {
"transform": {
"^.+\\.jsx?$": "babel-jest"
}
}
"devDependencies": {
"#babel/core": "^7.3.4",
"#babel/plugin-transform-modules-commonjs": "^7.2.0",
"#babel/preset-env": "^7.3.4",
"example-lib": "^0.11.4",
"babel-jest": "^24.5.0"
},
jest --debug
{
"configs": [
{
"automock": false,
"browser": false,
"cache": true,
"cacheDirectory": "/private/var/folders/y6/prqm_t9s177bp3zhfyc1czbr0000gn/T/jest_dx",
"clearMocks": false,
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"cwd": "/Users/user/development/repo/lib/plugin",
"dependencyExtractor": null,
"detectLeaks": false,
"detectOpenHandles": false,
"errorOnDeprecated": false,
"filter": null,
"forceCoverageMatch": [],
"globalSetup": null,
"globalTeardown": null,
"globals": {},
"haste": {
"computeSha1": false,
"providesModuleNodeModules": []
},
"moduleDirectories": [
"node_modules"
],
"moduleFileExtensions": [
"js",
"json",
"jsx",
"ts",
"tsx",
"node"
],
"moduleNameMapper": {},
"modulePathIgnorePatterns": [],
"name": "d855449ce7629afe8ca2c6248201816b",
"prettierPath": "prettier",
"resetMocks": false,
"resetModules": false,
"resolver": null,
"restoreMocks": false,
"rootDir": "/Users/user/development/repo/lib/plugin",
"roots": [
"/Users/user/development/repo/lib/plugin"
],
"runner": "jest-runner",
"setupFiles": [],
"setupFilesAfterEnv": [],
"skipFilter": false,
"snapshotSerializers": [],
"testEnvironment": "/Users/user/development/repo/lib/node_modules/jest-environment-jsdom/build/index.js",
"testEnvironmentOptions": {},
"testLocationInResults": false,
"testMatch": [
"**/__tests__/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[tj]s?(x)"
],
"testPathIgnorePatterns": [
"/node_modules/"
],
"testRegex": [],
"testRunner": "/Users/user/development/repo/lib/node_modules/jest-jasmine2/build/index.js",
"testURL": "http://localhost",
"timers": "real",
"transform": [
[
"^.+\\.jsx?$",
"/Users/user/development/repo/lib/node_modules/jest-config/node_modules/babel-jest/build/index.js"
]
],
"transformIgnorePatterns": [
"/node_modules/"
],
"watchPathIgnorePatterns": []
}
],
"globalConfig": {
"bail": 0,
"changedFilesWithAncestor": false,
"collectCoverage": false,
"collectCoverageFrom": null,
"coverageDirectory": "/Users/user/development/repo/lib/plugin/coverage",
"coverageReporters": [
"json",
"text",
"lcov",
"clover"
],
"coverageThreshold": null,
"detectLeaks": false,
"detectOpenHandles": false,
"errorOnDeprecated": false,
"expand": false,
"filter": null,
"globalSetup": null,
"globalTeardown": null,
"listTests": false,
"maxConcurrency": 5,
"maxWorkers": 7,
"noStackTrace": false,
"nonFlagArgs": [],
"notify": false,
"notifyMode": "failure-change",
"passWithNoTests": false,
"projects": null,
"rootDir": "/Users/user/development/repo/lib/plugin/",
"runTestsByPath": false,
"skipFilter": false,
"testFailureExitCode": 1,
"testPathPattern": "",
"testResultsProcessor": null,
"updateSnapshot": "new",
"useStderr": false,
"verbose": null,
"watch": false,
"watchman": true
},
"version": "24.1.0"
}
The error itself:
● 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/user/development/repo/lib/plugin/test/SystemDataService.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { example } from './helpers';
^
SyntaxError: Unexpected token {
at ScriptTransformer._transformAndBuildScript (../../node_modules/jest-runtime/build/ScriptTransformer.js:440:17)
Related
I am new at work and my colleagues are all on holidays. Before holidays my colleague replaced moment.js for day.js in our project and I started having some sync problems with all those files where the changes took place, they got solved by reinstalling Node.
However, I keep getting these ESLINT/ Prettier errors in both the browser and the terminal. I guess he also made some changes in that configuration. I tried reinstalling ESLINT and it didn't help, then I uninstalled Prettier but the problem persists. I see the problems are related to "," (comas) and spaces. How can I fix this?
error in browser
The .eslintrc file:
{
"env": {
"browser": true,
"es6": true,
"node": true,
"jest": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"plugin:prettier/recommended"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly",
"window": true
},
"parser": "babel-eslint",
"parserOptions": {
"ecmaFeatures": { "jsx": true },
"ecmaVersion": 2020,
"sourceType": "module"
},
"plugins": ["jest"],
"rules": {
"linebreak-style": ["error", "unix"],
"semi": ["error", "always"],
"no-unused-vars": "off",
"react/prop-types": 0,
"react/display-name": ["off"],
"react/no-find-dom-node": ["off"],
"prettier/prettier": ["error", {}, { "usePrettierrc": true }]
},
"settings": { "react": { "version": "detect" } },
"overrides": [
{
"plugins": ["jest"],
"files": ["**/*.ts", "**/*.tsx"],
"env": {
"browser": true,
"es6": true,
"node": true,
"jest": true
},
"extends": [
"eslint:recommended",
"plugin:#typescript-eslint/eslint-recommended",
"plugin:#typescript-eslint/recommended",
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"plugin:prettier/recommended"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly",
"window": true
},
"rules": {
"prettier/prettier": ["error", {}, { "usePrettierrc": true }],
"react/prop-types": "off",
"react/display-name": "off",
"#typescript-eslint/no-unused-vars": "warn",
"#typescript-eslint/no-loss-of-precision": "warn",
"no-unsafe-optional-chaining": "warn"
},
"parser": "#typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": { "jsx": true },
"ecmaVersion": 2020,
"sourceType": "module",
"project": "./tsconfig.json"
},
"settings": { "react": { "version": "detect" } }
}
]
}
.prettierrc file:
{
"arrowParens": "avoid",
"bracketSpacing": true,
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxBracketSameLine": false,
"jsxSingleQuote": false,
"printWidth": 80,
"proseWrap": "always",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": true
}
I installed StoryBook and Eslint. When building Storybook, Eslint sends an error about imports =>
Line 1:9: ComponentMeta not found in '#storybook/react' import/named
Line 1:24: ComponentStory not found in '#storybook/react' import/named
module.exports = {
"env": {
"browser": true,
"es2021": true
},
"overrides": [
{
"files": ['*.stories.#(ts|tsx|js|jsx|mjs|cjs)'],
"rules": {
'storybook/hierarchy-separator': 'error',
'storybook/default-exports': 'off',
"import/no-extraneous-dependencies": "off",
}
}
],
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx",]
}
},
},
"extends": ["eslint:recommended",
"plugin:react/recommended",
"plugin:#typescript-eslint/recommended",
"plugin:react-hooks/recommended",
"plugin:import/recommended",
"plugin:storybook/recommended",
"prettier"
],
"parser": "#typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["react", "#typescript-eslint"],
"rules": {
"react/prop-types": "off",
"react/react-in-jsx-scope": "off",
'react/jsx-filename-extension': [2, {
'extensions': ['.js', '.jsx', '.ts', '.tsx']
}],
"#typescript-eslint/no-explicit-any": ["off"],
"#typescript-eslint/no-unused-vars": "off",
"react/no-unescaped-entities": 0,
}
};
i have a problem when try run my tests in ionic app, Karma or Jasmine found an error inside node_modules folder and i don't have any idea of exclude spec's file inside node_modules for run my app tests
With this settings, when i run ng test open the browser with an import error of a file inside node_modules
And an error of angular core, imported of same dependency
my angular.js:
{
"$schema": "./node_modules/#angular/cli/lib/config/schema.json",
"version": 1,
"defaultProject": "Obra Show para Profissionais",
"newProjectRoot": "projects",
"projects": {
"app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"architect": {
"test": {
"builder": "#angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"styles": [],
"scripts": [],
"assets": [
{
"glob": "favicon.ico",
"input": "src/",
"output": "/"
},
{
"glob": "**/*",
"input": "src/assets",
"output": "/assets"
},
"src/manifest.webmanifest"
]
},
"configurations": {
"ci": {
"progress": false,
"watch": false
}
}
},
}
}
}
my karma.conf.js
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '#angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage'),
require('#angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
jasmineHtmlReporter: {
suppressAll: true // removes the duplicated traces
},
coverageReporter: {
dir: require('path').join(__dirname, './coverage/ngv'),
subdir: '.',
reporters: [
{ type: 'html' },
{ type: 'text-summary' }
]
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
});
};
my tsconfig.spec.json
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": [
"jasmine"
]
},
"files": [
"src/test.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
I am trying to get my eslint-plugins-css-modules plugin too work.
I am new too linting in general and may not have set this up correctly.
The exact error I get is about:
Parsing error: Unexpected token
The error is at the ':'
I have a scss file as follows:
:local(.onlineUsersWidget) {
height: 100%;
}
And an eslintrc like this:
{
"env": {
"browser": true,
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:jest/recommended",
"plugin:css-modules/recommended"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "babel-eslint",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react",
"css-modules"
],
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"windows"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
},
"overrides": [
{
"files": ["*test.js"],
"rules": {
"react/display-name": 0
}
}
]
}
Once trying to run the app, this error occurs:
Parsing angular-cli.json failed. Please make sure your
angular-cli.json is valid JSON. Error: SyntaxError: Unexpected string
in JSON at position 498
My application on
git Repo:https://github.com/kishore1288/Ki-Ng2-SPA-Form
There's a missing comma in your JSON (Line 22):
"styles": [
"app/core/preloader/preloader.scss",
"styles.scss" <----
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
],
Valid JSON:
{
"project": {
"version": "1.0.0-beta.24",
"name": "ki-angular2-spa-form0"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"test": "test.ts",
"tsconfig": "tsconfig.json",
"prefix": "app",
"mobile": false,
"styles": [
"app/core/preloader/preloader.scss",
"styles.scss",
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"app/core/preloader/preloader.ts",
"vendor.ts"
],
"environments": {
"source": "environments/environment.ts",
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
],
"addons": [],
"packages": [],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "css",
"prefixInterfaces": false,
"inline": {
"style": false,
"template": false
},
"spec": {
"class": false,
"component": true,
"directive": true,
"module": false,
"pipe": true,
"service": true
}
}
}
You can use JSONLint to validate your JSON and find errors.
Your angular-cli.json has a json error , you've forgotten a comma:
"styles": [
"app/core/preloader/preloader.scss",
"styles.scss"
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
],
should be
"styles": [
"app/core/preloader/preloader.scss",
"styles.scss",
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
]
in order to make sure your json is valid , you can put it in json validator;