I'm developing several node modules which extend the functionality of the Sequelize database ORM module. I'm using Travis to run tests before pushing new versions of my modules to NPM.
Travis allows you to run tests against different versions of node (v0.10, v0.12 etc). Similarly, I'd like to be able to run my tests using various different versions of the Sequelize module. i.e. run the tests once using Sequelize 2.0.0, again using 2.0.1, 2.0.2 etc.
Travis loads modules from NPM automatically according the the versions set in the package.json file, and so only runs tests using the latest module version available according to what's specified in the package.json of my module (in this case ^2.0.0).
Is there any way to interfere with this process and get Travis to run the tests multiple times using a different Sequelize version each time?
I've searched for advice on this on Stack Overflow and elsewhere but not had any luck. But I assume I can't be the only person who wants to do this...
In the end the solution I came up with is to put the following in the travis.yml file:
before_script:
- 'if [ "$SEQ_VERSION" ]; then npm install sequelize#^$SEQ_VERSION.0.0; fi'
env:
- SEQ_VERSION=2
- SEQ_VERSION=3
(with thanks to Sandro Munda for his answer which put me on the right track)
In your package.json, you could have some something like this:
...
"scripts": {
"test-sequelize-2.0.0": "npm install sequelize#2.0.0 && ./node_modules/mocha/bin/mocha",
"test-sequelize-2.1.0": "npm install sequelize#2.1.0 && ./node_modules/mocha/bin/mocha",
"test": "npm run test-sequelize-2.0.0 && npm run test-sequelize-2.1.0"
}
...
Then, run npm test.
test-all-versions is a package that will run a command (you want your test script) for a whole range of semantic versions. so if you want to run your tests against sequelize you might write a config file like this.
#.tav.yml
name: sequelize
versions: ^2.0.0
command: npm run test
This will run your npm test script against all published versions of sequelize that match ^2.0.0 (ie 2.0.0, 2.0.1, etc)
Then in your travis config run the tav command to test against all versions in the given range.
Related
I'm not familiar with npm so I might be holding the wrong end of the shovel here...
There is a package on npm that I would like to modify and use in my own project. The package is angular-crumbs. I forked the source repo (https://github.com/emilol/angular-crumbs) into my own account (https://github.com/capesean/angular-crumbs) and then run npm install capesean/angular-crumbs -force. However, this produces a node_modules folder in my project that hasn't been built (and whatever else - as I understand it) with the commands in the source repo's package.json file:
"build": "npm run clean && npm run transpile && npm run package && npm run minify && npm run copy"
i.e. it doesn't have the types, the correct package.json file, etc.
So my question is, how do I get the properly-built files (including type definitions, etc.) from my own repo to install or build-after-installing in my target project?
I am not sure about what you're trying to do, are trying to work on the
angular-crumbs source code, or are you trying to use it in your own project as a dependencuy ?
Anyway, running npm install will install all your dependencies so that you can directly use them in your project, those dependencies don't need to be built after they are installed.
In your case you seem to have an angular application (which is completely different from node.js), usually to start an angular app you can run ng serve which will build your source code and run an angular server so you can access it on localhost.
I'm aware that I can select the node version to use with NVM but, can I build two angular projects (with different ng version and node version) at the same time without issues? My scenario is a self-hosted build server (Windows) with two agents. Each of these might be, at the same time, on charge of building an Angular app with different version.
Regards
Sure you can, instead of running the globally installed ng run the local one with npx like this npx ng build, npx will use the local installed #angular/cli ng command found under ./node_modules/.bin/ of your project, npx comes installed with npm.
Another option is to add a script in package.json:
"scripts": {
"build": "ng build --prod=true --build-optimizer=true --aot=true",
},
And runt it with npm run build.
As #Andrei stated, is not possible to use NVM to set the Node version at the beginning of pipeline because it will change the version in all open consoles (so, if another pipeline with a different version of Node is running, would be affected).
Luckily, I found an easy workaround which does not require install additional tools or change package.json:
Download the node version you want as zip file
Unzip to a folder in Agent (like C:\LocalNode\node-v17.8.0-win-x64)
Add Node path to PATH environment var only for current pipeline
To add Node path only for current pipeline, we have to add a Powershell task as first task of the pipeline with the current command:
Write-Host ("##vso[task.setvariable variable=Path;]D:\LocalNode\node-v17.8.0-win-x64;$Env:Path")
Rest of the tasks of the pipeline will use the Node version from D:\LocalNode\node-v17.8.0-win-x64
If you want to create a pipeline for a different Node version, just add the version to D:\LocalNode and use the above command with the right path as first task of the pipeline. No problem if both pipelines run at the same time.
I have just started with a mono repo with multiple packages using lerna.
On all my packages I will have unit tests using mocha. But as it stands, only one of my packages is ready for unit tests.
From my understanding, shouldn't I be able to only install mocha on the root package.json and then run lerna run test in order to run it only in the packages that have available test scripts in their num packages, correct?
Unfortunately as it stands this is not working.
I have mocha installed as a devDependency in the root. However, if I do run lerna run test --scope=myPackage I get the following message:
mocha: command not found
Because it is in fact installed in the root, shouldn't it work for all packages? If it isn't the case, what am I missing here? DO I have to install mocha in all of them after all?
Just remove the test script from package.json wherever the unit tests are not ready and lerna wont call it.
I have installed Knex in my Node project and all is wonderful and great... so far...
Now I dig deeper into Knex and am confronted with migrations. All the docs I found talk about running commands like "knex migrate:latest", etc. What I get as a result when I try to run such commands from the (Windows) command line is an error telling me that 'knex' is an unknown command.
I am not a npm and Nodes expert, just enough to get the basics running. When digging into the knex node package I find some configuration for a cli.js file under a 'bin' section in the 'package.json'. I do not understand these configurations, even reading the npm documentation about this 'bin' section does not make it clearer to me.
So here my question:
I am on Windows 10 and have installed a package like 'knex' local to my project. Knex comes with a cli. What do I need to do to call that cli from my console?
You can find client from node_modules/.bin/knex if you haven't installed knex globally (which I don't recommend).
When you install packages locally to some directory, all the "bin" executables are linked automatically under node_modules/.bin. If you use these scripts from package.json scripts, npm automatically adds node_modules/.bin to path, so inside package json you don't have to refer node_modules/.bin/knex but just knex is enough.
In your console, try to $ npx knex migrate:latest
It helped me
First type in "npx knex" to access options and commands available to the knex module. To be able to make use of the Knex cli that comes bundled with it, you then have to access the knex module from whatever path you intend creating the file from. For example, let's say I was in the migrations directory and the node_modules folder is one path higher, I would access the Knex module in it this way '../node_modules/.bin/knex migrate:make create-user-table.js' to be able to create a 'create-user-table.js', migration file. I hope I'm clear enough.
If you have knex installed in your project, you can make it available for use via your shell by adding a simple script to your package.json.
"scripts": {
"knex": "knex"
}
Now you can use the knex cli with npm run knex or, if you use yarn, yarn knex.
How to use Knex CLI
Unix Shell
Locally
npm i knex
NODE_ENV=development npx knex migrate:list
# or
export NODE_ENV=development && npx knex migrate:list
Globally
npm i knex -g
NODE_ENV=development knex migrate:list
# or
export NODE_ENV=development && knex migrate:list
Windows Shell
Locally
npm i knex
set NODE_ENV=development&& npx knex migrate:list
Globally
npm i knex -g
set NODE_ENV=development&& knex migrate:list
Consider using Git Bash Shell which behaves like a Unix shell in addition to including the Windows environment.
I'm having trouble getting Mocha to work as expected, and I'd love to say as documented, but there (appears) to not be much documentation on actually getting the thing running.
I've installed it using npm (both globally and locally), and each time when I run it I get:
$ mocha
mocha: command not found
Ok, so I figured it's not in my PATH, so I tried running it directly,
$ ./node_modules/mocha/bin/mocha
execvp(): No such file or directory
Finally, I tried hitting the other bin file, and got,
$ ./node_modules/mocha/bin/_mocha
path.existsSync is deprecated. It is now called `fs.existsSync`.
.
✔ 1 tests complete (1ms)
How can I just execute my tests with a single command? Vows seems to let you, but I've heard Mocha is the better choice, I just can't seem to get it working correctly.
And any thoughts on the error I got above in my third attempt?
Edit:
I'm running,
Ubuntu 11.10 64-bit
Node.js 0.7.5
npm 1.1.8
mocha 0.14.1
should 0.6.0
since npm 5.2.0, there's a new command "npx" included with npm that makes this much simpler, if you run:
npx mocha <args>
Note: the optional args are forwarded to the command being executed (mocha in this case)
this will automatically pick the executable "mocha" command from your locally installed mocha (always add it as a dev dependency to ensure the correct one is always used by you and everyone else).
Be careful though that if you didn't install mocha, this command will automatically fetch and use latest version, which is great for some tools (like scaffolders for example), but might not be the most recommendable for certain dependencies where you might want to pin to a specific version.
You can read more on npx here
Now, if instead of invoking mocha directly, you want to define a custom npm script, an alias that might invoke other npm binaries...
you don't want your library tests to fail depending on the machine setup (mocha as global, global mocha version, etc), the way to use the local mocha that works cross-platform is:
node node_modules/.bin/mocha
npm puts aliases to all the binaries in your dependencies on that special folder.
Finally, npm will add node_modules/.bin to the PATH automatically when running an npm script, so in your package.json you can do just:
"scripts": {
"test": "mocha"
}
and invoke it with
npm test
After further reading, and confirmation from Linus G Thiel above, I found I simply had to,
Downgrade to Node.js 0.6.12
And either,
Install Mocha as global
Add ./node_modules/.bin to my PATH
To run Mocha with mocha command from your terminal you need to install mocha globally on this machine:
npm install --global mocha
Then cd to your projectFolder/test and run mocha yourTestFileName.js
If you want to make mocha available inside your package.json as a development dependency:
npm install --save-dev mocha
Then add mocha to your scripts inside package.json.
"scripts": {
"test": "mocha"
},
Then run npm test inside your terminal.
While installing the node modules for mocha I had tried the below commands
npm install
npm install mocha
npm install --save-dev mocha
npm install mocha -g # to install it globally also
and on running or executing the mocha test I was trying
mocha test
npm run test
mocha test test\index.test.js
npm test
but I was getting the below error as:
'Mocha' is not recognized as internal or external command
So , after trying everything it came out to be just set the path to environment variables under the System Variables as:
C:\Program Files\nodejs\
and it worked :)
For windows :
Package.json
"scripts": {
"start": "nodemon app.js",
"test": "mocha"
},
then run the command
npm run test
Late answer but I think will work.
Install mocha globally
npm install --global mocha
If you have already installed mocha then set the path to bin