同时测试两个不同的npm包版本

Che*_*Tai 8 javascript testing node.js npm reactjs

当我创建一个npm包时,有时它会面临向后旧的依赖包版本的需要.

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

import pkg from 'some-pkg';
const isNewVersion = pkg.newVersionApi !== 'undefined';

if (isNewversion) {
  pkg.newVersionApi();
} else {
  pkg.oldVersionApi(); // backward compatible api
}
Run Code Online (Sandbox Code Playgroud)

有了这种模式,当我想编写测试时,我只能测试installed version代码.其他版本的代码无法测试.

例如,在React v15和v16中,React v16具有新的API Portal.在Portal发布之前,v15有unstable_renderSubtreeIntoContainerapi实现类似的功能.

所以React的代码就像:

import ReactDOM from 'react-dom';
const isV16 = ReactDOM.createPortal !== 'undefined';

if (isV16) {
  ReactDOM.createPortal(...);
} else {
  ReactDOM.unstable_renderSubtreeIntoContainer(...);
}
Run Code Online (Sandbox Code Playgroud)

所以我想问一下有什么方法可以测试different dependency version吗?

目前,我想到的一种方法是再次安装另一个版本并进行测试.但它只能在当地做.它不能在ci上工作,它不能统计在一起.

我认为这不仅仅是反应测试.它可能面向node.js测试.任何建议都可以讨论.

更新

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

Vip*_*mar 5

这可能是一个解决方案,不确定它是否会按预期工作.但是,你将有一个向前发展的方向.

的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"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,在上面的package.json文件中,我添加了一个新密钥customDependecies,我将用它来安装多个依赖项.在这里,我使用body-parser包进行演示.接下来你需要文件,可以读取key并安装deps.

安装-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.`);
Run Code Online (Sandbox Code Playgroud)

在这里,我在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);
Run Code Online (Sandbox Code Playgroud)

希望,这将符合您的目的.