Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into release/7
Browse files Browse the repository at this point in the history
* origin/master:
  Upgraded ts-jest version. (#219)
  docs: add astTransformers in configuration example (#218)
  feat: Import jest-zone-patch (#214)
  chore: Migrate CI config to CircleCI v2 (#212)
  Transform templateUrl, styleUrls and styles everywhere (#211)
  feat: adjust semver range of jest-zone-patch (#209)
  Adjust CHANGELOG (#208)
  • Loading branch information
thymikee committed Jan 26, 2019
2 parents 61cfeb3 + 801cb62 commit c538a9a
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 33 deletions.
77 changes: 77 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
version: 2.1
executors:
node-10:
docker:
- image: circleci/node:10

jobs:
install-dependencies:
executor: node-10
steps:
- checkout
- restore_cache:
keys:
# if lock file changes, we still use increasingly general patterns to restore cache
- yarn-cache-lib-{{ .Branch }}-{{ checksum "yarn.lock" }}
- yarn-cache-lib-{{ .Branch }}-
- run:
name: Install dependencies
command: |
yarn --version
yarn install --frozen-lockfile
- save_cache:
key: yarn-cache-lib-{{ .Branch }}-{{ checksum "yarn.lock" }}
paths:
- ~/.yarn
- ~/.cache/yarn
- ./node_modules
- persist_to_workspace:
root: .
paths: .

test-library:
executor: node-10
steps:
- attach_workspace:
at: .
- run:
name: Test Library
command: yarn test:ci

install-test-example:
executor: node-10
steps:
- attach_workspace:
at: .
- restore_cache:
keys:
- yarn-cache-example-{{ .Branch }}-{{ checksum "yarn.lock" }}
- yarn-cache-example-{{ .Branch }}-
- run:
name: Install Example Dependencies
command: |
cd example
yarn install --frozen-lockfile
- save_cache:
key: yarn-cache-example-{{ .Branch }}-{{ checksum "yarn.lock" }}
paths:
- ./example/node_modules
- run:
name: Test Example
command: |
cd example
yarn test:ci
yarn test:coverage
workflows:
version: 2
install-and-test:
jobs:
- install-dependencies
- test-library:
requires:
- install-dependencies
- install-test-example:
requires:
- install-dependencies

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ import './jestGlobalMocks'; // browser mocks globally available for every test
"globals": {
"ts-jest": {
"tsConfig": "<rootDir>/src/tsconfig.spec.json",
"stringifyContentPathRegex": "\\.html$"
"stringifyContentPathRegex": "\\.html$",
"astTransformers": [
"jest-preset-angular/InlineHtmlStripStylesTransformer"
]
}
},
"transform": {
Expand Down
23 changes: 0 additions & 23 deletions circle.yml

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"license": "MIT",
"dependencies": {
"@types/jest": "^23.3.10",
"jest-zone-patch": ">=0.0.9 <1.0.0",
"ts-jest": "~23.10.0"
"ts-jest": "~23.10.5"
},
"devDependencies": {
"@types/node": "^10.12.12",
Expand Down
2 changes: 1 addition & 1 deletion setupJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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('jest-zone-patch');
require('./zone-patch');

const getTestBed = require('@angular/core/testing').getTestBed;
const BrowserDynamicTestingModule = require('@angular/platform-browser-dynamic/testing').BrowserDynamicTestingModule;
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2188,11 +2188,6 @@ jest-worker@^23.2.0:
dependencies:
merge-stream "^1.0.1"

"jest-zone-patch@>=0.0.9 <1.0.0":
version "0.0.9"
resolved "https://registry.yarnpkg.com/jest-zone-patch/-/jest-zone-patch-0.0.9.tgz#ee6823317798a3ae967e82be963f0b1f80b03d3f"
integrity sha512-QmlUxg2NIvLHSqexS3nqQ+Kid6o+5cxYnlcTi204XwsRcBm+mVYodpe+9ddcooKR9ATBIUl4zY247Uky/PgXhw==

jest@^23.6.0:
version "23.6.0"
resolved "https://registry.yarnpkg.com/jest/-/jest-23.6.0.tgz#ad5835e923ebf6e19e7a1d7529a432edfee7813d"
Expand Down Expand Up @@ -3688,7 +3683,7 @@ trim-right@^1.0.1:
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"
integrity sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=

ts-jest@~23.10.0:
ts-jest@~23.10.5:
version "23.10.5"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-23.10.5.tgz#cdb550df4466a30489bf70ba867615799f388dd5"
integrity sha512-MRCs9qnGoyKgFc8adDEntAOP64fWK1vZKnOYU1o2HxaqjdJvGqmkLCPCnVq1/If4zkUmEjKPnCiUisTrlX2p2A==
Expand Down
29 changes: 29 additions & 0 deletions zone-patch/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2017, Michał Pierzchała
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6 changes: 6 additions & 0 deletions zone-patch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# zone-patch
Enables Jest functions to be run within Zone.js context, specifically for [Angular](https://angular.io) apps.

It's crucial to run this patch here, because at this point patched functions like `test` or `describe` are available in global scope.

`zone-patch` has been included in `setupJest.js` by default.
106 changes: 106 additions & 0 deletions zone-patch/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Patch Jest's describe/test/beforeEach/afterEach functions so test code
* always runs in a testZone (ProxyZone).
*/

if (Zone === undefined) {
throw new Error('Missing: Zone (zone.js)');
}
if (jest === undefined) {
throw new Error(
'Missing: jest.\n' +
'This patch must be included in a script called with ' +
'`setupTestFrameworkScriptFile` in Jest config.'
);
}
if (jest['__zone_patch__'] === true) {
throw new Error("'jest' has already been patched with 'Zone'.");
}

jest['__zone_patch__'] = true;
const SyncTestZoneSpec = Zone['SyncTestZoneSpec'];
const ProxyZoneSpec = Zone['ProxyZoneSpec'];

if (SyncTestZoneSpec === undefined) {
throw new Error('Missing: SyncTestZoneSpec (zone.js/dist/sync-test)');
}
if (ProxyZoneSpec === undefined) {
throw new Error('Missing: ProxyZoneSpec (zone.js/dist/proxy.js)');
}

const env = global;
const ambientZone = Zone.current;

// Create a synchronous-only zone in which to run `describe` blocks in order to
// raise an error if any asynchronous operations are attempted
// inside of a `describe` but outside of a `beforeEach` or `it`.
const syncZone = ambientZone.fork(new SyncTestZoneSpec('jest.describe'));
function wrapDescribeInZone(describeBody) {
return function () { return syncZone.run(describeBody, null, arguments); }
}

// Create a proxy zone in which to run `test` blocks so that the tests function
// can retroactively install different zones.
const testProxyZone = ambientZone.fork(new ProxyZoneSpec());
function wrapTestInZone(testBody) {
if (testBody === undefined) {
return;
}
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
}

const bindDescribe = (originalJestFn) => function () {
const eachArguments = arguments;
return function (description, specDefinitions, timeout) {
arguments[1] = wrapDescribeInZone(specDefinitions)
return originalJestFn.apply(this, eachArguments).apply(
this,
arguments
)
}
};

['xdescribe', 'fdescribe', 'describe'].forEach(methodName => {
const originaljestFn = env[methodName];
env[methodName] = function(description, specDefinitions, timeout) {
arguments[1] = wrapDescribeInZone(specDefinitions)
return originaljestFn.apply(
this,
arguments
);
};
env[methodName].each = bindDescribe(originaljestFn.each);
if (methodName === 'describe') {
env[methodName].only = env['fdescribe'];
env[methodName].skip = env['xdescribe'];
env[methodName].only.each = bindDescribe(originaljestFn.only.each);
env[methodName].skip.each = bindDescribe(originaljestFn.skip.each);
}
});

['xit', 'fit', 'xtest', 'test', 'it'].forEach(methodName => {
const originaljestFn = env[methodName];
env[methodName] = function(description, specDefinitions, timeout) {
arguments[1] = wrapTestInZone(specDefinitions);
return originaljestFn.apply(this, arguments);
};
// The revised method will be populated to the final each method, so we only declare the method that in the new globals
env[methodName].each = originaljestFn.each;
if (methodName === 'test' || methodName === 'it') {
env[methodName].only = env['fit'];
env[methodName].only.each = originaljestFn.only.each;

env[methodName].skip = env['xit'];
env[methodName].skip.each = originaljestFn.skip.each;
}
});

['beforeEach', 'afterEach', 'beforeAll', 'afterAll'].forEach(methodName => {
const originaljestFn = env[methodName];
env[methodName] = function(specDefinitions, timeout) {
arguments[0] = wrapTestInZone(specDefinitions);
return originaljestFn.apply(this, arguments);
};
});

0 comments on commit c538a9a

Please sign in to comment.