Skip to content

Commit 2df83d3

Browse files
committed
feat: add automatic mock for angular entities (#2908)
1 parent 77b4332 commit 2df83d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1582
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Component, input } from '@angular/core';
2+
3+
@Component({
4+
selector: 'test',
5+
template: 'this is a manual mock for test',
6+
standalone: true,
7+
styles: [':host { display: block; }'],
8+
})
9+
export class TestComponent {
10+
public value = input.required<string>();
11+
12+
method() {}
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Component } from '@angular/core';
2+
import { TestBed } from '@angular/core/testing';
3+
4+
import * as testManualMockComponent from '../test-manual-mock.component';
5+
import * as testModule from '../test.module';
6+
7+
jest.mock('../test.module');
8+
jest.mock('../test-manual-mock.component');
9+
10+
it('should mock module', () => {
11+
@Component({
12+
template: `
13+
<test [value]="'value'">{{ 'test' | test }}</test>
14+
<div test [value]="'value'">{{ 'test' | test }}</div>
15+
`,
16+
imports: [testModule.TestModule],
17+
})
18+
class TestComponent {}
19+
20+
jest.spyOn(console, 'error');
21+
22+
expect(() => TestBed.createComponent(TestComponent).detectChanges()).not.toThrow();
23+
24+
expect(console.error).not.toHaveBeenCalled();
25+
});
26+
27+
it('should ignore modules with manual mock', () => {
28+
const fixture = TestBed.createComponent(testManualMockComponent.TestComponent);
29+
30+
fixture.detectChanges();
31+
32+
expect(fixture.debugElement.nativeElement.innerHTML).toBe('this is a manual mock for test');
33+
expect(jest.isMockFunction(fixture.componentInstance.method)).toBeFalsy();
34+
});

e2e/automocks/jest-cjs.config.ts

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-automocks',
5+
testEnvironment: 'jsdom',
6+
setupFilesAfterEnv: ['<rootDir>/../setup-test-env.ts', '<rootDir>/setup-test-env.ts'],
7+
transform: {
8+
'^.+\\.(ts|mjs|js|html)$': [
9+
'<rootDir>/../../build/index.js',
10+
{
11+
babelConfig: true,
12+
tsconfig: '<rootDir>/tsconfig-cjs.spec.json',
13+
stringifyContentPathRegex: '\\.(html|svg)$',
14+
},
15+
],
16+
},
17+
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
18+
};
19+
20+
export default config;

e2e/automocks/jest-esm.config.ts

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-automocks',
5+
extensionsToTreatAsEsm: ['.ts', '.mts'],
6+
setupFilesAfterEnv: ['<rootDir>/../setup-test-env.mts', '<rootDir>/setup-test-env.mts'],
7+
transform: {
8+
'^.+\\.(ts|mts|mjs|js|html)$': [
9+
'<rootDir>/../../build/index.js',
10+
{
11+
babelConfig: true,
12+
useESM: true,
13+
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
14+
},
15+
],
16+
},
17+
};
18+
19+
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-automocks',
5+
testEnvironment: 'jsdom',
6+
setupFilesAfterEnv: ['<rootDir>/../setup-test-env.ts', '<rootDir>/setup-test-env.ts'],
7+
transform: {
8+
'^.+\\.(ts|mjs|js|html)$': [
9+
'<rootDir>/../../build/index.js',
10+
{
11+
babelConfig: true,
12+
tsconfig: '<rootDir>/tsconfig-transpile-cjs.spec.json',
13+
stringifyContentPathRegex: '\\.(html|svg)$',
14+
},
15+
],
16+
},
17+
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
18+
};
19+
20+
export default config;
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-automocks',
5+
extensionsToTreatAsEsm: ['.ts', '.mts'],
6+
setupFilesAfterEnv: ['<rootDir>/../setup-test-env.mts', '<rootDir>/setup-test-env.mts'],
7+
transform: {
8+
'^.+\\.(ts|mts|mjs|js|html)$': [
9+
'<rootDir>/../../build/index.js',
10+
{
11+
babelConfig: true,
12+
useESM: true,
13+
tsconfig: '<rootDir>/tsconfig-transpile-esm.spec.json',
14+
},
15+
],
16+
},
17+
};
18+
19+
export default config;

e2e/automocks/setup-test-env.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { setupAutoMocks } from '../../setup-env/automocks.mjs';
2+
3+
setupAutoMocks();

e2e/automocks/setup-test-env.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { setupAutoMocks } from '../../setup-env/automocks.js';
2+
3+
setupAutoMocks();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Component, input } from '@angular/core';
2+
3+
@Component({
4+
selector: 'test',
5+
template: 'this is a test',
6+
standalone: true,
7+
styles: [':host { display: block; }'],
8+
})
9+
export class TestComponent {
10+
public value = input.required<string>();
11+
12+
method() {}
13+
}

e2e/automocks/test.component.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Component, input } from '@angular/core';
2+
3+
@Component({
4+
selector: 'test',
5+
template: 'this is a test',
6+
standalone: true,
7+
styles: [':host { display: block; }'],
8+
})
9+
export class TestComponent {
10+
public value = input.required<string>();
11+
12+
method() {}
13+
}

0 commit comments

Comments
 (0)