Run a mocha test? - javascript

Good evening, i am learning about testing and have installed mocha. I have a basic test that just compares 2 numbers and i cant get it to run. could anyone explain to me why and how to fix this?
Json
{ "scripts": {
"test": "mocha test/**/*.js"
},
"name": "image-gallery",
"version": "1.0.0",
"description": "",
"main": "script-test.js",
"directories": {
"test": "script-test.js"
},
"author": "",
"license": "ISC"
}
Test.js
const assert = require('assert');
describe('number test', function() {
it('matching numbers', function() {
assert.ok(2 === 2);
});
});

Did you install mocha by running this in your project directory?
npm install --save-dev mocha

Related

Why npm start not working after npm init?

I have initiated a new project with NPM :
npm init
I have installed the socket.io-client package.
package.json:
{ "name": "client", "version": "1.0.0", "description": "", "main": "script.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "socket.io-client": "^4.5.4" } }
script.js:
import { io } from "socket.io-client";
const socket = io('http://localhost:3000')
socket.on('connect', () => {
console.log('Hello - ' + socket.id)
})
The error I get:
npm ERR! Missing script: "start"
I have added the start command to package.json:
"start": "node script.js"
Now I get:
SyntaxError: Cannot use import statement outside a module
I have tried adding start command, and did not worked.
You can try one of these:
Add type="module" to whereever you import your script.
Add "type": "module" to your package.json file.
See more here: "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6

(postcss : not found) error I found when installing tailwindcss for my project

i want to use tailwind css with postcss but I have an issue with installation can anybody solve it?
my package . js is
{
"name": "ninja-food",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "postcss ./src/styles.css -o ./public/styles.css"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^10.4.8",
"postcss": "^8.4.16",
"tailwindcss": "^3.1.8"
}
}
afer tailwind init i didnot get postcss.config.js file so i have manualy create it and put code in it.
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
}
}
but i am again getting error that is saying postcss not found
install postcss using npm npm install postcss and run your project again.

How can I make the ESM work with electron in my project?

I've been trying fix for this a week now but can't really seem to find the problem.
I've followed this tutorial but instead of having that project structure I've my own (see image below)
In the esm.js:
require = require("esm")(module);
module.exports = require("./vickie.js");
Then i've changed vickie.js:
From const { app, BrowserWindow, ipcMain } = require('electron')
To import { app, BrowserWindow, ipcMain } from 'electron'
Then I got this error
In package.json:
{
"name": "vickie",
"type": "module",
"version": "0.0.1",
"description": "",
"main": "./vickie.js",
"scripts": {
"start": "electron ./vickie.js"
},
"author": "Arijanit",
"license": "ISC",
"devDependencies": {},
"dependencies": {
"dotenv": "^8.2.0",
"electron": "^8.2.3",
"electron-builder": "^22.5.1",
"esm": "^3.2.25",
"mysql2": "^1.7.0"
}
}
Why am I getting the error? Should I type in something extra in package.json to enable esm?
Thanks in advance
I created a build tool that lets you use ESM in your own Electron code and modules installed from npm:
https://github.com/mifi/build-electron
To use it:
yarn add -D build-electron concurrently wait-on
Put your Electron main ESM source code in src/main/index.js and preload source in src/preload/index.js.
Add to your package.json:
{
"main": "build/main.js",
"build": {
"files": [
"build/**/*"
]
},
"scripts": {
"start": "concurrently -k \"build-electron -d\" \"wait-on build/.build-electron-done && electron .\"",
"build": "build-electron"
}
Now create a configuration file in your project root build-electron.config.js:
module.exports = {
mainEntry: 'src/main/index.js',
preloadEntry: 'src/preload/index.js',
outDir: 'build',
mainTarget: 'electron16.0-main',
preloadTarget: 'electron16.0-preload',
}
Now you can start developing:
npm run start
And to build your production app:
npm run build && npm exec electron-builder --mac

Protractor screenshot tests error code 105

Protractor tests were running well till I messed around with my config.js file to include screenshot testing.
Errors from running protractor conf.js
C:\Users\hhhhhh\mmmm\tests\e2e>protractor conf.js
[10:50:13] E/configParser - error code: 105
[10:50:13] E/configParser - description: failed loading configuration file conf.js
C:\Users\hhhhhh\AppData\Roaming\npm\node_modules\protractor\built\configParser.js:130
throw new exitCodes_1.ConfigError(logger, 'failed loading configuration file ' + filename);`
package.json
{
"name": "e2e",
"version": "1.0.0",
"description": "yup yup yup",
"main": "conf.js",
"dependencies": {
"protractor-jasmine2-screenshot-reporter": "^0.3.1",
"protractor": "3.3.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
conf.js
var ScreenShotReporter = require('protractor-screenshot-reporter');
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
'browserName': 'chrome'
},
directConnect: true,
framework: 'jasmine2',
specs: ['specs/*spec.js'],
onPrepare: function() {
jasmine.getEnv().addReporter(new ScreenShotReporter({
baseDirectory: 'target/screenshots'
}));
}
};
Many seem to be having issues with this. Anyone found a sound solution?
It looks from your package.json that you are using protractor-jasmine2-screenshot-reporter but you are using protractor-screenshot-reporter in your conf.js. Both are different modules, please use one of those and try again!

Concatenate files with npm as build tool

I recently discovered that I can use npm as a task runner instead of gulp or grunt, everything is fantastic so far (lint, stylus, jade, uglify, watch .. etc) but the concatenation part, I cannot seem to achieve that. With gulp it was something like:
gulp.task('scripts', function() {
return gulp.src('www/js/**/*.js')
.pipe(concat('all.js'))
.pipe(gulp.dest('www/dist'))
.pipe(rename('all.min.js'))
.pipe(uglify())
.pipe(gulp.dest('www/dist'));
});
Is there a way I can do that with npm?
To be more clear, my goal is to do something like this:
// package.json
{
"name": "f_todo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"author": "",
"license": "MIT",
"devDependencies": {
"concat": "^1.0.0",
"rerun-script": "^0.6.0",
"stylus": "^0.53.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"stylus": "stylus ss --compress --out lib/stylesheets",
"concat": "concat dependency code would be here",
"dev": "rerun-script"
},
"watches": {
"stylus": "ss/**"
}
}
try this
var concat = require('concat')
concat(['a.css', 'b.css', 'c.css'], 'all.css')
https://www.npmjs.com/package/concat
and don't forget about npm install concat
By command
use concat-glob-cli
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"concat": "concat-glob-cli -f path/to/**/*.js -o bundle.js",
...
},
https://www.npmjs.com/package/concat-glob-cli
You can also use the power of the shell to do what you want:
"scripts": {
"concat": "cat www/js/**/*.js > all.js"},
Yep, concat is gone. I was looking at this as well while moving away from gulp to pure node and found the package to be missing.
As an alternative I am now using buildify.
Might be a slight overkill, but it works.
var buildify = require('buildify');
var files = [
"./node_modules/moduleA/file1.js",
"./node_modules/moduleB/file2.js",
];
buildify()
.concat(files)
.save("./dist/www/scripts/init.min.js");
I am using concat-files
And I noticed there's also concatenate-files
Both are pretty simple.
Also note writing your own is pretty simple too:
var output = files.map((f)=>{
return fs.readFileSync(f).toString();
}).join(';')
fs.writeFileSync('dist/bundle.js', output)
The concat package is no longer available. I would suggest using concat-with-sourcemaps
https://www.npmjs.com/package/concat-with-sourcemaps
var concat = new Concat(true, 'all.js', '\n');
concat.add(null, "// (c) John Doe");
concat.add('file1.js', file1Content);
concat.add('file2.js', file2Content, file2SourceMap);
var concatenatedContent = concat.content;
var sourceMapForContent = concat.sourceMap;

Categories

Resources