Related
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.
I updated all the packages in my project. And upgrade vue 3.
After the done with updates there is a problem in my code.
I cannot use Vue.use(VueCompositionAPI);
In the old version of the project I call it from import VueCompositionAPI from '#vue/composition-api';
but this package is not compatible with due 3 anymore so I cannot call it.
What shouldd I do?
package.json:
{
"name": "front",
"version": "1.0.0",
"description": "Frontend",
"main": ".eslintrc.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"test:e2e": "vue-cli-service test:e2e",
"lint": "vue-cli-service lint"
},
"repository": {
"type": "git",
},
"keywords": [
"fdm"
],
"author": "etc",
"license": "ISC",
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^6.1.1",
"#fortawesome/free-solid-svg-icons": "^6.1.1",
"#fortawesome/vue-fontawesome": "^3.0.1",
"#jsonforms/core": "^2.5.2",
"#jsonforms/vue": "^2.5.2",
"#jsonforms/vue-vanilla": "^2.5.2",
"#vue/cli-plugin-babel": "^5.0.8",
"#vue/cli-plugin-e2e-nightwatch": "^5.0.8",
"#vue/cli-plugin-eslint": "^5.0.8",
"#vue/cli-plugin-pwa": "^5.0.8",
"#vue/cli-plugin-router": "^5.0.8",
"#vue/cli-plugin-unit-mocha": "^5.0.8",
"#vue/cli-plugin-vuex": "^5.0.8",
"#vue/cli-service": "^5.0.8",
"#vue/eslint-config-airbnb": "^6.0.0",
"#vue/test-utils": "^2.0.2",
"axios": "^0.27.2",
"bootstrap": "^5.1.3",
"bootstrap-vue": "^2.22.0",
"chai": "^4.3.6",
"chromedriver": "^103.0.0",
"core-js": "^3.23.4",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-vue": "^9.2.0",
"geckodriver": "^3.0.2",
"json-schema-ref-parser": "^9.0.9",
"jsonpath": "^1.1.1",
"lodash": "^4.17.21",
"node-sass": "^7.0.1",
"npm": "^8.13.2",
"register-service-worker": "^1.7.2",
"sass-loader": "^13.0.2",
"vue": "^3.2.37",
"vue-axios": "^3.4.1",
"vue-bootstrap4-table": "^1.1.11",
"vue-router": "^4.1.1",
"vue-sorted-table": "^1.3.0",
"vue-template-compiler": "^2.7.4",
"vuedraggable": "^2.24.3",
"vuex": "^4.0.2"
},
"devDependencies": {
"#babel/core": "^7.18.6",
"#babel/eslint-parser": "^7.18.2",
"eslint": "^8.19.0"
}
}
my main.js:
import Vue from 'vue';
import { BootstrapVue } from 'bootstrap-vue';
import { library } from '#fortawesome/fontawesome-svg-core';
import SortedTablePlugin from 'vue-sorted-table';
import {
faSpinner, faSortUp, faSortDown, faSort, faFilter, faCheck, faTimesCircle, faUser, faPause,
faTrash,
} from '#fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon, FontAwesomeLayers } from '#fortawesome/vue-fontawesome';
import App from './App.vue';
import './registerServiceWorker';
import router from './router';
import store from './store';
Vue.config.productionTip = false;
Vue.use(VueCompositionAPI);
// Install BootstrapVue
Vue.use(BootstrapVue);
Vue.use(SortedTablePlugin);
library.add(
faSpinner,
);
Vue.component('font-awesome-icon', FontAwesomeIcon);
Vue.component('font-awesome-layers', FontAwesomeLayers);
new Vue({
router,
store,
render: (h) => h(App),
}).$mount('#app');
Look, vue 2 is build on the options API.
Vue 3 is build on composition API.
In order to use composition API in your Vue 2 app, you needed the #vue/composition-api package to run it.
What you did is: You upgraded to 3 (wich is by default composition API) and try to use the #vue/composition-api wich is only made for vue 2 in order to use the composition API. But vue 3 is by default composition API. I hope you get me here.
Also, since 2.7 you dont even need this package anymore
Also there is a note:
When you migrate to Vue 3, just replacing #vue/composition-api to vue
and your code should just work.
In vue 2.x you did:
const { ref, reactive } = VueCompositionAPI
In vue 3.x you do:
import { ref, reactive } from "vue"
or:
const { ref, reactive } = Vue;
I have a node/typescript server. I wish to test the api routes with supertest. I've written my first test, but cannot start it because
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/myname/Desktop/Code/Reapr/server/src/tests/search.test.ts
The test.ts file is:
import request from "supertest";
import { server } from "../index";
describe("GET /user/:id", () => {
it("return user information", (done) => {
request(server)
.get("/user/123")
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200, done);
});
});
The package.json is:
{
"name": "my-server",
"version": "1.0.0",
"main": "index.js",
"license": "",
"scripts": {
"build": "rimraf ./build && tsc",
"dev": "nodemon",
"lint": "eslint . --ext .js,.ts",
"start": "npm run build && node build/index.js",
"test": "node src/tests/search.test.ts"
},
"type": "module",
"dependencies": {
"bcrypt": "^5.0.1",
"body-parser": "^1.19.0",
"cloudinary": "^1.25.1",
"compression": "^1.7.4",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-rate-limit": "^5.2.6",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.12.3",
"multer": "^1.4.2",
"nodemailer": "^6.5.0",
"nodemailer-express-handlebars": "^4.0.0",
"socket.io": "^4.0.1",
"stripe": "^8.142.0"
},
"devDependencies": {
"#types/express": "^4.17.11",
"#types/jest": "^26.0.22",
"#types/mongoose": "^5.10.4",
"#types/node": "^14.14.37",
"#types/socket.io": "^2.1.13",
"#types/stripe": "^8.0.417",
"#typescript-eslint/eslint-plugin": "^4.20.0",
"#typescript-eslint/parser": "^4.20.0",
"babel-plugin-module-resolver": "^4.1.0",
"dotenv": "^8.2.0",
"eslint": "^7.23.0",
"nodemon": "^2.0.7",
"rimraf": "^3.0.2",
"supertest": "^6.1.3",
"ts-node": "^9.1.1",
"typescript": "^4.2.3"
}
}
Also, this error is only here when I explicitly write the script "test": node src/tests/search.test.ts". If I write "test": "jest", I got this error:
jest: command not found
So, how to properly launch my supertests files, if possible all at once, instead of changing the script line for every file?
You can execute a ts file directly with ts-node.
"test": "ts-node src/tests/search.test.ts"
Or build it to js file and run it with node.
To execute jest command, you have to install it, and you are using Typescript (?) then you also need ts-jest:
npm install ts-jest jest -D
Create a configuration file for jest: jest.config.js
module.exports = {
roots: ['<rootDir>/src'],
testMatch: [
'**/__tests__/**/*.+(ts|tsx|js)',
'**/?(*.)+(spec|test).+(ts|tsx|js)',
],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
};
Now, try to execute jest: npx jest
in package.json:
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"setupFilesAfterEnv": [
"./src/test/setup.ts"
]
},
jest does not understand what typescript is, "ts-jest" will add typescript support.
"setupFilesAfterEnv" is optional. if you have a setup file, it will run this to set up extra configuration like adding "beforeAll, beforeEach" etc.
here is the dependencies you need to install for testing environment:
"devDependencies": {
"#types/jest": "^26.0.15",
"#types/supertest": "^2.0.10",
"jest": "^26.6.1",
"supertest": "^6.0.0",
"ts-jest": "^26.4.3"
}
also add this script to package.json:
"test": "jest --watchAll --no-cache"
--no-cache flag is related to typescript setup. jest sometimes does not catch the changes in typescript files.
lastly you imported server but make sure you set up server.ts properly. here I explained: supertest not found error testing express endpoint
this should set your testing environment
I've been searching and trying to get rid of this error without success. I read the whole documentation of webpack and tried to implement it correctly, but it seems not work. I use expo, maybe there is an issue with the entry of expo, but I don't know.
Here my code:
webpack.config.js
var path = require('path');
module.exports = {
mode: 'production',
entry: "./App.js",
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
};
App.js
import * as React from "react";
import { Provider } from "react-redux";
import { createStore } from "redux";
import { reducers } from "./reducers";
import { Drawer } from "./Navigation";
import LibraryReducer from "./reducers/LibraryReducer";
export default class App extends React.Component {
render() {
const store = createStore(LibraryReducer)
return (
<Provider store={store}>
<Drawer />
</Provider>
);
}
}
package.json
{
"name": "empty-project-template",
"main": "node_modules/expo/AppEntry.js",
"private": true,
"scripts": {
"start": "webpack",
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject",
"build": "webpack --config webpack.config.js"
},
"dependencies": {
"#babel/core": "^7.0.0",
"#babel/plugin-transform-react-jsx-source": "^7.0.0",
"#expo/vector-icons": "^8.0.0",
"babel-preset-expo": "^5.0.0",
"create-webpack-config": "^0.2.1",
"db": "^1.1.1",
"expo": "^31.0.0",
"expo-asset": "^1.0.2",
"expo-constants": "^1.1.0",
"expo-core": "^1.2.0",
"expo-file-system": "^1.1.0",
"expo-font": "^1.1.0",
"expo-react-native-adapter": "^1.0.2",
"loose-envify": "^1.4.0",
"metro-react-native-babel-preset": "^0.50.0",
"native-base": "^2.8.0",
"pirates": "^4.0.0",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-31.0.0.tar.gz",
"react-native-calendars": "^1.20.0",
"react-native-dialog": "^5.3.0",
"react-native-elements": "^0.19.1",
"react-native-haptic-feedback": "^1.2.0",
"react-native-simple-dialogs": "^0.4.1",
"react-native-swipeout": "^2.3.6",
"react-native-vector-icons": "^5.0.0",
"react-native-view-overflow": "^0.0.3",
"react-navigation": "^2.12.1",
"react-redux": "^6.0.0",
"redux": "^4.0.1",
"watchman": "^1.0.0"
},
"devDependencies": {
"webpack": "^4.27.1",
"webpack-cli": "^3.1.2"
}
}
.babelrc
{
"presets": ["module:metro-react-native-babel-preset"]
}
Any idea is welcome.
You probably fixed this long ago, but I had the same problem and fixed it by changing the minify setting to this line in .expo/settings.json:
"minify": false
It was previously set to true.
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"
},