I am trying to setup unit tests for our Docusaurus instance. I am unable to load any of the Docusaurus modules during testing. Another user had the same issue and his fix does allow me to get past the #theme/Layout error.
However, I am also getting the error when I try to test any components that use #docusaurus/router. I am using the router in my code for some page redirects so I need to be able to interact with in my tests.
I'm sure its something I'm doing wrong with how I setup Jest. I suspect me using react-scripts is probably what's causing thing to go crab.
I am able to replicate this using these steps:
npx create-docusaurus#latest unit-tests classic --typescript
yarn add #testing-library/react #types/jest jest react-scripts react-dom --dev
Adding a test script to the package.json.
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start",
"build": "docusaurus build",
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
"clear": "docusaurus clear",
"serve": "docusaurus serve",
"write-translations": "docusaurus write-translations",
"write-heading-ids": "docusaurus write-heading-ids",
"typecheck": "tsc",
"test": "react-scripts test"
},
Then adding a simple unit test:
import { render } from '#testing-library/react';
import Home from './index';
describe('<index>', () => {
it('should', () => {
render(<Home />);
});
});
Here's my entire package.json
{
"name": "unit-tests",
"version": "0.0.0",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start",
"build": "docusaurus build",
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
"clear": "docusaurus clear",
"serve": "docusaurus serve",
"write-translations": "docusaurus write-translations",
"write-heading-ids": "docusaurus write-heading-ids",
"typecheck": "tsc",
"test": "react-scripts test"
},
"dependencies": {
"#docusaurus/core": "2.0.0-beta.18",
"#docusaurus/preset-classic": "2.0.0-beta.18",
"#mdx-js/react": "^1.6.22",
"clsx": "^1.1.1",
"prism-react-renderer": "^1.3.1",
"react": "^17.0.2",
"react-dom": "^18.0.0"
},
"devDependencies": {
"#docusaurus/module-type-aliases": "2.0.0-beta.18",
"#testing-library/react": "^13.1.1",
"#tsconfig/docusaurus": "^1.0.5",
"#types/jest": "^27.4.1",
"jest": "^27.5.1",
"react-scripts": "^5.0.1",
"typescript": "^4.6.3"
},
"jest": {
"coverageReporters": [
"json",
"text",
"lcov"
],
"resetMocks": false
},
"browserslist": {
"production": [
">0.5%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Edit
Its not react-scripts. I changed the yarn script to be just "test": "jest" and I get this error now:
● Test suite failed to run
src/pages/index.tsx:3:20 - error TS2307: Cannot find module '#theme/Layout' or its corresponding type declarations.
3 import Layout from '#theme/Layout';
~~~~~~~~~~~~~~~
src/pages/index.tsx:4:18 - error TS2307: Cannot find module '#docusaurus/Link' or its corresponding type declarations.
4 import Link from '#docusaurus/Link';
~~~~~~~~~~~~~~~~~~
src/pages/index.tsx:5:34 - error TS2307: Cannot find module '#docusaurus/useDocusaurusContext' or its corresponding type declarations.
5 import useDocusaurusContext from '#docusaurus/useDocusaurusContext';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/pages/index.tsx:6:20 - error TS2307: Cannot find module './index.module.css' or its corresponding type declarations.
6 import styles from './index.module.css';
~~~~~~~~~~~~~~~~~~~~
Related
I am setting a web app up in TypeScript and I seem to be missing some basic types I need.
When I compile (npm run build), I get the following errors,
error TS2304: Cannot find name 'HTMLElement'.
error TS2304: Cannot find name 'SVGElement'.
error TS2304: Cannot find name 'EventTarget'.
error TS2304: Cannot find name 'TouchEvent'.
error TS2304: Cannot find name 'MouseEvent'.
error TS2304: Cannot find name 'PointerEvent'.
Based on my Googling I assuming I am missing something basic in my project setup. It seems like these types are just assumed to be there with Typescript.
EDIT: Specially it should be part of the ES6 types, https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts.
Here is my package.json file:
{
"name": "wip",
"version": "1.0.0",
"description": "",
"main": "index.html",
"dependencies": {
"hammerjs": "2.0.8"
},
"devDependencies": {
"#types/chai": "3.4.35",
"#types/mocha": "2.2.39",
"#types/node": "7.0.5",
"#types/hammerjs": "2.0.34",
"chai": "3.5.0",
"mocha": "3.2.0",
"safe-mock": "0.2.0",
"ts-node": "2.1.0",
"tslint": "4.5.1",
"typescript": "2.2.1",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.4.1"
},
"scripts": {
"test": "mocha test --require ts-node/register test/**/*.ts && npm run build",
"dev": "webpack-dev-server --watch --content-base . -d --progress",
"build": "tsc"
},
"author": "",
"license": "ISC"
}
Any suggestions?
Try adding the following lib section to your tsconfig.json file.
{
"compilerOptions": {
"lib": [
"es2016",
"dom"
]
}
}
Additional answer for testing.
If using mocha, you also need to tell mocha about the DOM environment using jsdom.
https://journal.artfuldev.com/unit-testing-node-applications-with-typescript-using-mocha-and-chai-384ef05f32b2
$ npm install jsdom jsdom-global --save-dev
So your "test" script would add -r jsdom-global/register:
{
"scripts": {
"test": "mocha test -r ts-node/register -r jsdom-global/register test/**/*.ts && npm run build"
}
}
I'm attempting to use the mikro-orm CLI but am getting a ts-node error:
$ npx mikro-orm debug
npx: installed 280 in 14.531s
(node:19228) UnhandledPromiseRejectionWarning: Error: Cannot find module 'ts-node'Require stack:
- C:\tmp\nodejs\npm-cache\_npx\19228\node_modules\mikro-orm\dist\cli\CLIHelper.js
- C:\tmp\nodejs\npm-cache\_npx\19228\node_modules\mikro-orm\dist\cli.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1080:15)
at Function.Module._load (internal/modules/cjs/loader.js:923:27)
at Module.require (internal/modules/cjs/loader.js:1140:19)
at require (internal/modules/cjs/helpers.js:75:18)
My package.json has the following:
"scripts": {
"watch": "tsc -w",
"dev": "nodemon dist/index.js",
"start": "node dist/index.js",
"dev2": "nodemon --exec ts-node src/index.ts",
"start2": "ts-node src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1",
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#types/node": "^14.6.3",
"nodemon": "^2.0.4",
"ts-node": "^9.0.0",
"typescript": "^4.0.2"
},
"dependencies": {
"#mikro-orm/cli": "^4.0.0-alpha.0",
"#mikro-orm/core": "^4.0.0-alpha.0",
"#mikro-orm/migrations": "^4.0.0-alpha.0",
"#mikro-orm/postgresql": "^4.0.0-alpha.0",
"pg": "^8.3.3"
},
"mikro-orm": {
"useTsNode": true,
"configPaths": [
"./src/mikro-orm.config.ts",
"./dist/mikro-orm.config.js"
]
}
I'm using the 'watch' and 'dev' scripts to run currently, ts-node also works fine with the script 'start2'
My folder structure is: https://i.stack.imgur.com/7BGMT.png
This is new to me and I can't figure out what the problem is. Do you know what it could be?
Thank You!
answered by #MartinAdámek 'Upgrade all packages to latest version, you are on the very first alpha, latest is rc.6'
The real answer should be that your mikro-orm configuration inside package.json says useTsNode: true. Change it to false and it wont complain about missing ts-node.
I am using the npm module ffmpeg-extract-frames for splitting video frames. It works completely fine when I am testing the module in a vanilla node environment. However, when I import the module in my Electron React desktop app component, it gives me a error when building.
This is how I am importing it:
const extractFrames = require("ffmpeg-extract-frames");
This is the error I am getting when building in my React Electron component:
> expedition-hacks-project#0.1.0 build /home/taylorallen/Desktop/expedition-hacks-project
> set "FLUENTFFMPEG_COV=false" && react-scripts build
Creating an optimized production build...
Failed to compile.
./node_modules/fluent-ffmpeg/index.js
Cannot find module: './lib-cov/fluent-ffmpeg'. Make sure this package is installed.
You can install this package by running: npm install ./lib-cov/fluent-ffmpeg.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! expedition-hacks-project#0.1.0 build: `set "FLUENTFFMPEG_COV=false" && react-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the expedition-hacks-project#0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/taylorallen/.npm/_logs/2020-03-28T22_36_18_100Z-debug.log
My package.json:
{
"name": "expedition-hacks-project",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"bootstrap": "^4.4.1",
"bootstrap-fileinput": "^5.0.8",
"ffmpeg-extract-frames": "^2.0.2",
"fluent-ffmpeg": "^2.1.2",
"jquery": "^3.4.1",
"popper.js": "^1.16.1",
"react": "^16.13.1",
"react-bootstrap": "^1.0.0",
"react-dom": "^16.13.1",
"react-dropzone": "^10.2.2",
"react-icons": "^3.9.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1"
},
"homepage": "./",
"main": "src/main.js",
"scripts": {
"start": "react-scripts start",
"build": "set \"FLUENTFFMPEG_COV=false\" && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"electron": "electron ."
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"electron": "^8.2.0",
"electron-log": "^4.1.0",
"react-app-rewired": "^2.1.5"
}
}
I found a github issue https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/issues/573#issuecomment-305408048 addressing this error, and I set FLUENTFFMPEG_COV=0 on in my build script
My scripts:
"scripts": {
"start": "react-scripts start",
"build": "set \"FLUENTFFMPEG_COV=false\" && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"electron": "electron ."
},
Setting the env variable seems to not do anything, I still get the same error.
I have created a new github repository with files from a working project I downloaded from another github repository of mine. I have run npm install. When I try npm start, I get the following error:
npm ERR! missing script: start
npm ERR! A complete log of this run can be found in:
npm ERR! /home/mycomputername/.npm/_logs/2020-03-22T17_16_37_070Z-debug.log
I have read in other posts that the problem might be in the 'scripts', 'start' package.json file. This is what mine looks like:
{
"name": "myproject",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap-4-grid": "^3.2.0",
"formik": "^2.1.4",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.2.0",
"react-spring": "^8.0.27",
"use-places-autocomplete": "^0.5.4",
"yup": "^0.28.1"
},
"scripts": {
"start": "react-scripts start", //supposedly problematic
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I have tried changing the start script to "start": "index.js" or "start": "App.js" as they're my main file but I have no clue whether I am doing the right thing.
If I run npm run this is what I get
Lifecycle scripts included in mycomputername:
test
echo "Error: no test specified" && exit 1
| npm update check failed │
│ Try running with sudo or get access │
│ to the local update config store via │
│ sudo chown -R $USER:$(id -gn $USER) /home/mycomputername/.config |
Moreover, this repository will be shared with other people -- will they have the same problem when they clone from my repository?
I've got a sample react app I've been messing with to build out little small side projects. But I've hit a connundrum. Ctrl+C doesn't do anything to stop my app. I have to go through task manager manually to kill it. I've attached my package json. Is there some package or approach for this?
{
"name": "practice-app",
"description": "Sample react app to practice different designs, tools and concepts",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^1.2.2",
"#material-ui/icons": "^1.1.0",
"concurrently": "^3.6.0",
"json-server": "^0.14.0",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "concurrently \"react-scripts start\" \"json-server --watch data/db.json --port 3001\"",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:3001"
}
maybe try writing an npm stop script?
"scripts": {
"start": "app.js",
"stop": "pkill --signal SIGINT myApp"
}
Hope that helps