Skip to content

Fix most tests that break with es2020 output. #5991

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

Merged
merged 1 commit into from
Nov 9, 2022

Conversation

bmd3k
Copy link
Contributor

@bmd3k bmd3k commented Oct 21, 2022

The migration to spec_bundle() will require we configure the TypeScript compiler to output to JavaScript 'es2020'. For the most part, this will require very little changes to our code. There is one glaring exception, though, that we mostly address in this PR.

The following pattern will break:

in my_utilities.ts:

export function myFuncToSpyOn() {
  // Some code.
}

in my_test.ts:

import * as my_utilities from './my_utilities'

spyOn(my_utilities, 'myFuncToSpyOn').and.returnValue(1);

The Jasmine library will complain:
Error: <spyOn> : myFuncToSpyOn is not declared writable or has no setter

Jasmine is aware of the issue but can't provide a fix:

Instead they suggest we refactor our code so that the functions being spied on are wrapped in some Class or other Object.

This PR does exactly that, for the most part doing the most simple of wrapping:

now in my_utilities.ts:

// No longer exported.
function myFuncToSpyOn() {
  // Some code.
}

export const MyUtilities = {
  myFuncToSpyOn
}

now in my_test.ts:

import {MyUtilities} from './my_utilities'

spyOn(MyUtilities, 'myFuncToSpyOn').and.returnValue(1);

The change has a bit of bigger blast radius because not only do the tests need to be changed but so does every other file (test or not) that are using the utility functions.

@bmd3k bmd3k requested a review from JamesHollyer October 21, 2022 14:36
@@ -15,7 +15,7 @@ limitations under the License.

import {Extent, Polyline, Rect} from './internal_types';

export function convertRectToExtent(rect: Rect): Extent {
function convertRectToExtent(rect: Rect): Extent {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we continue to export these so that we can import them directly in places other than the tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If code uses the functions directly then they won't be using the mocks.

For example:

If the code you're writing does this:

function myFunction() {
  return convertRectToExtent(this.someRect);
}

But your test does this:

spyOn(ChartUtils, 'convertRectToExtent').and.returnValue(someExtent);
expect(myFunction()).toEqual(someExtent);

It won't work. Because you're replacing what ChartUtils.convertRectToExtent points to with a mock/spy but raw convertRectToExtent remains unmodified.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yea that makes sense. Dang ok I guess we have to do it this way.

@bmd3k bmd3k merged commit f340a5d into tensorflow:master Nov 9, 2022
@@ -17,7 +17,7 @@ limitations under the License.
*/
import {Component, Input, NO_ERRORS_SCHEMA} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import * as loadMonacoShim from './load_monaco_shim';
import {MonacoShim} from './load_monaco_shim';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the sync. Would you mind working on the fix?
See cl/487331149. Thanks

qihach64 pushed a commit to qihach64/tensorboard that referenced this pull request Dec 19, 2022
The migration to spec_bundle() will require we configure the TypeScript
compiler to output to JavaScript 'es2020'. There is one pattern of code
that we often use that will break:

in my_utilities.ts:
```
export function myFuncToSpyOn() {
  // Some code.
}
```

in my_test.ts:
```
import * as my_utilities from './my_utilities'

spyOn(my_utilities, 'myFuncToSpyOn').and.returnValue(1);
```

The Jasmine library will complain: 
`Error: <spyOn> : myFuncToSpyOn is not declared writable or has no
setter`

Jasmine is aware of the issue but can't provide a fix:
* jasmine/jasmine#1942
* https://jasmine.github.io/pages/faq.html#module-spy

Instead they suggest we refactor our code so that the functions being
spied on are wrapped in some Class or other Object.

This PR does exactly that, for the most part doing the most simple of
wrapping:

now in my_utilities.ts:
```
// No longer exported.
function myFuncToSpyOn() {
  // Some code.
}

export const MyUtilities = {
  myFuncToSpyOn
}
```

now in my_test.ts:
```
import {MyUtilities} from './my_utilities'

spyOn(MyUtilities, 'myFuncToSpyOn').and.returnValue(1);
```

The change has a bit of bigger blast radius because not only do the
tests need to be changed but so does every other file (test or not) that
are using the utility functions.
yatbear pushed a commit to yatbear/tensorboard that referenced this pull request Mar 27, 2023
The migration to spec_bundle() will require we configure the TypeScript
compiler to output to JavaScript 'es2020'. There is one pattern of code
that we often use that will break:

in my_utilities.ts:
```
export function myFuncToSpyOn() {
  // Some code.
}
```

in my_test.ts:
```
import * as my_utilities from './my_utilities'

spyOn(my_utilities, 'myFuncToSpyOn').and.returnValue(1);
```

The Jasmine library will complain: 
`Error: <spyOn> : myFuncToSpyOn is not declared writable or has no
setter`

Jasmine is aware of the issue but can't provide a fix:
* jasmine/jasmine#1942
* https://jasmine.github.io/pages/faq.html#module-spy

Instead they suggest we refactor our code so that the functions being
spied on are wrapped in some Class or other Object.

This PR does exactly that, for the most part doing the most simple of
wrapping:

now in my_utilities.ts:
```
// No longer exported.
function myFuncToSpyOn() {
  // Some code.
}

export const MyUtilities = {
  myFuncToSpyOn
}
```

now in my_test.ts:
```
import {MyUtilities} from './my_utilities'

spyOn(MyUtilities, 'myFuncToSpyOn').and.returnValue(1);
```

The change has a bit of bigger blast radius because not only do the
tests need to be changed but so does every other file (test or not) that
are using the utility functions.
dna2github pushed a commit to dna2fork/tensorboard that referenced this pull request May 1, 2023
The migration to spec_bundle() will require we configure the TypeScript
compiler to output to JavaScript 'es2020'. There is one pattern of code
that we often use that will break:

in my_utilities.ts:
```
export function myFuncToSpyOn() {
  // Some code.
}
```

in my_test.ts:
```
import * as my_utilities from './my_utilities'

spyOn(my_utilities, 'myFuncToSpyOn').and.returnValue(1);
```

The Jasmine library will complain: 
`Error: <spyOn> : myFuncToSpyOn is not declared writable or has no
setter`

Jasmine is aware of the issue but can't provide a fix:
* jasmine/jasmine#1942
* https://jasmine.github.io/pages/faq.html#module-spy

Instead they suggest we refactor our code so that the functions being
spied on are wrapped in some Class or other Object.

This PR does exactly that, for the most part doing the most simple of
wrapping:

now in my_utilities.ts:
```
// No longer exported.
function myFuncToSpyOn() {
  // Some code.
}

export const MyUtilities = {
  myFuncToSpyOn
}
```

now in my_test.ts:
```
import {MyUtilities} from './my_utilities'

spyOn(MyUtilities, 'myFuncToSpyOn').and.returnValue(1);
```

The change has a bit of bigger blast radius because not only do the
tests need to be changed but so does every other file (test or not) that
are using the utility functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants