同时testing两个不同的npm软件包版本

当我创build一个npm包时,有时候会面临需要向后依赖旧的依赖包版本。

如果新版本有新的API,我可以用这种模式编写代码:

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

而有了这种模式,当我想写testing时,我只能testinginstalled version代码。 其他版本的代码不能被testing。

例如,在React v15和v16中,React v16有了新的API Portal 。 在Portal发布之前,v15有unstable_renderSubtreeIntoContainer api来实现类似的function。

所以React的代码就是这样的:

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

所以我想问问有没有什么方法来testingdifferent dependency version

目前,我想到的一种方法是再次安装其他版本并对其进行testing。 但它只能在本地进行。 它不能在ci上工作,它不能统计在一起。

我认为这不仅是反应testing。 它可能面临node.jstesting。 任何build议都可以讨论。

更新

这个问题可能与在npm中安装two versions dependency有关。 但是我知道目前安装两个版本依赖是不可行的。

这是一个可能的解决scheme,不确定它会如你所愿。 但是,你会有一个前进的方向。

的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" ] } } 

注意在上面的package.json文件中,我已经添加了一个新的关键字customDependecies ,我将用它来安装多个依赖关系。 在这里,我使用body-parser包进行演示。 接下来你需要文件,可以读取这个key并安装代码。

安装-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.`); 

在这里,我在tmp文件夹中逐一安装deps,一旦安装,我将它们移动到./node_modules文件夹中。

一旦安装了所有东西,你可以检查下面的版本

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); 

希望,这将有助于你的目的。

创build3个独立的项目(包含package.json的文件夹)和一个共享文件夹:

  1. 包含testing模块( my-test )的共享文件夹。 导出一个函数来运行testing;
  2. 导入my-test和dependency v1的客户端项目。 在my-test导出调用testing函数的函数。
  3. 导入my-test和dependency v2的客户端项目。 在my-test导出调用testing函数的函数。
  4. 导入两个客户端项目的主项目。 运行每个导出的函数。

你将不得不分别运行它们。 为每个依赖版本创build一个单独的项目文件夹。 防爆。 React10,React11,React12。 每个都有自己的package.json ,指定正确的版本。 在运行integration和/或versionedtesting时,您将在每个版本上运行标准unit testing,但也可能build议将任何版本特定的unit testing添加到该文件夹​​。

在运行完整的testing套件时,创buildmake文件将使您的工作变得更加轻松。 如果你这样做,你可以很容易地将其整合到CI中。