-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new setup test env functions
Closes #2755
- Loading branch information
Showing
30 changed files
with
437 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ src/config/setup-jest.ts | |
coverage | ||
src/transformers/jit_transform.js | ||
index.html | ||
*.mjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest.mjs'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest.mjs'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest.mjs'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest.mjs'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest.mjs'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
4 changes: 3 additions & 1 deletion
4
examples/example-app-yarn-workspace/packages/angular-app/setup-jest-esm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest.mjs'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
4 changes: 3 additions & 1 deletion
4
examples/example-app-yarn-workspace/packages/angular-app/setup-jest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
4 changes: 3 additions & 1 deletion
4
examples/example-app-yarn-workspace/packages/user/setup-jest-esm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest.mjs'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone/index.mjs'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
4 changes: 3 additions & 1 deletion
4
examples/example-app-yarn-workspace/packages/user/setup-jest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; | ||
import './jest-global-mocks'; | ||
|
||
setupZoneTestEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { TextDecoder, TextEncoder } from 'util'; | ||
|
||
export const polyfillEncoder = () => { | ||
if (typeof globalThis.TextEncoder === 'undefined') { | ||
globalThis.TextEncoder = TextEncoder; | ||
globalThis.TextDecoder = TextDecoder; | ||
} | ||
}; | ||
|
||
export const resolveTestEnvOptions = (options) => { | ||
const globalTestEnvOptions = globalThis.ngJest?.testEnvironmentOptions; | ||
if (globalTestEnvOptions) { | ||
console.warn( | ||
'Setting testEnvironmentOptions via globalThis.ngJest is deprecated. Please provide testEnvironmentOptions via function argument', | ||
); | ||
} | ||
|
||
return globalTestEnvOptions ?? options; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { TestEnvironmentOptions } from '@angular/core/testing'; | ||
|
||
export declare const setupZoneTestEnv: (options?: TestEnvironmentOptions) => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { TestEnvironmentOptions } from '@angular/core/testing'; | ||
|
||
declare const _default: { | ||
setupZoneTestEnv: (options?: TestEnvironmentOptions) => void; | ||
}; | ||
export = _default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require('zone.js'); | ||
require('zone.js/testing'); | ||
|
||
const { TextEncoder, TextDecoder } = require('util'); | ||
|
||
const { getTestBed } = require('@angular/core/testing'); | ||
const { | ||
BrowserDynamicTestingModule, | ||
platformBrowserDynamicTesting, | ||
} = require('@angular/platform-browser-dynamic/testing'); | ||
|
||
const polyfillEncoder = () => { | ||
if (typeof globalThis.TextEncoder === 'undefined') { | ||
globalThis.TextEncoder = TextEncoder; | ||
globalThis.TextDecoder = TextDecoder; | ||
} | ||
}; | ||
|
||
const resolveTestEnvOptions = (options) => { | ||
const globalTestEnvOptions = globalThis.ngJest?.testEnvironmentOptions; | ||
if (globalTestEnvOptions) { | ||
console.warn( | ||
'Setting testEnvironmentOptions via globalThis.ngJest is deprecated. Please provide testEnvironmentOptions via function argument', | ||
); | ||
} | ||
|
||
return globalTestEnvOptions ?? options; | ||
}; | ||
|
||
const setupZoneTestEnv = (options) => { | ||
polyfillEncoder(); | ||
const testEnvironmentOptions = resolveTestEnvOptions(options); | ||
|
||
getTestBed().initTestEnvironment( | ||
BrowserDynamicTestingModule, | ||
platformBrowserDynamicTesting(), | ||
testEnvironmentOptions, | ||
); | ||
}; | ||
|
||
module.exports = { | ||
setupZoneTestEnv, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'zone.js'; | ||
import 'zone.js/testing'; | ||
|
||
import { getTestBed } from '@angular/core/testing'; | ||
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; | ||
|
||
import { polyfillEncoder, resolveTestEnvOptions } from '../utils.mjs'; | ||
|
||
export const setupZoneTestEnv = (options) => { | ||
polyfillEncoder(); | ||
const testEnvironmentOptions = resolveTestEnvOptions(options); | ||
|
||
getTestBed().initTestEnvironment( | ||
BrowserDynamicTestingModule, | ||
platformBrowserDynamicTesting(), | ||
testEnvironmentOptions, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { TestEnvironmentOptions } from '@angular/core/testing'; | ||
|
||
export declare const setupZonelessTestEnv: (options?: TestEnvironmentOptions) => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { TestEnvironmentOptions } from '@angular/core/testing'; | ||
|
||
declare const _default: { | ||
setupZonelessTestEnv: (options?: TestEnvironmentOptions) => void; | ||
}; | ||
export = _default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const setupZonelessTestEnv = (_options) => { | ||
throw Error( | ||
'Zoneless testing environment only works when running Jest in ESM mode with Jest 29. ' + | ||
'Jest 30+ will support to work with CommonJS mode.', | ||
); | ||
}; | ||
|
||
module.exports = { | ||
setupZonelessTestEnv, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ErrorHandler, NgModule, provideExperimentalZonelessChangeDetection } from '@angular/core'; | ||
import { getTestBed } from '@angular/core/testing'; | ||
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; | ||
|
||
import { polyfillEncoder, resolveTestEnvOptions } from '../utils.mjs'; | ||
|
||
export const setupZonelessTestEnv = (options) => { | ||
if (typeof provideExperimentalZonelessChangeDetection !== 'undefined') { | ||
polyfillEncoder(); | ||
const testEnvironmentOptions = resolveTestEnvOptions(options); | ||
@NgModule({ | ||
providers: [ | ||
provideExperimentalZonelessChangeDetection(), | ||
{ | ||
provide: ErrorHandler, | ||
useValue: { | ||
handleError: (e) => { | ||
throw e; | ||
}, | ||
}, | ||
}, | ||
], | ||
}) | ||
export class TestModule {} | ||
|
||
getTestBed().initTestEnvironment( | ||
[BrowserDynamicTestingModule, TestModule], | ||
platformBrowserDynamicTesting(), | ||
testEnvironmentOptions, | ||
); | ||
|
||
return; | ||
} | ||
|
||
throw Error( | ||
'Cannot find provideExperimentalZonelessChangeDetection() to setup zoneless testing environment. ' + | ||
'Please use setupZoneTestEnv() from jest-preset-angular/setup-env/setup-zone-env.mjs instead.', | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.