diff --git a/__tests__/CoreJS-es6-reflect.test.ts b/__tests__/CoreJS-es6-reflect.test.ts new file mode 100644 index 0000000000..2cf22a9b1f --- /dev/null +++ b/__tests__/CoreJS-es6-reflect.test.ts @@ -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(); + }); + +}); diff --git a/__tests__/CoreJS-es7-reflect.test.ts b/__tests__/CoreJS-es7-reflect.test.ts new file mode 100644 index 0000000000..db24cfdc74 --- /dev/null +++ b/__tests__/CoreJS-es7-reflect.test.ts @@ -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(); + }); + +}); diff --git a/__tests__/tsconfig.spec.json b/__tests__/tsconfig.spec.json index bd6e0cd2ea..e04d24f5df 100644 --- a/__tests__/tsconfig.spec.json +++ b/__tests__/tsconfig.spec.json @@ -5,6 +5,6 @@ "target": "es5", "baseUrl": "", "allowJs": true, - "types": ["jest"] + "types": ["jest", "node"] } } diff --git a/example/package.json b/example/package.json index 65d3c72bb9..10e8e6ddef 100644 --- a/example/package.json +++ b/example/package.json @@ -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" }, diff --git a/example/yarn.lock b/example/yarn.lock index 4a2b719b7f..20cbaf24ed 100644 --- a/example/yarn.lock +++ b/example/yarn.lock @@ -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== @@ -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" diff --git a/package.json b/package.json index c7c8b94d1f..6352892ccb 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/setupJest.js b/setupJest.js index f71d5194e7..21015390a4 100644 --- a/setupJest.js +++ b/setupJest.js @@ -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() ); diff --git a/yarn.lock b/yarn.lock index 69f9ccbf2d..7b2e39e923 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"