Check two versions of npm package at the same time

When I create an npm package, sometimes it comes up against the need for an old version of the dependency package.

If the new version has a new api, I can write code in this template:

import pkg from 'some-pkg'; const isNewVersion = pkg.newVersionApi !== 'undefined'; if (isNewversion) { pkg.newVersionApi(); } else { pkg.oldVersionApi(); // backward compatible api } 

And with this template, when I want to write a test, I can only check the installed version code. Other version code cannot be tested.

For a real example, in React v15 and v16, React v16 has a new Portal API. Before the release of the portal, v15 has an unstable_renderSubtreeIntoContainer api for implementing a similar function.

So, the code for React will look like this:

 import ReactDOM from 'react-dom'; const isV16 = ReactDOM.createPortal !== 'undefined'; if (isV16) { ReactDOM.createPortal(...); } else { ReactDOM.unstable_renderSubtreeIntoContainer(...); } 

So, I want to ask if there is any method for testing with different dependency version ?

Currently, one of the methods that I think is installing another version and checking it. But this can only be done locally. He cannot work on ci, and he cannot count on coverage together.

I think this is not just for a reaction test. It may run into the node.js. benchmark. Any suggestion can be discussed.

Update

This question may be related to installing two versions dependency in npm. But I know that currently installing two versions of the dependencies does not work.

+8
javascript npm reactjs testing
source share
3 answers

There may be a solution, I’m not sure that it will work as you expect. But you will have a direction to move forward.

package.json

 { "name": "express-demo", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "cookie-parser": "~1.4.3", "debug": "~2.6.3", "express": "~4.15.2", "jade": "~1.11.0", "morgan": "~1.8.1", "serve-favicon": "~2.4.2", "webpack": "^3.8.1", "webpack-dev-middleware": "^1.12.0", "webpack-hot-middleware": "^2.20.0" }, "customDependecies": { "body-parser": [ "", "1.18.1", "1.18.0" ] } } 

Note in the package.json file above, I added a new customDependecies key, which I will use to install several dependencies. Here I use the body-parser package for a demo. Then you need a file that can read this key and install the depilations.

INSTALL-deps.js

 const {spawnSync} = require('child_process'); const fs = require('fs'); const customDependencies = require('./package.json').customDependecies; spawnSync('mkdir', ['./node_modules/.tmp']); for (var dependency in customDependencies) { customDependencies[dependency].forEach((version) => { console.log(`Installing ${dependency}@${version}`); if (version) { spawnSync('npm', ['install', `${dependency}@${version}`]); spawnSync('mv', [`./node_modules/${dependency}`, `./node_modules/.tmp/${dependency}@${version}`]); } else { spawnSync('npm', ['install', `${dependency}`]); spawnSync('mv', [`./node_modules/${dependency}`, `./node_modules/.tmp/${dependency}`]); } }); customDependencies[dependency].forEach((version) => { console.log(`Moving ${dependency}@${version}`); if (version) { spawnSync('mv', [`./node_modules/.tmp/${dependency}@${version}`, `./node_modules/${dependency}@${version}`]); } else { spawnSync('mv', [`./node_modules/.tmp/${dependency}`, `./node_modules/${dependency}`]); } }); } spawnSync('rm', ['-rf', './node_modules/.tmp']); console.log(`Installing Deps finished.`); 

Here I install deps one by one in the tmp folder and after installation, I move them to the ./node_modules folder.

Once everything is installed, you can check the versions as shown below

index.js

 var bodyParser = require('body-parser/package.json'); var bodyParser1181 = require('body-parser@1.18.1/package.json'); var bodyParser1182 = require('body-parser@1.18.0/package.json'); console.log(bodyParser.version); console.log(bodyParser1181.version); console.log(bodyParser1182.version); 

Hope this serves your purpose.

+4
source share

Create 3 separate projects (folders with package.json) and a shared folder:

  • The shared folder containing the test module ( my-test ). Export a function to run a test;
  • Import my-test project client and v1 dependency. Export the function that calls the test function in my-test .
  • Import my-test project client and v2 dependency. Export the function that calls the test function in my-test .
  • A master project that imports both client projects. Run each exported function.
0
source share

You will have to run them separately. Create a separate project folder for each version of the dependencies. Ex. React10, React11, React12. Each of them will have its own package.json specified for the correct version. When you run integration and / or versioned tests, you will run your standard unit tests for each version, but it may also be advisable to add any tests related to a specific version to this folder.

Creating a make file will simplify your life when you run the full test suite. If you do this, you can easily integrate it into CI.

0
source share

All Articles