Skip to content

Commit 6045a96

Browse files
authored
feat: add custom jsdom env (#2904)
This environment is a copy of `jest-environment-jsdom` which allows end users to decide which version of `jsdom` they would like to use in their project. See more in documentation site at https://thymikee.github.io/jest-preset-angular/docs/guides/jsdom-version Closes #2883
1 parent 9012e71 commit 6045a96

19 files changed

+592
-33
lines changed

.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ commitlint.config.js
3636
.prettierrc
3737

3838
# Internal jest config
39-
jest.config.js
4039
jest*.config.ts
4140

4241
# Tsconfig
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { FooComponent } from '../foo.component';
4+
5+
describe('FooComponent', () => {
6+
it('should trigger change detection without fixture.detectChanges', () => {
7+
TestBed.configureTestingModule({
8+
imports: [FooComponent],
9+
});
10+
const fixture = TestBed.createComponent(FooComponent);
11+
12+
expect(fixture.componentInstance.value1()).toBe('val1');
13+
14+
fixture.componentRef.setInput('value1', 'hello');
15+
16+
expect(fixture.componentInstance.value1()).toBe('hello');
17+
});
18+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!-- SOMETHING -->
2+
<p>Line 1</p>
3+
<div>
4+
<div *ngIf="condition1">
5+
{{ value1() }}
6+
</div>
7+
<span *ngIf="condition2">
8+
{{ value2() }}
9+
</span>
10+
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
p {
2+
font-size: 1.6rem;
3+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { NgIf } from '@angular/common';
2+
import { Component, input } from '@angular/core';
3+
4+
@Component({
5+
selector: 'foo',
6+
standalone: true,
7+
templateUrl: './foo.component.html',
8+
styleUrls: ['./foo.component.scss'],
9+
// we have to setup styles this way, since simple styles/styleUrs properties will be removed (jest does not unit test styles)
10+
styles: [
11+
`
12+
p {
13+
color: red;
14+
}
15+
`,
16+
],
17+
imports: [NgIf],
18+
})
19+
export class FooComponent {
20+
readonly value1 = input('val1');
21+
readonly value2 = input('val2');
22+
23+
protected readonly condition1 = true;
24+
protected readonly condition2 = false;
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { JestConfigWithTsJest } from 'ts-jest';
2+
3+
const config: JestConfigWithTsJest = {
4+
displayName: 'e2e-custom-jsdom-env',
5+
testEnvironment: '<rootDir>/../../environments/jest-jsdom-env.js',
6+
setupFilesAfterEnv: ['<rootDir>/../setup-test-env.ts'],
7+
transform: {
8+
'^.+\\.(ts|mjs|js|html)$': [
9+
'<rootDir>/../../build/index.js',
10+
{
11+
tsconfig: '<rootDir>/tsconfig-cjs.spec.json',
12+
stringifyContentPathRegex: '\\.(html|svg)$',
13+
},
14+
],
15+
},
16+
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
17+
};
18+
19+
export default config;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { JestConfigWithTsJest } from 'ts-jest';
2+
3+
const config: JestConfigWithTsJest = {
4+
displayName: 'e2e-custom-jsdom-env',
5+
testEnvironment: '<rootDir>/../../environments/jest-jsdom-env.js',
6+
setupFilesAfterEnv: ['<rootDir>/../setup-test-env.mts'],
7+
moduleNameMapper: {
8+
rxjs: '<rootDir>/../../node_modules/rxjs/dist/bundles/rxjs.umd.js',
9+
},
10+
extensionsToTreatAsEsm: ['.ts', '.mts'],
11+
transform: {
12+
'^.+\\.(ts|mts|mjs|js|html)$': [
13+
'<rootDir>/../../build/index.js',
14+
{
15+
useESM: true,
16+
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
17+
stringifyContentPathRegex: '\\.(html|svg)$',
18+
},
19+
],
20+
},
21+
};
22+
23+
export default config;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { JestConfigWithTsJest } from 'ts-jest';
2+
3+
const config: JestConfigWithTsJest = {
4+
displayName: 'e2e-custom-jsdom-env',
5+
testEnvironment: '<rootDir>/../../environments/jest-jsdom-env.js',
6+
setupFilesAfterEnv: ['<rootDir>/../setup-test-env.ts'],
7+
transform: {
8+
'^.+\\.(ts|mjs|js|html)$': [
9+
'<rootDir>/../../build/index.js',
10+
{
11+
tsconfig: '<rootDir>/tsconfig-cjs.spec.json',
12+
stringifyContentPathRegex: '\\.(html|svg)$',
13+
isolatedModules: true,
14+
},
15+
],
16+
},
17+
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
18+
};
19+
20+
export default config;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { JestConfigWithTsJest } from 'ts-jest';
2+
3+
const config: JestConfigWithTsJest = {
4+
displayName: 'e2e-custom-jsdom-env',
5+
testEnvironment: '<rootDir>/../../environments/jest-jsdom-env.js',
6+
setupFilesAfterEnv: ['<rootDir>/../setup-test-env.mts'],
7+
moduleNameMapper: {
8+
rxjs: '<rootDir>/../../node_modules/rxjs/dist/bundles/rxjs.umd.js',
9+
},
10+
extensionsToTreatAsEsm: ['.ts', '.mts'],
11+
transform: {
12+
'^.+\\.(ts|mts|mjs|js|html)$': [
13+
'<rootDir>/../../build/index.js',
14+
{
15+
useESM: true,
16+
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
17+
stringifyContentPathRegex: '\\.(html|svg)$',
18+
isolatedModules: true,
19+
},
20+
],
21+
},
22+
};
23+
24+
export default config;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../tsconfig-base.spec.json"
3+
}

0 commit comments

Comments
 (0)