NX workspace - angular 14 - jest not working - ts-jest

I'm trying to upgrade my angular 13 NX workspaces to angular 14, but somehow I can't get my jest unit tests to work properly. I tried the following from zero:
Install #angular/cli and nx globally:
npm install --global #angular/cli#latest
npm install --global nx#latest
Open and cmd in a path without spaces
npx create-nx-workspace
> test
> angular
> demo
> scss
Result:
Nx is creating your v13.10.5 workspace
So now I could migrate to the latest NX workspace version
cd test
nx migrate latest
The #angular/cli version is still on 13, so I changed this in the root package.json to 14.0.1 (nx removes the version annotations ~ and ^...)
npm install doesn't work out. jest-preset-angular#11.1.2 requires #angular-devkit/build-angular#">=0.1002.4" while in the workspace this is a dependency fixed at #angular-devkit/build-angular#"14.0.1":
npm ERR! Found: #angular-devkit/build-angular#13.3.7
npm ERR! node_modules/#angular-devkit/build-angular
npm ERR! dev #angular-devkit/build-angular#"14.0.1" from the root project
npm ERR! peer #angular-devkit/build-angular#">=0.1002.4" from jest-preset-angular#11.1.1
npm ERR! node_modules/jest-preset-angular
npm ERR! dev jest-preset-angular#"11.1.2" from the root project
So I run update instead:
npm update
npx nx migrate --run-migrations
I'm able to run the project
npm start -- --open
Before we can run the command for the unit tests, add the following script under the root package.json scripts section:
{
...,
"scripts: {
...,
"nx": "nx"
}
}
Now I'm unable to run the unit tests:
npm run nx run-many -- --target=test --projects=demo --watch=false --browsers=ChromeHeadless
FAIL demo apps/demo/src/app/app.component.spec.ts
● Test suite failed to run
Cannot find module '#angular/core/testing' from '../../node_modules/jest-preset-angular/build/config/setup-jest.js'
Require stack:
C:/repos/b/test/node_modules/jest-preset-angular/build/config/setup-jest.js
C:/repos/b/test/node_modules/jest-preset-angular/setup-jest.js
src/test-setup.ts
at Resolver.resolveModule (../../node_modules/jest-resolve/build/resolver.js:324:11)
Note that the module is definitely installed:
Other Q&A's suggest updating jest-preset-angular, so I did. I changed it to 12.1.0 in the package.json. This requires jest#28.1.1, so changed that too. And ts-jest should be at 28.0.5.
Running npm update succeeds now. But running the unit tests
npm run nx run-many -- --target=test --projects=demo --watch=false --browsers=ChromeHeadless
now gives the following errors:
ReferenceError: document is not defined
NG0200: Circular dependency in DI detected for TestComponentRenderer. Find more at https://angular.io/errors/NG0200
NG0200: Circular dependency in DI detected for InjectionToken DocumentToken. Find more at https://angular.io/errors/NG0200
...
It litterally suggests the following:
The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
Consider using the "jsdom" test environment.
So I installed it:
npm i --save-dev jest-environment-jsdom
And updated the root jest.config.js:
module.exports = {
projects: getJestProjects(),
testEnvironment: 'jest-environment-jsdom'
};
This still gives me the same error. I also tried with testEnvironment: 'jsdom' without success. So how can I fix this? Or do I have to wait until the NX developers release a new stable version?

As of today (like just now...) I was able to upgrade to 14.
nx migrate latest
Open the root package.json file
restore the ~ and ^
remove the #angular/eslint packages which are still on 13
manually change #angular/cli to 14.0.0
Then npm update and npx nx migrate --run-migrations were working as expected. You'll see warnings about the tsconfig. You need to open each library's project.json and add
"targets:build:options:tsConfig": "libs/my-lib/tsconfig.lib.json"
The test section doesn't need modifications, it would say that the tsconfig field there is deprecated.
This is my final root package.json
{
"name": "mintplayer-ng-bootstrap",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "nx",
"nx": "nx",
"postinstall": "node ./decorate-angular-cli.js && ngcc --properties es2020 browser module main",
"start": "nx serve",
"build": "nx build",
"test": "nx test"
},
"private": true,
"dependencies": {
"#angular/animations": "~14.0.2",
"#angular/cdk": "~14.0.1",
"#angular/common": "~14.0.2",
"#angular/compiler": "~14.0.2",
"#angular/core": "~14.0.2",
"#angular/forms": "~14.0.2",
"#angular/platform-browser": "~14.0.2",
"#angular/platform-browser-dynamic": "~14.0.2",
"#angular/router": "~14.0.2",
"#mintplayer/ng-pagination": "~14.0.0",
"#nrwl/angular": "14.3.5",
"bootstrap": "^5.1.3",
"bootstrap-icons": "^1.7.2",
"ngx-highlightjs": "~6.1.1",
"qrcode": "^1.5.0",
"rxjs": "~7.4.0",
"tslib": "^2.0.0",
"zone.js": "~0.11.4"
},
"devDependencies": {
"#angular-devkit/build-angular": "~14.0.2",
"#angular/cli": "~14.0.0",
"#angular/compiler-cli": "~14.0.2",
"#angular/language-service": "~14.0.2",
"#nrwl/cli": "14.3.5",
"#nrwl/cypress": "14.3.5",
"#nrwl/eslint-plugin-nx": "14.3.5",
"#nrwl/jest": "14.3.5",
"#nrwl/linter": "14.3.5",
"#nrwl/workspace": "14.3.5",
"#types/jest": "27.4.1",
"#types/node": "16.11.7",
"#types/qrcode": "^1.4.2",
"#typescript-eslint/eslint-plugin": "~5.24.0",
"#typescript-eslint/parser": "~5.24.0",
"cypress": "^9.1.0",
"eslint": "~8.15.0",
"eslint-config-prettier": "8.1.0",
"eslint-plugin-cypress": "^2.10.3",
"jest": "27.5.1",
"jest-preset-angular": "11.1.2",
"ng-packagr": "^14.0.2",
"postcss": "^8.4.5",
"postcss-import": "^14.1.0",
"postcss-preset-env": "^7.5.0",
"postcss-url": "^10.1.3",
"prettier": "^2.6.2",
"ts-jest": "27.1.4",
"typescript": "~4.7.3",
"nx": "14.3.5"
}
}
The related project is hosted here

Related

Getting a "o is not a function" error only when doing a production build in vue 2.7.14

I'm working on a Laravel 5.7 project that has been through a few different developers. Recently one of the devs did an npm update that caused a few issues. I've got most all of those issues cleaned up, but now we are seeing an "o is not a function" error any time we try and access a variable in a vue component. This issue only shows up after an npm run prod. If we do an npm run dev everything works fine and the site looks perfect.
This has been a nightmare to debug since it only shows up in the minified production build. I've tried using a few different versions of npm and some different minor releases of vue. I know it's happening whenever we access a variable because I removed everything except for a #{{ status }} call inside of the component. Without that I get no error and with it I get that "o is not a function" error.
Here are the dependencies in our package.json:
"devDependencies": {
"axios": "^0.18.1",
"bootstrap": "^4.3.1",
"cross-env": "^5.1",
"jquery": "^3.6.2",
"laravel-mix": "^2.1.14",
"lodash": "^4.17.15",
"popper.js": "^1.16.1",
"vue": "^2.7.14",
"vue-template-compiler": "^2.7.14"
},
"dependencies": {
"axios-retry": "^3.1.2",
"codemirror": "^5.65.10",
"es6-promise": "^4.2.8",
"inputmask": "^4.0.8",
"mjml": "^4.4.1",
"moment": "^2.24.0",
"npm": "^6.11.2",
"signature_pad": "^3.0.0-beta.3",
"spectrum-colorpicker": "^1.8.0",
"vue-ckeditor2": "^2.1.5",
"vue-markdown": "^2.2.4",
"vue-simplemde": "^1.1.2",
"vue2-dropzone": "^3.6.0",
"vuejs-datepicker": "^1.6.2",
"vuex": "^3.6.2"
}
I run into the same problem, so what i did is removing vue-bootstap package
if you changed or updated version of laravel-mix or wabpack restore the inial ones

I can't run 'npm run dev' since Laravel updated with Vite

Taylor Otwell announced that new Laravel projects now will run with Vite and that Vite is installed by default. I can't seem to be able to run dev environment 'npm run dev'
I installed new laravel project, installed Laravel JetStream with SSR and teams support hit the 'npm install command'.
Every time I run npm run dev it shows this:
And if I open the local link, it shows this:
Why can't I user npm run dev and compile my files?
This is package.json for my brand new laravel app
{
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build && vite build --ssr"
},
"devDependencies": {
"#inertiajs/inertia": "^0.11.0",
"#inertiajs/inertia-vue3": "^0.6.0",
"#inertiajs/progress": "^0.2.7",
"#inertiajs/server": "^0.1.0",
"#tailwindcss/forms": "^0.5.2",
"#tailwindcss/typography": "^0.5.2",
"#vitejs/plugin-vue": "^2.3.3",
"#vue/server-renderer": "^3.2.31",
"autoprefixer": "^10.4.7",
"axios": "^0.25",
"laravel-vite-plugin": "^0.2.1",
"lodash": "^4.17.19",
"postcss": "^8.4.14",
"tailwindcss": "^3.1.0",
"vite": "^2.9.11",
"vue": "^3.2.31"
}
}
and if I try hitting 'vite' in the terminal I get this:
If you don't want to use vite but mix instead in your new laravel project, you can just get the usual behavior of npm run dev back with the following changes:
Install Laravel Mix (because by the new installation it is not there anymore):
npm install --save-dev laravel-mix
Create a webpack.mix.js file, if it is not there, and make sure it has the following content:
const mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel applications. By default, we are compiling the CSS
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', [
//
]);
Update package.json:
"scripts": {
- "dev": "vite",
- "build": "vite build"
+ "dev": "npm run development",
+ "development": "mix",
+ "watch": "mix watch",
+ "watch-poll": "mix watch -- --watch-options-poll=1000",
+ "hot": "mix watch --hot",
+ "prod": "npm run production",
+ "production": "mix --production"
}
Remove vite helper functions (if they are there):
- import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
createInertiaApp({
title: (title) => `${title} - ${appName}`,
- resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
+ resolve: (name) => require(`./Pages/${name}.vue`),
setup({ el, app, props, plugin }) {
return createApp({ render: () => h(app, props) })
.use(plugin)
.mixin({ methods: { route } })
.mount(el);
},
});
Update environment valiables (in .env, VITE_ prefix to MIX_):
- VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
- VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
+ MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
+ MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Remove Vite and the laravel Plugin
npm remove vite laravel-vite-plugin
Remove the Vite config file:
rm vite.config.js
Remove these paths from .gitignore:
- /public/build
- /storage/ssr
If you created some code already with vite, you must have some more changes in your blade files, check out this article. But if it is a new project, you just good to go.
For anyone experiencing the problem:
With Vite, running npm run dev will only build your frontend and start watching any changes to your code to rebuild automatically.
To actually start your server, you need to run php artisan serve in a separate command window.
Source (See With Laravel section): https://laravel-vite.dev/guide/essentials/development.html#with-laravel
Had the same issue, but none of the mentioned solutions worked for me. Instead I saw an issue with the <script> src's in the head-section of the rendered html.
screenshot of script src's buggy
Added in vite.config.js the following code which solved the issue.
server: {
hmr: {
host: 'localhost',
},
}
Edit:
The issue was reported in laravel's vite-plugin repo and it will be fixed with this PR
I was having the same issue, I did the following and it finally worked!
I did:
Upgraded my Laravel Project to Latest (v9.19.0). Infact i upgraded all my packages to latest one too.
Remove the node_modules and install the dependency using npm install
Make sure you follow the upgrade guides properly.
Run the server using php artisan serve
And run the dev server using npm run dev
If you done properly, it should start the dev server and all your javascript code should compile. (If it succeed, you will see the desired output.)
I fixed the issue in the above steps.
Vite needs an updated node version.
You can download the latest node version then run npm install and npm run dev
To create the server you can use php artisan serve
If you are using laragon as a local deployment you can set the --host flag to the virtual host url of the app ,it worked for me
Try:
.env :
APP_URL=http://localhost:8000
welcome.blade.php :
<head>
<title>my project</title>
#vite(['/resources/js/app.js', '/resources/css/app.css'])

Error when deploying SvelteKit to Vercel while npm run preview works fine locally

I have a problem where I get the following error when deploying my SvelteKit website to Vercel:
> Using #sveltejs/adapter-auto
18:05:29.787 > 404 /about (linked from /)
18:05:29.787 at file:///vercel/path0/node_modules/#sveltejs/kit/dist/chunks/index5.js:167:11
18:05:29.787 at visit (file:///vercel/path0/node_modules/#sveltejs/kit/dist/chunks/index5.js:326:5)
18:05:29.802 Error: Command "npm run build" exited with 1
The website works fine both in dev and build environment locally.
I have been trying all day to fix this and in the process broke my latest website version, which now gives a different error when using npm run dev locally:
6:29:51 PM [vite] Internal server error: File to import not found or unreadable: ../scss/components/footer.
Plugin: vite-plugin-svelte
File: /Users/koenraijer/Documents/Code/Svelte/kaashuys/src/lib/footer/Footer.svelte
6:29:51 PM [vite] Internal server error: File to import not found or unreadable: ../scss/components/nav.
Plugin: vite-plugin-svelte
File: /Users/koen/Documents/Code/Svelte/kaashuys/src/lib/header/Header.svelte
6:29:51 PM [vite] Internal server error: File to import not found or unreadable: ../scss/components/footer.
Plugin: vite-plugin-svelte
File: /Users/koen/Documents/Code/Svelte/kaashuys/src/lib/footer/Footer.svelte
6:29:51 PM [vite] Internal server error: File to import not found or unreadable: ../scss/components/nav.
Plugin: vite-plugin-svelte
File: /Users/koen/Documents/Code/Svelte/kaashuys/src/lib/header/Header.svelte
This exact same nav file was imported perfectly fine in the version used to generate the first error message. All of these files are present.
When running npm run preview, suddenly it does work.
First part of my package-lock.json:
{
"name": "blog-template",
"version": "0.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "blog-template",
"version": "0.0.1",
"dependencies": {
"#fontsource/fira-mono": "^4.5.0",
"#lukeed/uuid": "^2.0.0",
"cookie": "^0.4.1",
"scss": "^0.2.4"
},
"devDependencies": {
"#sveltejs/adapter-auto": "next",
"#sveltejs/adapter-static": "^1.0.0-next.26",
"#sveltejs/kit": "next",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-svelte3": "^3.2.1",
"mdsvex": "^0.9.8",
"node-sass": "^7.0.1",
"postcss": "^8.4.5",
"prettier": "^2.4.1",
"prettier-plugin-svelte": "^2.4.0",
"svelte": "^3.44.0",
"svelte-dark-mode": "^2.0.0",
"svelte-preprocess": "^4.10.1",
"typescript": "^4.5.4"
}
},
Other things I've tried:
run npm audit, update outdated packages etc.
removed everything scss related, reinstalled svelte-preprocess.
checked if removing the sveltePreprocess config scss: prependData line did anything.
Incrementally added newer parts to the earlier version that did work, but I didn't find manage to find the perpetrator.
I don't know how I broke it or how to fix it...

.env variables return undefined in Vue components inside a Laravel project

Trying to get the .env variables to work inside Vue components in an Laravel project but the just return as undefined. I have tried npm run watch, npm run dev, artisan serve but no result.
package.json
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "mix",
"watch": "mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"production": "mix --production"
},
"devDependencies": {
"#intlify/eslint-plugin-vue-i18n": "^0.11.1",
"#vue/compiler-sfc": "^3.0.11",
"autoprefixer": "^10.2.5",
"axios": "^0.19.2",
"bootstrap": "^4.6.0",
"eslint": "^7.27.0",
"jquery": "^3.6",
"laravel-mix": "^6.0.18",
"lodash": "^4.17.19",
"node-sass": "^6.0.0",
"popper.js": "^1.16.1",
"postcss": "^8.2.15",
"resolve-url-loader": "^3.1.2",
"sass": "^1.32.11",
"sass-loader": "^10.0.0",
"tailwindcss": "^2.1.2",
"vue": "^3.0.5",
"vue-loader": "^16.1.2",
"vue-template-compiler": "^2.6.12",
"webpack-cli": "^4.7.0"
},
"dependencies": {
"#vue/cli": "^4.5.13",
"browser-sync": "^2.26.14",
"browser-sync-webpack-plugin": "^2.3.0",
"npm-watch": "^0.9.0",
"postcss-import": "^14.0.2",
"vue-i18n": "^9.1.6",
"vue-router": "^4.0.8",
"vue3-click-away": "^1.2.1",
"vuex": "^4.0.0"
}
}
.env
VUE_APP_I18N_SUPPORTED_LOCALE=en,de,nl
Component.vue
console.log(process.env.VUE_APP_I18N_SUPPORTED_LOCALE)
You'll have to prefix the variable in your .env with "MIX" like this: MIX_VUE_APP_I18N_SUPPORTED_LOCALE=en,de,nl, then you can call it by doing console.log(process.env.MIX_VUE_APP_I18N_SUPPORTED_LOCALE).
read more on the Laravel doc
Also, remember to restart your server, recompile the vue app and then you should be good to go.
You may inject environment variables into your webpack.mix.js script by prefixing one of the environment variables in your .env file with MIX_:
MIX_SENTRY_DSN_PUBLIC=http://example.com
After the variable has been defined in your .env file, you may access it via the process.env object. However, you will need to restart the task if the environment variable's value changes while the task is running:
process.env.MIX_SENTRY_DSN_PUBLIC
I've tested it and it works for Laravel 5.7 and Vue 2.6.
Reference:
https://laravel.com/docs/8.x/mix#environment-variables
process.env holds the actual environment variables available to the node.js process when it is started.
If you want to load the values from .env and have your node process treat them just as regular environment variables, you could use one of the existing npm packages, eg. dotenv.
But beware:
The values are only available (and "frozen") at build time
You might expose confidential data like api keys or other secrets to the client, if you're not careful

WebPack devServer - localhost refused to connect

I have configured WebPack for my application and added webpack-dev-server. It compiles successfully after running and gives me "Project is running at http://localhost:9000/". However, when I try to open it in my browser I get a "This site can't be reached" page with the following error:
ERR_CONNECTION_REFUSED
Any fix suggestions will be greatly appreciated!
Snippets from my package.json file:
"scripts": {
"build": "webpack --config webpack.config.js",
"start:dev": "webpack-dev-server"
"devDependencies": {
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1",
Snippet from my webpack.config.js file:
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000
}
For those who might have the same issue, I seem to have found a solution shortly after posting this question. Here it is: link.

Resources