-
Notifications
You must be signed in to change notification settings - Fork 310
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
fix(package): update core-js to version 3.0.0 #246
Conversation
package.json
Outdated
@@ -19,6 +19,7 @@ | |||
"peerDependencies": { | |||
"@angular/core": ">=2.0.0", | |||
"@angular/platform-browser-dynamic": ">=2.0.0", | |||
"core-js": "^3.0.0", |
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 would be a breaking change, while this preset works with core-js@2
. Can we make it "core-js": ">=2.0.0 < 4.0.0",
for now and adjust the setupJest
to try imports from core-js
v2 and v3?
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 am thinking to try/catch
the require()
functions. Will that be a good idea?
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.
If we want to support both, this is the only solution I can see for now :D
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 got a small issue :)
How am I supposed to test the changes? I can't make tests fail.
I deleted ./setupJest.js
, ran npm run prepare
, and then both npm run test
are green in ./
and ./example
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.
We'd need to have 2 projects, which is slow to test unfortunately. What you can do is to import ./setupJest.js
from a new test file and mock require
there to behave appropriately for v2 and v3
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 think a require
-mock is appropriate. We do not have to test node being able to resolve and load an installed package, we just have to test if the errors are handled. So mocking require and throwing the corresponding error as node would do should suffice in my opinion.
If no core-js
installation can be found, an appropriate error should still be thrown.
Maybe in your catch you can put sth to stdout to inform user about the version of core-js and in your test maybe you can capture that stdout ? |
Well, what looked from the first sight "a hour of work" transformed in days. I don't know why, but I can't make the tests work when I add conditional What I tried
try {
require('core-js/es6/reflect');
require('core-js/es/reflect');
require('core-js/es7/reflect');
require('core-js/proposals/reflect-metadata');
} catch (e) { }
try {
require('core-js/es6/reflect');
} catch (e) {
require('core-js/es/reflect');
}
try {
require('core-js/es7/reflect');
} catch (e) {
require('core-js/proposals/reflect-metadata');
}
var fs = require('fs');
const coreJS_2_es6reflect = 'core-js/es6/reflect';
const coreJS_3_es6reflect = 'core-js/es/reflect';
const coreJS_2_es7reflect = 'core-js/es7/reflect';
const coreJS_3_es7reflect = 'core-js/proposals/reflect-metadata';
switch(true) {
case fs.existsSync(coreJS_2_es6reflect):
require(coreJS_2_es6reflect);
break;
case fs.existsSync(coreJS_3_es6reflect):
require(coreJS_3_es6reflect);
break;
default:
console.log('ES6 `core-js/reflect` not found.');
break;
}
switch(true) {
case fs.existsSync(coreJS_2_es7reflect):
require(coreJS_2_es7reflect);
break;
case fs.existsSync(coreJS_3_es7reflect):
require(coreJS_3_es7reflect)
break;
default:
console.log('ES7 `core-js/reflect` not found.');
break;
}
var tryRequire = require('try-require');
if (tryRequire.resolve('core-js/es6/reflect')) {
tryRequire('core-js/es6/reflect')
} else {
tryRequire('core-js/es/reflect')
}
if (tryRequire.resolve('core-js/es7/reflect')) {
tryRequire('core-js/es7/reflect')
} else {
tryRequire('core-js/proposals/reflect-metadata')
} None of the methods worked for me. I even started to doubt my coding skills... I think maybe I missed something, and I would like to ask somebody to apply the changes I made to see if they will have the same result or not. Also read the first paragraph from Given that |
I tried the second approach (try/catch) and it worked for me. Note that I run |
I also realized, that I usually use yarn when working on |
I still can't handle the unit testing. Using let requireMock = require('mock-require');
it('should import from core-js@2', () => {
const path = 'core-js/es6/reflect';
requireMock(path, () => {
console.log(`importing ${path}`);
return true;
});
const module = require(path);
expect(module).toBeDefined();
}); And I get |
Yeah it's actually tricky. I tried using This means, we have to install This is a limit, as we
cc @thymikee do you think we should skip test this for now? One idea I can come up with right now is to just add a smoke test in Edit: |
You can use |
Do the module has to exist in
|
Nothing needs to exist in |
Yeah, I did not use It works now with this setup: describe('core-js loading', () => {
beforeEach(() => {
jest.resetAllMocks()
jest.mock('@angular/core/testing', () => ({ getTestBed: () => ({initTestEnvironment() {}})}), {virtual: true})
jest.mock('@angular/platform-browser-dynamic/testing', () => ({ BrowserDynamicTestingModule: null, platformBrowserDynamicTesting: () => {}}), {virtual: true})
jest.mock('zone.js/dist/zone.js', () => {}, {virtual: true});
jest.mock('zone.js/dist/proxy.js', () => {}, {virtual: true});
jest.mock('zone.js/dist/sync-test', () => {}, {virtual: true});
jest.mock('zone.js/dist/async-test', () => {}, {virtual: true});
jest.mock('zone.js/dist/fake-async-test', () => {}, {virtual: true});
jest.mock('../zone-patch', () => {}, {virtual: true});
})
it('should fail if no core-js is installed', () => {
jest.mock('core-js/es6/reflect', () => {throw new Error('no v2 es6 reflect')}, {virtual: true});
jest.mock('core-js/es7/reflect', () => {throw new Error('no v2 es6 reflect')}, {virtual: true});
jest.mock('core-js/es/reflect', () => {throw new Error('no v3 es reflect')}, {virtual: true});
jest.mock('core-js/proposal/reflect-metadata', () => {throw new Error('no v3 proposal reflect-metadata')}, {virtual: true});
expect(() => require('../setupJest')).toThrow()
})
it('should pass if core-js v2 is installed', () => {
jest.mock('core-js/es6/reflect', () => {}, {virtual: true});
jest.mock('core-js/es7/reflect', () => {}, {virtual: true});
jest.mock('core-js/es/reflect', () => {throw new Error('no v3 es reflect')}, {virtual: true});
jest.mock('core-js/proposal/reflect-metadata', () => {throw new Error('no v3 proposal reflect-metadata')}, {virtual: true});
expect(() => require('../setupJest')).toThrow()
})
}) My let coreJsLoadded = false
try {
require('core-js/es6/reflect');
require('core-js/es7/reflect');
coreJsLoadded = true
} catch (_) {}
try {
require('core-js/es/reflect');
require('core-js/proposals/reflect-metadata');
coreJsLoadded = true
} catch (_) {}
if (!coreJsLoadded) {
throw new Error('Could not load module core-js, make sure it is installed')
}
require('...');
... |
I'm kinda new to Jest too 😄 |
Does this not help you? |
@thymikee, I added the tests and squashed + rebased the PR. |
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.
LGTM, @wtho mind having a final look?
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.
Just this one naming might be chosen better, otherwise it looks awesome!
__tests__/CoreJS-es6-reflect.test.ts
Outdated
@@ -0,0 +1,44 @@ | |||
describe('importing ES6 reflect', () => { | |||
|
|||
let resetMocks = () => { |
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.
better call this setMocks
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.
will update tomorrow
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.
Done
Thank you so much @oktav777! |
Released in 7.1.0 |
Closes #244
Closes #243