Amplify Jest configuration - jestjs

I need to setup build pipeline in AWS amplify for create react app.I couldn't find code snippet to add unit test that uses Jest with React testing library and typescript. Can you please share the amplify.yml that have jest unit test. I have tried below
version: 1
frontend:
phases:
preBuild:
commands: ['npm ci']
build:
commands: 'npm run test' && 'npm run build'
artifacts:
baseDirectory: build
files:
- '**/*'
cache:
paths:
- 'node_modules/**/*'

To add Jest you should first make sure it's installed and listed in your package.json file.
In the root of your project:
npm install jest --save-dev
You should also add this to package.json:
"scripts": {
"test": "jest"
},
Then run Jest locally by running
npm run test
If that works you just need to ensure that jest is available in your build environment.
Package.json:
{
"name": "Your-app-name",
"author": "",
"license": "",
"homepage": "",
"version": "1.0.0",
"description": "xxx",
"scripts": {
"test": "jest"
},
"devDependencies": {
"jest": "^26.6.3",
"uuid": "^8.3.2"
}
}
Hope it helps you some!

Related

how to run live-server inside project using scripts property of package.json file

I have project and i have installed live-server using npm install --save-dev live-server
I want to now run live-server using scripts property in package.json file.
I know we can run it globally but i want to run live-server within the project folder.
i dont know why you want to do that, but you can add this on package.json
"scripts": {
"live": "live-server"
}
and then run with npm like this:
npm run live
here's an example of package.json:
{
"name": "tailwindcss",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"tailwindcss": "^1.4.6"
},
"devDependencies": {},
"scripts": {
"build:css": "tailwind build src/style.css -o style.css",
"live": "live-server"
},
"keywords": [],
"author": "",
"license": "ISC"
}

Publishing NPM package with np throws error with exit code 128 (Gatsby & rollup)

I've had no problems manually publishing my scoped package to the NPM registry however I found the np module could be used to ensure no steps were skipped in the process.
I've configured only a couple options in my package.json as the defaults are otherwise sensible for me. (This is the package.json for the npm package itself)
{
"name": "#scope/package",
"version": "0.0.6",
"description": "blah",
"main": "index.js",
"scripts": {
"prepublishOnly": "npm run release",
"release": "npm run build:dev && npm run build:prod",
"build:prod": "NODE_ENV=production rollup -c",
"build:dev": "NODE_ENV=development rollup -c",
"test": "echo \"No tests specified\""
},
"repository": {
"type": "git",
"url": "http://blah/blah/blah"
},
"author": "blah",
"license": "ISC",
"devDependencies": {
...
},
"peerDependencies": {
...
},
"np": { <----
"yarn": false, <----
}, <----
"publishConfig": { <----
"access": "public" <----
} <----
}
Unfortunately when I run np I receive the error:
Command failed with exit code 128 (Unknown system error -128): git log --format=%s %h commit sha commit sha..HEAD
The first commit sha is the initial commit from Gatsby.
If you've used Gatsby you may have seen it:
Initial commit from gatsby: (https://github.com/gastbyjs/gatsby-starter-hello-world.git)
For context: I'm creating a component library where the parent dir is setup using GatsbyJS so that I can use it for the demo site while the child dirs under src/packages will be the individual components that will be published to the npm registry. So the demo site is bundled with Gatsby while the packages are bundled using rollup.
Why am I receiving this -128 error code and how do I fix it?

sh: 1: cucumber.js: not found

Trying to use this tutorial here:
https://github.com/lykmapipo/nodejs-cucumber-sample
The output to nvm current is: v10.12.0.
The output to npm --version is: 6.4.1>
I get the error below once I invoke npm test:
> nodejs-cucumber-sample#0.0.1 test /home/gnuc/code/nodejs-cucumber-sample
> cucumber.js
sh: 1: cucumber.js: not found
I am not sure as to why this is the case. The $PATH includes /home/gnuc/.nvm/versions/node/v10.12.0/bin. And I have already used npm install cucumber -g and npm install cucumber
Make sure that your package.json file includes this: "test": "cucumber-js"
So that it looks something like this:
{
"name": "hellocucumber",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": **"cucumber-js"**
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"cucumber": "^5.1.0"
}
}
So, you need to actually call the npm package/library. I have the following defined in my package.json:
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js"
},
you can also add some ---tags in this call.
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js --tags #RegressionTestSuite"
},
this will run any feature files that have #RegressionTestSuite at the top
Also, I have an output/results file created with a time stamp.
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js --tags #RegressionTestSuite --format json:./results/log_new_`date +%Y-%m-%m__%H-%M`.json""
},
I hope this helped.
node ./node_modules/cucumber/bin/cucumber-js
This command is working fine.
And you got sh:1: cucumber.js: not found error means first things please look out the path of cucumber.js

Configure local typescript compiler inside package.json

EDIT #1: seems like I have a working configuration, all suggestions to improve this are welcome though. See answer: https://stackoverflow.com/a/42269408/1155847
ORIGINAL QUESTION:
I'm currently trying to setup my environment so that my package.json's devDependencies typescript version will be used. What are some of the best practices for this, so that it is "editor unaware" and preferably can be used as an npm script, e.g.: npm run tscompile?
To be clear - I can get everything working when using npm install typescript -g - but then I'm relying on a global installed version, whic is not what I want - as we'll want to work in a team and set on a specific typescript version for each member before upgrading, so we're all on the same page.
I'm currently trying to set it up like this - yet npm then complains it doesn't recognize "node_modules" as an internal or external command... I presume I do have to pass the tsconfig.json to tsc as well, or at least give it the "working directory" - but I can't even get past launching tsc from my locally downloaded npm cache.
package.json
{
"name": "tswithnodejsgettingstarted",
"version": "1.0.0",
"description": "",
"main": "app/index.js",
"scripts": {
"start": "node app/index.js",
"tscompile": "node_modules/typescript/tsc"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "2.1.6"
}
}
tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"sourceMap": true,
"outDir": "app"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Ok... It seems like it was as simple as this (see below). Answering it here, for anyone else looking for the answer. Or please let me know if there are better solutions.
Configure the script like "tsc": "tsc" inside package.json. Then just run npm run tsc and it will use your tsc version you have installed locally, and discover your tsconfig.json of course. It doesn't use your global version - as I uninstalled that one - just entering tsc in the command line errors out.
E.g.:
Check the repo* where I was playing with this.
package.json
{
"name": "tscnodejsgettingstarted",
"version": "1.0.0",
"description": "",
"main": "app/index.js",
"scripts": {
"start": "npm run tsc && node app/index.js",
"tsc": "tsc"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "2.1.6"
}
}
*repo: https://github.com/pluralsight-courses/typescript-fundamentals/tree/master/001-GettingStarted
You can also use the prestart script. By default it runs before the start command (see all the default scripts that you can set up here).
"scripts": {
"prestart": "npm run tsc",
"start": "node app/index.js",
"tsc": "tsc"
}

Watcher automation in Mocha

I would like to achieve automation of motcha --watcher feature using package.json file without globally installing mocha.
One of npm features is to allow add custom scripts into npm command. Previously I configured test runner successfully and I can type in bash now:
npm test
Everything works fine, so I would like also do something similar because
./node_modules/mocha/bin/mocha --watch app.js test.js"
is not too efective.
My goal is to run mocha watcher by typing in bash:
npm watch
Unfortunately watcher doesn't run - instead I see standard output of npm command without parameters. It looks like my custom script wasn't registered by npm.
Here is my actual package.json file
{
"name": "screencast",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha test.js", // works properly after typing 'npm test'
"watch": "mocha --watch app.js test.js" // Syntax looks ok, but command 'npm watch' d
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.10.6"
},
"devDependencies": {
"mocha": "^2.0.1",
"supertest": "^0.15.0"
}
}
Anyone had this issue before?
For a 'custom' script like watch, you have to do npm run watch instead of npm watch

Resources