Jest with 'npm test' fails with "Use of const in strict mode" - node.js

I am just trying to use Jest as unit testing framework.
I done and example from official ReactJS side.
function sum(value1, value2) {
return value1 + value2;
}
module.exports = sum;
----------------------------------------------------------------
jest.dontMock('../sum');
describe('sum', function() {
it('adds 1 + 2 to equal 3', function() {
var sum = require('../sum');
expect(sum(1, 2)).toBe(3);
});
});
But when I run npm tests I got next error:
/home/app.local/node_modules/jest-cli/bin/jest.js:12
const fs = require('fs');
^^^^^
SyntaxError: Use of const in strict mode.
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
npm ERR! Test failed. See above for more details.
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
What is going wrong ?

If you read the documentation of the link that you posted, you can see that it says the following:
Jest uses ES2015 features and requires a Node.js version of at least 4.0.0 to run.
You are on version 0.10.25 which is probably why you are having issues. Upgrading your version to >= 4.0.0 should fix this.

Related

Angular giving Syntax Error during initial configuration

I am trying to create an Angular application on a Windows 10 OS. Previously when I attempted to install Node.js and NPM I was getting 'Cannot find 'resolve' of undefined'. I solved it by downgrading my Node.js to 6.17 and the NPM to 3.10. I can now download the Angular CLI but when I use the 'new' command, I get the following error.
C:\Users\user_name\AppData\Roaming\npm\node_modules\#angular\cli\bin\ng:23
);
^
SyntaxError: Unexpected token )
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.runMain (module.js:611:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:160:9)
When I open the file location I get the following:
#!/usr/bin/env node
'use strict';
// Provide a title to the process in `ps`.
// Due to an obscure Mac bug, do not start this title with any symbol.
try {
process.title = 'ng ' + Array.from(process.argv).slice(2).join(' ');
} catch (_) {
// If an error happened above, use the most basic title.
process.title = 'ng';
}
// This node version check ensures that extremely old versions of node are not used.
// These may not support ES2015 features such as const/let/async/await/etc.
// These would then crash with a hard to diagnose error message.
// tslint:disable-next-line: no-var-keyword
var version = process.versions.node.split('.').map(part => Number(part));
if (version[0] < 10 || version[0] === 11 || (version[0] === 10 && version[1] < 13)) {
process.stderr.write(
'Node.js version ' + process.version + ' detected.\n' +
'The Angular CLI requires a minimum Node.js version of either v10.13 or v12.0.\n\n' +
'Please update your Node.js version or visit https://nodejs.org/ for additional instructions.\n',
);
process.exit(3);
}
require('../lib/init');
It's complaining about the parentheses for the if statement. I tried getting rid of the parentheses and it only makes the situation worse. What would be the best way to go about solving this situation. WOuld it be better to use somehow install a new version of Node and work around the 'resolve' error or take with what I have with the version at hand?

Deployment of Node native modules to Azure WebSites,I'm experiencing some problems

I deploy a node application to the Azure website.
The tricky part is that I'm using a node module called "opencv" that needs to be compiled at install time using node-gyp.
I use the node-gyp rebuild --arch=ia32 command to compile it int the windows10. But when I used it in the Azure website, I get following problem:
Wed Apr 26 2017 04:48:35 GMT+0000 (Coordinated Universal Time): Application has thrown an uncaught exception and is terminated:
Error: The specified module could not be found.
\\?\D:\home\site\wwwroot\app\api\lib\opencv.node
at Error (native)
at Object.Module._extensions..node (module.js:434:18)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (D:\home\site\wwwroot\app\api\lib\image_operation.js:5:14)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
This is the code I call:
'use strict';
var fs = require('fs');
var path = require('path');
var opencv = require("./opencv.node");
function isContains(str, substr) {
return new RegExp(substr).test(str);
}
module.exports = {
post: function (imageInfo, data) {
if (isContains(imageInfo.imageName, ".yuv")) {
var imageName = imageInfo.imageName.substr(0, imageInfo.imageName.length - 3) + "jpg";
opencv.yuvToJpeg(path.resolve(__dirname, "../../image/" + imageName), imageInfo.width, imageInfo.height, Array.prototype.slice.call(data, 0));
} else if (isContains(imageInfo.imageName, ".jpg")) {
fs.writeFileSync(path.resolve(__dirname, "../../image/" + imageInfo.imageName), data);
}
return "success";
}
};
But that file is in-fact deployed on the server:
Please help me, and tell me what to do
Its readme says that "You'll need OpenCV 2.3.1 or newer installed before installing node-opencv". And as Azure Web Apps is a Software as a service (SaaS), we don't have the privilege to install arbitrary software there. But if that is a requirement for you, you'll need to use an alternative offering, like Cloud Service or VM.

Npm test Yeoman Custom Generator

some days ago i've started to write a custom yeoman generator and i want to make a test for it (so i can use travis ecc) but i have a problem when i lunch npm test.
My generator copy only 4 files, a gulpfile.js and 3 files each in a different folder so the final result is:
Project-folder
|
|----- gulpfile.js
|--------production
---------css/main.css
---------html/index.html
---------js/index.js
I want to test my generator so i write this test,i start to write it from the template test provided by generator-generator
app.js
'use strict';
var path = require('path');
var assert = require('yeoman-assert');
var helpers = require('yeoman-test');
describe('generator-postcss-template:app', function () {
before(function () {
return helpers.run(path.join(__dirname, '../generators/app'))
.withPrompts({someAnswer: true})
.toPromise();
});
it('creates files', function () {
assert.file([
'production/css/styles.css',
'production/html/index.html'
]);
});
});
When i lunch the npm test i have this error
module.js:471
throw err;
^
Error: Cannot find module 'gulp-eslint'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/run/media/giulio/Lavoro/Personal/generator-postcss-template/gulpfile.js:4:14)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
npm ERR! Test failed. See above for more details.
i don't know how fix it,test of generator-generator is the same but check only if dummyfile was created and test pass with 100%
Some one can help me?
-- Update--
after deletion of all modules and reinstall it the error was changed
new error

Gulp-Sourcemaps, SyntaxError: Unexpected token >

Gulp / npm noobie here.
I'm trying to use gulp-sourcemaps, and for some reason, it crashes on var sourcemaps = require('sourcemaps').(It crash only when this line's in the file)
gulpfile:
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
gulp.task('generateApp', function () {
return gulp.src([some paths...])
.pipe(sourcemaps.init())
.pipe(concat('app.min.js'))
.pipe(uglify())
.pipe(sourcemaps.write())
.pipe(gulp.dest(path...));
});
Error :
C:\Projets\node_modules\strip-bom\index.js:2
module.exports = x => {
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (C:\Projets\node_modules\gulp-sourcemaps\src\init.js:10:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
Has anyone encounter this type of error?
I tried to google it, without any success.
I just started getting the same error and fixed it by replacing the code in C:\Projects\node_modules\strip-bom\index.js with this:
'use strict';
module.exports = function (x) {
if (typeof x !== 'string') {
throw new TypeError('Expected a string, got ' + typeof x);
}
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
// conversion translates it to FEFF (UTF-16 BOM)
if (x.charCodeAt(0) === 0xFEFF) {
return x.slice(1);
}
return x;
};
Then, I had to run npm rebuild node-sass to get it to work again. It seems to be an issue with an older version of the Strip-bom node module.
For more info, check this out: https://github.com/sindresorhus/strip-bom/commit/e2a3c3b83706ee5baac284f3862d3f6b9e1564e5
UPDATED ANSWER:
This error is caused by using an older version of Node. The Strip-bom module is now using ES2015 (ES6) syntax which requires Node 5.0+. (See Node's ES2015 support list here)
To test your version of Node, run:
node -v
If it's less than 5.0, you'll need to update it. You can download the newest version of Node here:
https://nodejs.org/en/
After installing the new version of Node, I still needed to run npm rebuild node-sass to get Gulp up and running again.
The former answer will still work if you don't want to update your Node version, however, as Louis pointed out, manually editing node module files is not a best-practice.

Visual Studio NPM Task Runner fails, but running task outside of VS runs fine

I'm working on bringing a ReactJS app over to a .NET setup. I'm trying to run an NPM script from the task runner (or anywhere within VS.) When I run my build command from the task runner, it fails due to an ES6 arrow function within one of my scripts. However, if I load up cmd.exe or powershell, navigate to the project and run it by hand, it runs perfectly.
Any idea why Visual Studio would be doing this?
The output looks something like this:
> cmd.exe /c npm run build --color=always
> ...cmd1
> ...cmd2
> mocha --reporter spec tools/testSetup.js "Scripts/app/**/*.test.js"
C:\Code\HAL\dev\Test\HALTest\HALReact\tools\testSetup.js:40
Object.keys(document.defaultView).forEach((property) => {
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:217:14)
at Mocha.run (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:469:10)
at Object.<anonymous> (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\bin\_mocha:404:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
The file that it errors on basically just sets up JSDOM:
process.env.NODE_ENV = 'test';
// Register babel so that it will transpile ES6 to ES5
// before our tests run.
require('babel-register')();
// Disable webpack-specific features for tests since
// Mocha doesn't know what to do with them.
require.extensions['.css'] = function () {return null;};
require.extensions['.png'] = function () {return null;};
require.extensions['.jpg'] = function () {return null;};
// Configure JSDOM and set global variables
// to simulate a browser environment for tests.
var jsdom = require('jsdom').jsdom;
var exposedProperties = ['window', 'navigator', 'document'];
global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
});
global.navigator = {
userAgent: 'node.js'
};
documentRef = document; //eslint-disable-line no-undef
Again, I can run this from the CLI directly, and it will work. It has babel-register invoked. I'm just not sure why it wants to error here. If I swap out the arrow function with a regular function, I get other errors in my tests, so it's like the task runner is not able to find babel, or is losing some environmental setting.

Resources