I want to test some JS files that are within a Visual Studio solution.
I've been following https://github.com/MicrosoftDocs/visualstudio-docs/blob/main/docs/javascript/unit-testing-javascript-with-visual-studio.md .
I have a file in /js/Layers.js:
class Layers {
constructor() {
this.xxxLayers = [];
}
}
I have test file in /test/Layers.test.js:
require("./../js/Layers");
describe('Layers class ctor',
function () {
it('should have initialised arrays',
function () {
let sut = new Layers();
expect(true).toBe(true);
});
});
When I run this in Visual Studio (using the Resharper test runner) it passes without raising an error.
My package.json is:
{
"name": "nodejs-web-app1",
"version": "0.0.0",
"description": "NodejsWebApp1",
"main": "server.js",
"author": {
"name": ""
},
"devDependencies": {
"eslint": "^8.21.0"
},
"eslintConfig": {},
"dependencies": {
"mocha": "^10.2.0"
},
"scripts": {
"test": "mocha test"
}
}
When I run npm test I see that the test is run but it does not seem to load the Layers.js file that I wish to test:
$ npm test
> nodejs-web-app1#0.0.0 test
> mocha test
Layers class ctor
1) should have initialised arrays
0 passing (3ms)
1 failing
1) Layers class ctor
should have initialised arrays:
ReferenceError: Layers is not defined
at Context.<anonymous> (test\Layers.test.js:9:27)
at process.processImmediate (node:internal/timers:471:21)
I wonder if Resharper's test runner is filling in a gap that I have missed . Interestingly compared to the advice in https://github.com/MicrosoftDocs/visualstudio-docs/blob/main/docs/javascript/unit-testing-javascript-with-visual-studio.md I cannot see the tests in the VS Test Explorer. only in the Resharper one, although that feels like a different issue. What I want is to be able to run the tests from the command line so I can add it to our CI pipeline.
Can anyone advise?
Thanks.
Related
Hi I am starting with Node Js and tried a code as per the documentation over here. I did everything accordingly but I am getting below error.
const Bumblebee = require('bumblebee-hotword');
^
SyntaxError: Identifier 'Bumblebee' has already been declared
at Loader.moduleStrategy (node:internal/modules/esm/translators:147:18)
at async link (node:internal/modules/esm/module_job:48:21)
Here is the index.js code
import Bumblebee from "bumblebee-hotword";
const Bumblebee = require('bumblebee-hotword');
let bumblebee = new Bumblebee();
// set path to worker files
bumblebee.setWorkersPath('/bumblebee-workers');
// add hotword
bumblebee.addHotword('jarvis');
// set sensitivity from 0.0 to 1.0
bumblebee.setSensitivity(1.0);
bumblebee.on('hotword', function(hotword) {
// YOUR CODE HERE
console.log('hotword detected:', hotword);
});
bumblebee.start();
And here is the package.json
{
"name": "Hotword_template",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"bumblebee-hotword": "^0.2.1"
}
}
Also the required directory "bumblebee-workers" is in the same directory.
I don't know where I am doing wrong, any help is highly appreciated!!
Look at these two lines, you are importing and using constant variable of same name.
import Bumblebee from "bumblebee-hotword";
const Bumblebee = require('bumblebee-hotword');
Changing the name of const variable can solve your problem.
I try to build my first npm package, but now I got stuck.
I want to load from my package a config file that is located in the root of my test-project
I used "npm link my-package" to install it locale for testing (if this important)
This is my folder structure
- my-package
- test-project
- node-modules
- my-package (npm link)
- config.json
The package is a vuejs app that should start a server and serve a page
I run this script from my test-project
"scripts": {
"generatePage": "npm explore my-package -- npm run serve"
},
script from my package
"scripts": {
"serve": "node readFile.js & vue-cli-service serve"
},
my-package/readFile.js
file_path = path.join(process.cwd(), 'config.json')
console.log('file_path', file_path)
If I'm running my script I get this path /Users/name/work/my-package/config.json but I need /Users/name/work/test-project/config.json
How do I get the correct path?
When you write a library that will be used by another code base you cannot be sure where exactly NPM or yarn (or whatever package manager you choose) will install your library. It may be directly in node_modules but it may be nested depending upon the use case. However, as indicated by the stackoverflow answer here the process.main.filename variable will tell us exactly where the main process that is calling us lives. We can use this value to determine where the config file you want to read exists.
Here is some example code:
The config-lib library reads a configuration.
index.js file:
const fs = require("fs");
const path = require("path");
const promisify = require("util").promisify;
const readFilep = promisify(fs.readFile);
module.exports = async function loadConfig(filename = "config.json") {
const configPath = path.resolve(
path.dirname(require.main.filename),
filename
);
console.log("(load-config lib) Config Path: " + configPath);
try {
const data = await readFilep(configPath, "utf8");
return JSON.parse(data);
} catch (err) {
console.log(err);
}
};
This library has a simple package.json
{
"name": "config-lib",
"version": "1.0.0",
"description": "I read a config",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Some Cool Guy <somecoolguy#example.com>",
"license": "MIT"
}
Now the project itself requires this library. Here is the projects package.json
{
"name": "project",
"version": "1.0.0",
"description": "I am the project",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Some Other Cool Guy <someoneelse#example.com>",
"license": "MIT",
"dependencies": {
"config-lib": "file:../lib"
}
}
(The config-lib library in this example was loaded from a local folder.)
The config-lib library requires me, in my project, to create a configuration file called config.json (BTW... name it something more specific to your library as to not collide with a local project configuration if they have one).
Here is an example config.json:
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
Finally, in my project I can use the config-lib library to read my configuration file:
const configLib = require("config-lib");
(async () => {
console.log("Reading config");
const data = await configLib();
console.log("Here is my config: " + JSON.stringify(data));
console.log("key1 value: " + data.key1);
})();
I have a working discord bot, but I would like to compile it into a .exe file. How to do that?
I tried to use pkg and nexe, but both did not make it correctly. Nexe did not even start giving me some strange errors that I did not manage to fix after reading docs and some discussions on the Internet.
Error: https://github.com/nexe/nexe/releases/download/v3.3.3/windows-x64-14.8.0 is not available, create it using the --build flag and Error: vcbuild.bat nosign release x64 exited with code: 1.
The pkg worked better, I got and exe file, but it did not seem to work:
I have this code, where bot is my discord client.
bot.on('ready', () =>
{
console.log(bot.guilds.cache.toJSON());
bot.guilds.cache.forEach((gui) =>
{
console.log(gui.id + ' ' + gui.name + ' ' + gui.memberCount);
});
console.log('Bot started!');
});
It gives one server when I start it using node index.js, but does not give any if I execute the exe file created, though the bot becomes online.
My package.json looks like this:
{
"name": "sth",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"pkg": {
"assets": [ "jsons/**/*", "config.json" ]
},
"bin": "index.js",
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "^12.4.1"
}
}
What am I doing wrong? Why does the bot start, but does not seem to be able to do anything? What have I missed?
i don't know of an .exe file code you can use BUT you can use a .bat file and put it on desktop and create shortcuts or put it on shell:startup in that you will want to write this
#echo off
node "js file path"
Hi everyone I'm beginner in nodejs .I want to test some basic functionality in my code.I m using JEST testing framework.In the command prompt I used npm test I could not test my code it shows
npm ERR! Test failed. Can anyone solve this issue? Thanks in advance...
lib.test.js:
const lib=require('../lib.js');
describe('absolute',()=>{
it('It should return positive number if given number is positive',()=>{
const result=lib.absolute(1);
expect(result).toBe(1);
});
it('It should return positive number if given number is negative',()=>{
const result=lib.absolute(-1);
expect(result).toBe(1);
})
it('It should return zero if given number is zero',()=>{
const result=lib.absolute(0);
expect(result).toBe(1);
});
});
lib.js:
// Testing numbers
module.exports.absolute = function(number) {
if (number > 0) return number;
if (number < 0) return -number;
return 0;
}
package.json:
{
"name": "testing-demo",
"version": "1.0.0",
"description": "",
"main": "db.js",
"directories": {
"test": "tests"
},
"dependencies": {},
"devDependencies": {
"jest": "^22.2.2"
},
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC"
}
I'm guessing the error you are seeing is
SecurityError: localStorage is not available for opaque origins
The default test environment for Jest is a browser-like environment provided by jsdom, which is required for testing code like React applications that are designed to run in the browser.
If you are going to use jsdom as the test environment then you need to set testURL in the Jest configuration, otherwise you will get the error seen above.
If your code is designed to run on node and you don't need a browser-like environment then you can set your test environment to be node.
The easiest way to do that in your case is to pass it as a command line argument to jest in your package.json:
"scripts": {
"test": "jest --env=node"
},
Description
I want to run a .net core NUnit test with the vstest.console.
This is working fine but I want to pass to the "dotnet test" execution a parameter, in my case "--no-build"
How I can parse this parameter to the dotnet execution? I have not found the right runsettings variable
Steps to reproduce
vstest.console.exe " "project.json" /UseVsixExtensions:true /Settings:settings.runsettings
Expected behavior
["C:\Program Files\dotnet\dotnet.exe" test "project.json" --port 55307 --no-build]
Actual behavior
["C:\Program Files\dotnet\dotnet.exe" test "project.json" --port 55307 ]
Environment
windows 7
project.json
{
"version": "1.0.0-*",
"dependencies": {
"NUnit": "3.5.0",
"dotnet-test-nunit": "3.4.0-beta-3"
},
"testRunner": "nunit",
"frameworks": {
"netcoreapp1.0": {
"imports": "portable-net45+win8",
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0-*",
"type": "platform"
}
}
}
}
}`
This was the answer from the vstudio develoment team
https://github.com/Microsoft/vstest/issues/383