Skip to content

Commit

Permalink
feat: use jsdom v13 by default
Browse files Browse the repository at this point in the history
This enables us to use `localStorage` and `sessionStorage` without
mocking it manually.

Instructions to handle node < v6 were added to README.md.
  • Loading branch information
wtho committed Feb 16, 2019
1 parent d65fdc9 commit 9a4ce00
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 9a4ce00

Please sign in to comment.