React-router: Test on multiple React versions

Created on 10 Oct 2018  路  7Comments  路  Source: ReactTraining/react-router

I'd like to run our tests on multiple versions of React in CI, if we can.

  • Travis supports build matrices which we can use to run multiple jobs with different environment variables, so that should help.
  • npm supports a --prefix config option that we can use to change (I believe) the location where it looks for package.json and node_modules.

So we may be able to use these 2 features in combination to do something like this in our Travis config:

env:
  - NPM_PREFIX=react-15
  - NPM_PREFIX=react-16
install:
  - npm install --prefix=$NPM_PREFIX

It seems a little hacky, and there would probably be some bugs to work out. But it might work.

stale

Most helpful comment

Quick chime in: this was the only way I found to do it that didn't have the npm equivalent of dll hell. If the way you implement it is similar, let me know, I would be willing to abstract that into an external package for testing against multiple versions of external deps

All 7 comments

We've got this set up on React Redux if you want to steal some ideas: https://github.com/reduxjs/react-redux/tree/master/test @cellog set it up for us.

Ah, I see. Instead of using a build matrix you copy all of the source files into a dir with a package.json that specifies the version of React you want to test against. That's a great approach, and doesn't feel quite as hacky. It also seems like it'd be a little more portable if we decide to switch to yarn in the future instead of npm.

Quick chime in: this was the only way I found to do it that didn't have the npm equivalent of dll hell. If the way you implement it is similar, let me know, I would be willing to abstract that into an external package for testing against multiple versions of external deps

I got a quick start on this in the multi-versions branch, but nowhere near finished yet. The basic idea was to install different versions of React + their dependencies in the /react directory, then use a REACT_VERSION environment variable in the Jest config to setup modulePaths to find React in the right place.

This approach required me to remove react and react-dom from each dir in packages because Jest gives precedence to the local node_modules dir. This caused a few other things to break.

I wonder if there's a good way to tell Jest to search modulePaths before the local node_modules. That would be great in this case.

I resolved it using yarn with jest:

  1. Install multiple versions of react, react-dom, react-test-renderer and enzyme adapters.
  2. Redirect requires in jest setup file.
  3. Add multiple test scripts.
  4. Add extra scripts to CI script.

Dang, this is a bit late.

But here's how I switch between versions for testing:

#!/bin/bash
# ./test.sh
cd ${PROJECT_FOLDER:-./test}

# switch to v15
sed -i -E 's/"react": ".*",/"react": "^15.0.0",/' package.json
npm install 
react-scripts test --env=jsdom

# switch to v16.6
sed -i -E 's/"react": ".*",/"react": "16.6.0",/' package.json
npm install 
react-scripts test --env=jsdom

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hgezim picture hgezim  路  3Comments

davetgreen picture davetgreen  路  3Comments

alexyaseen picture alexyaseen  路  3Comments

tomatau picture tomatau  路  3Comments

ackvf picture ackvf  路  3Comments