-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RW-5959][risk=no] Upgrade jest to latest version #4733
Conversation
@@ -7,7 +7,7 @@ import {Runtime} from 'generated/fetch'; | |||
import {RuntimeStatus} from 'generated/fetch'; | |||
import {RuntimeApi} from 'generated/fetch/api'; | |||
import SpyInstance = jest.SpyInstance; | |||
import expect = jest.Expect; | |||
import { expect } from '@jest/globals'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ui/package.json
Outdated
@@ -25,7 +25,7 @@ | |||
"./test-shim.js", | |||
"./test-setup.js" | |||
], | |||
"setupTestFrameworkScriptFile": "./test-setup-script.js", | |||
"setupFilesAfterEnv": ["./test-setup-script.js"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jest deprecated setupTestFrameworkScriptFile
in favor of new setupFilesAfterEnv
(#7119)
@@ -248,7 +248,7 @@ export const CohortSearch = withCurrentCohortSearchContext()(class extends React | |||
clearTimeout(this.growlTimer); | |||
} | |||
// This is to set style display: 'none' on the growl so it doesn't block the nav icons in the sidebar | |||
this.growlTimer = setTimeout(() => this.setState({growlVisible: false}), 2500); | |||
this.growlTimer = global.setTimeout(() => this.setState({growlVisible: false}), 2500); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolving typescript issue
@@ -7,12 +7,10 @@ | |||
// No outDir needs to be specified here, as test runs will not produce output files anyway. | |||
"baseUrl": "./", | |||
"module": "commonjs", | |||
"target": "es5", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
target: "es6"
inherited from ../tsconfig.json
.
"types": [ | ||
"jest", | ||
"node" | ||
], | ||
"jsx": "react" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inherited from ../tsconfig.json
"typeRoots": [ | ||
"node_modules/@types" | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defining typeRoots
is unnecessary. It is part of the default value.
https://www.typescriptlang.org/tsconfig#typeRoots
if (valueKey && !Array.isArray(valueKey)) { | ||
valueKey = [valueKey]; | ||
} | ||
if (valueKey && valueKey.length > 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The profile-page.spec.tsx
was failing consistently after versions upgrade. @ch helped me and figured out it was caused by a bug here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good - but would like @petesantos' review on the line I mentioned before merging in case I'm missing something.
@@ -433,7 +433,10 @@ export const ProfilePage = fp.flow( | |||
|
|||
const makeProfileInput = ({title, valueKey, isLong = false, ...props}) => { | |||
let errorText = profile && errors && errors[valueKey]; | |||
if (valueKey && Array.isArray(valueKey) && valueKey.length > 1) { | |||
if (valueKey && !Array.isArray(valueKey)) { | |||
valueKey = [valueKey]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@petesantos can you review this specific change? Based on my reading of the previous code, L444 when the valueKey
is 'givenName'
, the array expansion should equate to:
this.setState(fp.set(['currentProfile', 'g', 'i', 'v', 'e', 'n', 'N', 'a', 'm', 'e'], v))
With Alex's change, the test started failing from this somehow. Do you have ideas on what's going on here? I'm still puzzled as to how the current code is functional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interesting, it looks like JS and TS handle spread differently. When the TS compiles down to javascript it must do something to prevent strings from getting split apart on a spread. The new code looks more correct and in-line with what I would expect. I will put together a code example demonstrating this issue. What was the specific error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UI unit profile-page test failure:
FAIL src/app/pages/profile/profile-page.spec.tsx
● ProfilePageComponent › should invalidate inputs correctly
expect(received).toBeTruthy()
Received: false
88 | const wrapper = component();
89 | wrapper.find(TextInput).first().simulate('change', {target: {value: ''}});
> 90 | expect(wrapper.find(TextInput).first().prop('invalid')).toBeTruthy();
| ^
91 | });
92 |
93 | it('should display/update address', async() => {
at Object.<anonymous> (src/app/pages/profile/profile-page.spec.tsx:90:61)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks - it should be possible to set up a more isolated reproducer here (the above failure is highly detached from the cause, and required some trial and error to trace down), I can try and help set this up as well if you'd like.
For the purposes of this PR I think it should be fine to merge now. Pete's comment of "The new code looks more correct and in-line with what I would expect" answers what I was looking for, which is to make sure I wasn't missing something obvious and that the change makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is the issue:
This Typescript code (in version 3.2.z)
const checkValueKey = (valueKey) => {
console.log(['test', ...valueKey])
}
checkValueKey('test')
compiles to this in javascript:
var checkValueKey = function (valueKey) {
console.log(['test'].concat(valueKey));
};
checkValueKey('test');
When using concat
as a spread operator you will see the behavior we are seeing in the code. The string item in the concat
will not be split apart.
However, the same code when compiled in TS 4.2.3 yields:
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
var checkValueKey = function (valueKey) {
console.log(__spreadArray(['test'], valueKey));
};
checkValueKey('test');
Where the __spreadArray
function more closely follows what JS does with the spread operator, splitting apart the string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice find! Thanks. That seems like the likely culprit.
Something that's still not obvious to me though is why the typescript version changed with this PR, that seems like an unexpected side effect. My best guess is that changing ts-jest
changed the Typescript version behind the scenes for tests, but if so this interaction is not obvious from yarn.lock
or from the ts-jest docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like "ts-jest": "^26.5.4"
uses TypeScript 4.x. This is probably why this error cropped up in the test.
This is a caveat we should be aware of - the jest tests will be using a different version of typescript from our code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more update - I was wondering what our ES target version was, and whether a change to that would prevent substituting the spread operator at all, I don't think there is any browser we are targeting that doesn't support this.
Looking at the tsconfig.json
L13 we are changing our ES target to es6
in this change.
Prior to this change, the code in question would compile to this (note the concat) :
return _this.setState(lodash_fp__WEBPACK_IMPORTED_MODULE_1__["set"](['currentProfile'].concat(valueKey), v));
and it now compiles to:
onChange: v => this.setState(lodash_fp__WEBPACK_IMPORTED_MODULE_1__["set"](['currentProfile', ...valueKey], v)),
I am curious as to whether the error popped up as a result of the target change, or the jest upgrade. In other words - is jest using a different typescript version from our build?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright... Final update... just for a full understanding of what is happening.
It looks like ts-jest is using our version of TS, the real issue is with the target
change in the config and how it effects the resulting Javascript that comes out.
This is what I tested to confirm the behavior of the tests:
ts-jest version | target | result
----------------------------------------------
25.3.0 | es5 | pass (because it uses concat )
25.3.0 | es6 | fail (uses spread operator)
26.5.4 | es5 | pass (uses concat) --> If this were using 4.x of TS it would fail by using `__spreadArray`
26.5.4 | es6 | fail (uses spread operator)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% confident but it looks to me that there was one usage of valueKey that this missed:
valueKey: 'verifiedInstitutionalAffiliation.institutionDisplayName'
-> #4822
@@ -7,7 +7,7 @@ import {Runtime} from 'generated/fetch'; | |||
import {RuntimeStatus} from 'generated/fetch'; | |||
import {RuntimeApi} from 'generated/fetch/api'; | |||
import SpyInstance = jest.SpyInstance; | |||
import expect = jest.Expect; | |||
import { expect } from '@jest/globals'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: general convention in the codebase (and this file) is {expect}
instead of { expect }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻
@@ -433,7 +433,10 @@ export const ProfilePage = fp.flow( | |||
|
|||
const makeProfileInput = ({title, valueKey, isLong = false, ...props}) => { | |||
let errorText = profile && errors && errors[valueKey]; | |||
if (valueKey && Array.isArray(valueKey) && valueKey.length > 1) { | |||
if (valueKey && !Array.isArray(valueKey)) { | |||
valueKey = [valueKey]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright... Final update... just for a full understanding of what is happening.
It looks like ts-jest is using our version of TS, the real issue is with the target
change in the config and how it effects the resulting Javascript that comes out.
This is what I tested to confirm the behavior of the tests:
ts-jest version | target | result
----------------------------------------------
25.3.0 | es5 | pass (because it uses concat )
25.3.0 | es6 | fail (uses spread operator)
26.5.4 | es5 | pass (uses concat) --> If this were using 4.x of TS it would fail by using `__spreadArray`
26.5.4 | es6 | fail (uses spread operator)
ui/package.json
Outdated
@@ -117,7 +121,7 @@ | |||
"serialize-javascript": "^3.1.0", | |||
"stackdriver-errors-js": "^0.4.0", | |||
"stacktrace-js": "^2.0.0", | |||
"ts-jest": "^25.3.0", | |||
"ts-jest": "^26.5.4", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be moved to devDependencies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional changes to address PR feedback and tests failures:
- Updated notebook download menu functions because Puppeteer notebook-download test. menu clicking is not working 100% of time from PR changes.
- Reverted
target
back toes5
. - Edited
polyfills.ts
to resolve a new error.
* Resolving ERROR: ReferenceError: global is not defined. | ||
* | ||
*/ | ||
(window as any).global = window; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made more changes and fixed broken tests. CircleCI testing passed 2 of 2. Could you do a final review? Just to verify new changes made today are okay. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding the global variable and changing the code to use the global
to accommodate an upgrade in the testing lib "feels" a bit off... But I will accept the tradeoff to keep the lib up to date. Perhaps we can revisit those changes once Angular is removed.
@@ -7,7 +7,7 @@ import {Runtime} from 'generated/fetch'; | |||
import {RuntimeStatus} from 'generated/fetch'; | |||
import {RuntimeApi} from 'generated/fetch/api'; | |||
import SpyInstance = jest.SpyInstance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be combined with the import statement below it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I wasn't able to combine two imports.
However, I can do this so that we don't need import SpyInstance = jest.SpyInstance;
let mockGetRuntime: jest.SpyInstance;
let mockCreateRuntime: jest.SpyInstance;
let mockDeleteRuntime: jest.SpyInstance;
let mockStartRuntime: jest.SpyInstance;
Let me know if you like me to make this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to merge the PR now. If something is off, please let me know so I will make a follow up PR to address them.
@@ -7,7 +7,7 @@ import {Runtime} from 'generated/fetch'; | |||
import {RuntimeStatus} from 'generated/fetch'; | |||
import {RuntimeApi} from 'generated/fetch/api'; | |||
import SpyInstance = jest.SpyInstance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I wasn't able to combine two imports.
However, I can do this so that we don't need import SpyInstance = jest.SpyInstance;
let mockGetRuntime: jest.SpyInstance;
let mockCreateRuntime: jest.SpyInstance;
let mockDeleteRuntime: jest.SpyInstance;
let mockStartRuntime: jest.SpyInstance;
Let me know if you like me to make this change.
Hello, yesterday's nightly test fails with error:
Is that reverent to this PR? |
No. It failed here. the runtime status spinner were not excluded in |
Retry reverted PR commit 4287.