Skip to content

Commit

Permalink
feat: support core-js >=2.0.0 < 4.0.0 (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
oktav777 authored and thymikee committed Apr 9, 2019
1 parent 3b88d0c commit 239a647
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 28 deletions.
44 changes: 44 additions & 0 deletions __tests__/CoreJS-es6-reflect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
describe('importing ES6 reflect', () => {

let setMocks = () => {
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', () => true, { virtual: true });
jest.mock('zone.js/dist/proxy.js', () => true, { virtual: true });
jest.mock('zone.js/dist/sync-test', () => true, { virtual: true });
jest.mock('zone.js/dist/async-test', () => true, { virtual: true });
jest.mock('zone.js/dist/fake-async-test', () => true, { virtual: true });
jest.mock('../zone-patch', () => true, { virtual: true });

jest.mock('core-js/es7/reflect', () => true, { virtual: true });
jest.mock('core-js/proposals/reflect-metadata', () => true, { virtual: true });
}

let coreJs2Error = new Error('core-js@3 es6 reflect failed');
let coreJs3Error = new Error('core-js@2 es6 reflect failed');

beforeEach(() => setMocks());
afterEach(() => jest.resetAllMocks());

it('should throw if core-js es6-reflect is not installed', () => {
jest.mock('core-js/es6/reflect', () => { throw coreJs2Error }, { virtual: true });
jest.mock('core-js/es/reflect', () => { throw coreJs3Error }, { virtual: true });

expect(() => require('../setupJest')).toThrow('core-js es6-reflect not found!');
});

it('should import from core-js@2', () => {
jest.mock('core-js/es6/reflect', () => true, { virtual: true });
jest.mock('core-js/es/reflect', () => { throw coreJs3Error }, { virtual: true });

expect(() => require('../setupJest')).not.toThrow();
});

it('should import from core-js@3', () => {
jest.mock('core-js/es6/reflect', () => { throw coreJs2Error }, { virtual: true });
jest.mock('core-js/es/reflect', () => true, { virtual: true });

expect(() => require('../setupJest')).not.toThrow();
});

});
44 changes: 44 additions & 0 deletions __tests__/CoreJS-es7-reflect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
describe('importing ES7 reflect', () => {

let setMocks = () => {
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', () => true, { virtual: true });
jest.mock('zone.js/dist/proxy.js', () => true, { virtual: true });
jest.mock('zone.js/dist/sync-test', () => true, { virtual: true });
jest.mock('zone.js/dist/async-test', () => true, { virtual: true });
jest.mock('zone.js/dist/fake-async-test', () => true, { virtual: true });
jest.mock('../zone-patch', () => true, { virtual: true });

jest.mock('core-js/es6/reflect', () => true, { virtual: true });
jest.mock('core-js/es/reflect', () => true, { virtual: true });
}

let coreJs2Error = new Error('core-js@2 es7 reflect failed');
let coreJs3Error = new Error('core-js@3 es7 reflect failed');

beforeEach(() => setMocks());
afterEach(() => jest.resetAllMocks());

it('should throw if core-js es7-reflect is not installed', () => {
jest.mock('core-js/es7/reflect', () => { throw coreJs2Error }, { virtual: true });
jest.mock('core-js/proposals/reflect-metadata', () => { throw coreJs3Error }, { virtual: true });

expect(() => require('../setupJest')).toThrow('core-js es7-reflect not found!');
});

it('should import from core-js@2', () => {
jest.mock('core-js/es7/reflect', () => true, { virtual: true });
jest.mock('core-js/proposals/reflect-metadata', () => { throw coreJs3Error }, { virtual: true });

expect(() => require('../setupJest')).not.toThrow();
});

it('should import from core-js@3', () => {
jest.mock('core-js/es7/reflect', () => { throw coreJs2Error }, { virtual: true });
jest.mock('core-js/proposals/reflect-metadata', () => true, { virtual: true });

expect(() => require('../setupJest')).not.toThrow();
});

});
2 changes: 1 addition & 1 deletion __tests__/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"target": "es5",
"baseUrl": "",
"allowJs": true,
"types": ["jest"]
"types": ["jest", "node"]
}
}
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@angular/platform-browser": "7.2.8",
"@angular/platform-browser-dynamic": "7.2.8",
"@angular/router": "7.2.8",
"core-js": "^2.5.7",
"core-js": "^2.0.0",
"rxjs": "6.4.0",
"zone.js": "0.8.29"
},
Expand Down
4 changes: 2 additions & 2 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ copy-webpack-plugin@4.6.0:
p-limit "^1.0.0"
serialize-javascript "^1.4.0"

core-js@^2.4.0, core-js@^2.5.7:
core-js@^2.0.0, core-js@^2.4.0:
version "2.6.5"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.5.tgz#44bc8d249e7fb2ff5d00e0341a7ffb94fbf67895"
integrity sha512-klh/kDpwX8hryYL14M9w/xei6vrv6sE8gTHDG7/T/+SEovB/G4ejwcfE/CBzO6Edsu+OETZMZ3wcX/EjUkrl5A==
Expand Down Expand Up @@ -3950,7 +3950,7 @@ jest-mock@^24.0.0:
integrity sha512-sQp0Hu5fcf5NZEh1U9eIW2qD0BwJZjb63Yqd98PQJFvf/zzUTBoUAwv/Dc/HFeNHIw1f3hl/48vNn+j3STaI7A==

"jest-preset-angular@file:..":
version "7.0.0"
version "7.0.1"
dependencies:
jest-environment-jsdom-thirteen "^1.0.0"
ts-jest "^24.0.0"
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
"ts-jest": "^24.0.0"
},
"devDependencies": {
"@types/jest": "^24.0.0",
"@types/node": "^11.0.0",
"@types/jest": "^24.0.11",
"@types/node": "^11.11.5",
"jest": "^24.0.0",
"typescript": "^3.2.1"
},
"peerDependencies": {
"@angular/core": ">=2.0.0",
"@angular/platform-browser-dynamic": ">=2.0.0",
"core-js": ">=2.0.0 < 4.0.0",
"jest": "^24.1.0"
},
"jest": {
Expand Down
46 changes: 32 additions & 14 deletions setupJest.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
'use strict';
'use strict';

require('core-js/es6/reflect');
require('core-js/es7/reflect');
require('zone.js/dist/zone.js');
require('zone.js/dist/proxy.js');
require('zone.js/dist/sync-test');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
try {
require('core-js/es6/reflect');
} catch (e) {
try {
require('core-js/es/reflect');
} catch(e) {
throw new Error('core-js es6-reflect not found!');
}
}

try {
require('core-js/es7/reflect');
} catch (e) {
try {
require('core-js/proposals/reflect-metadata');
} catch (e) {
throw new Error('core-js es7-reflect not found!');
}
}

require('zone.js/dist/zone.js');
require('zone.js/dist/proxy.js');
require('zone.js/dist/sync-test');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
require('./zone-patch');

const getTestBed = require('@angular/core/testing').getTestBed;
const BrowserDynamicTestingModule = require('@angular/platform-browser-dynamic/testing').BrowserDynamicTestingModule;
const platformBrowserDynamicTesting = require('@angular/platform-browser-dynamic/testing').platformBrowserDynamicTesting;
const getTestBed = require('@angular/core/testing').getTestBed;
const BrowserDynamicTestingModule = require('@angular/platform-browser-dynamic/testing').BrowserDynamicTestingModule;
const platformBrowserDynamicTesting = require('@angular/platform-browser-dynamic/testing').platformBrowserDynamicTesting;

getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,17 @@
resolved "https://registry.yarnpkg.com/@types/jest-diff/-/jest-diff-20.0.1.tgz#35cc15b9c4f30a18ef21852e255fdb02f6d59b89"
integrity sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==

"@types/jest@^24.0.0":
version "24.0.9"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.0.9.tgz#74ce9cf337f25e189aa18f76ab3d65e8669b55f2"
integrity sha512-k3OOeevcBYLR5pdsOv5g3OP94h3mrJmLPHFEPWgbbVy2tGv0TZ/TlygiC848ogXhK8NL0I5up7YYtwpCp8xCJA==
"@types/jest@^24.0.11":
version "24.0.11"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.0.11.tgz#1f099bea332c228ea6505a88159bfa86a5858340"
integrity sha512-2kLuPC5FDnWIDvaJBzsGTBQaBbnDweznicvK7UGYzlIJP4RJR2a4A/ByLUXEyEgag6jz8eHdlWExGDtH3EYUXQ==
dependencies:
"@types/jest-diff" "*"

"@types/node@^11.0.0":
version "11.10.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.10.4.tgz#3f5fc4f0f322805f009e00ab35a2ff3d6b778e42"
integrity sha512-wa09itaLE8L705aXd8F80jnFpxz3Y1/KRHfKsYL2bPc0XF+wEWu8sR9n5bmeu8Ba1N9z2GRNzm/YdHcghLkLKg==
"@types/node@^11.11.5":
version "11.11.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.11.5.tgz#0c57e12eb44d44e5b6735593925286553ee7cebf"
integrity sha512-pz6wNe/XwyesgfVX7P6B0hY3TnTAYXk6KSTLdpQfbuq3be+hnMoCuFzE+yLTskPdBwmNiGRL2TAsnF09aRugvQ==

abab@^2.0.0:
version "2.0.0"
Expand Down

0 comments on commit 239a647

Please sign in to comment.