Skip to content

Commit

Permalink
chore: simplify jest config
Browse files Browse the repository at this point in the history
  • Loading branch information
fengtianze committed Jan 9, 2024
1 parent 893f548 commit 5158fd4
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"node": "16",
"node": "20",
"publishDirectory": {
"@alauda/ui": "release"
},
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
cache: yarn

- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
cache: yarn

- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
cache: yarn

- name: Install Dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/stories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
cache: yarn

- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
cache: yarn

- name: Install Dependencies
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
3 changes: 2 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Config } from '@jest/types';

const config: Config.InitialOptions = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
testMatch: ['<rootDir>/src/**/+(*.)+(spec|test).+(ts|js)?(x)'],
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
coverageReporters: ['text', 'html'],
coverageDirectory: '<rootDir>/coverage',
modulePathIgnorePatterns: [
Expand Down
64 changes: 18 additions & 46 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,24 @@
import 'jest-preset-angular/setup-jest';

import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';

getTestBed().resetTestEnvironment();
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{ teardown: { destroyAfterEach: false } },
);

Object.defineProperty(document, 'doctype', {
value: '<!DOCTYPE html>',
});

Object.defineProperty(window, 'CSS', { value: null });

Object.defineProperty(window, 'getComputedStyle', {
value() {
return {
display: 'none',
appearance: ['-webkit-appearance'],
};
// @ts-expect-error https://thymikee.github.io/jest-preset-angular/docs/getting-started/test-environment
globalThis.ngJest = {
testEnvironmentOptions: {
errorOnUnknownElements: true,
errorOnUnknownProperties: true,
},
});
};

// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Object.defineProperty(window, 'matchMedia', {
value() {
return {
matches: 'light',
addListener: () => undefined as void,
addEventListener: () => undefined as void,
};
},
});

/**
* ISSUE: https://github.com/angular/material2/issues/7101
* Workaround for JSDOM missing transform property
*/
Object.defineProperty(document.body.style, 'transform', {
value() {
return {
enumerable: true,
configurable: true,
};
},
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"JounQin <[email protected]> (https://www.1stG.me)"
],
"license": "MIT",
"packageManager": "[email protected]",
"keywords": [
"alauda",
"angular",
Expand Down Expand Up @@ -88,6 +89,7 @@
"jest": "^29.6.4",
"jest-junit": "^16.0.0",
"jest-preset-angular": "^13.1.1",
"jsdom": "^23.2.0",
"lint-staged": "^14.0.1",
"ng-packagr": "^16.2.2",
"npm-run-all": "^4.1.5",
Expand Down
Loading

0 comments on commit 5158fd4

Please sign in to comment.