Skip to content

Commit

Permalink
feat: jsdom-v13 (#229)
Browse files Browse the repository at this point in the history
This PR introduces jsdom v13 as default test environment for jest-preset-angular. This is achieved by
* Adding [jest-environment-jsdom-thirteen](https://github.com/theneva/jest-environment-jsdom-thirteen) as dependency
* Adding `testEnvironment: 'jest-environment-jsdom-thirteen' to `jest-preset.js`
* Removing Web Storage API mocks from `example/jestGlobalmocks.ts`
* Adding instructions in README Troubleshooting to configure usage of jsdom v11 (one line in jest configuration)

I added tests in a second commit to confirm it works, but I think they do not have to end up in the repository, as they basically test, what is already tested in jsdom itself.


Closes #216
  • Loading branch information
wtho authored and thymikee committed Feb 16, 2019
1 parent d65fdc9 commit 864440e
Show file tree
Hide file tree
Showing 8 changed files with 773 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* (**BREAKING**): Upgrade ts-jest to ^23.10.4 and use ast-transformer instead of processor ([#204](https://github.com/thymikee/jest-preset-angular/pull/204))
* Chore: Remove template literal character escaping (reverts [#34](https://github.com/thymikee/jest-preset-angular/pull/34))
* Feat: Use test environment JSDOM v13 by default ([#229](https://github.com/thymikee/jest-preset-angular/pull/229))


#### Migration Guide

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,16 @@ global.$ = global.jQuery = $;
The same declaration can be applied to other vendor libraries.

Reference: https://github.com/facebook/jest/issues/708

### Failing with Node v7 or lower

By default we use JSDOM v13, which requires Node v8+. If you want to use Node in a lower version to run your tests, make sure to configure jest to use an older JSDOM version, either in your `jest.config.js`, `jest.config.json` or `package.json`:

```json
"testEnvironment": "jsdom"
```

If you use JSDOM v11 or lower, you might have to mock `localStorage` or `sessionStorage` on your own or using some third-party library by loading it in `setupFilesAfterEnv`.

Reference: https://jestjs.io/docs/en/configuration.html#testenvironment-string, https://github.com/jsdom/jsdom/blob/master/Changelog.md#1200

1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@angular-devkit/build-angular": "^0.7.3",
"@angular/cli": "^6.1.3",
"@angular/compiler-cli": "6.1.2",
"@types/jest": "^23.3.14",
"@types/node": "^10.5.8",
"codelyzer": "^4.4.3",
"jest": "^23.5.0",
Expand Down
12 changes: 0 additions & 12 deletions example/src/jestGlobalMocks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
const createStorageMock = () => {
let storage = {};
return {
getItem: key => key in storage ? storage[key] : null,
setItem: (key, value) => storage[key] = value || '',
removeItem: key => delete storage[key],
clear: () => storage = {},
};
};

Object.defineProperty(window, 'CSS', {value: null});
Object.defineProperty(window, 'localStorage', {value: createStorageMock()});
Object.defineProperty(window, 'sessionStorage', {value: createStorageMock()});
Object.defineProperty(document, 'doctype', {
value: '<!DOCTYPE html>'
});
Expand Down
Loading

0 comments on commit 864440e

Please sign in to comment.