Skip to content

Commit c59e545

Browse files
feat: auto-cleanup
Closes #67
2 parents a945a6f + e194bac commit c59e545

File tree

9 files changed

+216
-202
lines changed

9 files changed

+216
-202
lines changed

package.json

+16-16
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
]
2424
},
2525
"dependencies": {
26-
"@angular/animations": "^9.0.0",
27-
"@angular/cdk": "^9.0.0",
28-
"@angular/common": "^9.0.0",
29-
"@angular/compiler": "^9.0.0",
30-
"@angular/core": "^9.0.0",
31-
"@angular/forms": "^9.0.0",
32-
"@angular/material": "^9.0.0",
33-
"@angular/platform-browser": "^9.0.0",
34-
"@angular/platform-browser-dynamic": "^9.0.0",
35-
"@angular/router": "^9.0.0",
26+
"@angular/animations": "^9.0.3",
27+
"@angular/cdk": "^9.1.0",
28+
"@angular/common": "^9.0.3",
29+
"@angular/compiler": "^9.0.3",
30+
"@angular/core": "^9.0.3",
31+
"@angular/forms": "^9.0.3",
32+
"@angular/material": "^9.1.0",
33+
"@angular/platform-browser": "^9.0.3",
34+
"@angular/platform-browser-dynamic": "^9.0.3",
35+
"@angular/router": "^9.0.3",
3636
"@ngrx/store": "^8.0.0-rc.0",
3737
"@phenomnomnominal/tsquery": "^3.0.0",
3838
"@testing-library/dom": "^6.12.2",
@@ -44,14 +44,14 @@
4444
"zone.js": "~0.10.2"
4545
},
4646
"devDependencies": {
47-
"@angular-devkit/build-angular": "~0.900.1",
48-
"@angular-devkit/build-ng-packagr": "~0.900.1",
49-
"@angular/cli": "~9.0.1",
50-
"@angular/compiler-cli": "^9.0.0",
51-
"@angular/language-service": "^9.0.0",
47+
"@angular-devkit/build-angular": "~0.900.3",
48+
"@angular-devkit/build-ng-packagr": "~0.900.3",
49+
"@angular/cli": "~9.0.3",
50+
"@angular/compiler-cli": "^9.0.3",
51+
"@angular/language-service": "^9.0.3",
5252
"@testing-library/jest-dom": "^4.1.0",
5353
"@types/jest": "~24.0.11",
54-
"@types/node": "^12.11.1",
54+
"@types/node": "^13.7.6",
5555
"codelyzer": "^5.1.2",
5656
"husky": "^2.3.0",
5757
"jest": "^24.1.0",

projects/jest-utils/src/lib/configure-test-suite.ts

-26
This file was deleted.

projects/jest-utils/src/lib/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export * from './configure-test-suite';
21
export * from './create-mock';

projects/jest-utils/tsconfig.lib.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"experimentalDecorators": true,
1313
"importHelpers": true,
1414
"allowSyntheticDefaultImports": true,
15-
"types": [],
15+
"types": ["@types/jest"],
1616
"lib": ["dom", "es2015"]
1717
},
1818
"angularCompilerOptions": {

projects/testing-library/src/lib/testing-library.ts

+20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { createSelectOptions, createType, tab } from './user-events';
2020
@Component({ selector: 'wrapper-component', template: '' })
2121
class WrapperComponent {}
2222

23+
const mountedContainers = new Set();
24+
2325
export async function render<ComponentType>(
2426
component: Type<ComponentType>,
2527
renderOptions?: RenderComponentOptions<ComponentType>,
@@ -74,6 +76,7 @@ export async function render<SutType, WrapperType = SutType>(
7476
if (idAttribute && idAttribute.startsWith('root')) {
7577
fixture.nativeElement.removeAttribute('id');
7678
}
79+
mountedContainers.add(fixture.nativeElement);
7780
}
7881

7982
await TestBed.compileComponents();
@@ -236,3 +239,20 @@ function addAutoImports({ imports, routes }: Pick<RenderComponentOptions<any>, '
236239

237240
return [...imports, ...animations(), ...routing()];
238241
}
242+
243+
function cleanup() {
244+
mountedContainers.forEach(cleanupAtContainer);
245+
}
246+
247+
function cleanupAtContainer(container) {
248+
if (container.parentNode === document.body) {
249+
document.body.removeChild(container);
250+
}
251+
mountedContainers.delete(container);
252+
}
253+
254+
if (typeof afterEach === 'function' && !process.env.ATL_SKIP_AUTO_CLEANUP) {
255+
afterEach(async () => {
256+
cleanup();
257+
});
258+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Component } from '@angular/core';
2+
import { render } from '../src/public_api';
3+
4+
@Component({
5+
selector: 'fixture',
6+
template: `
7+
Hello!
8+
`,
9+
})
10+
class FixtureComponent {}
11+
12+
test('first', async () => {
13+
await render(FixtureComponent);
14+
});
15+
16+
test('second', () => {
17+
expect(document.body.innerHTML).toEqual('');
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://github.com/testing-library/angular-testing-library/issues/67
2+
import { Component } from '@angular/core';
3+
import { render } from '../../src/public_api';
4+
5+
@Component({
6+
template: `
7+
<div>
8+
<!-- if remove for="name" no error happens -->
9+
<label for="name">
10+
<input type="checkbox" id="name" data-testid="checkbox" />
11+
TEST
12+
</label>
13+
</div>
14+
`,
15+
})
16+
export class BugGetByLabelTextComponent {}
17+
18+
it('first step to reproduce the bug: skip this test to avoid the error or remove the for attribute of label', async () => {
19+
expect(await render(BugGetByLabelTextComponent)).toBeDefined();
20+
});
21+
22+
it('second step: bug happens :`(', async () => {
23+
const { getByLabelText, getByTestId } = await render(BugGetByLabelTextComponent);
24+
25+
const checkboxByTestId = getByTestId('checkbox');
26+
const checkboxByLabelTest = getByLabelText('TEST');
27+
28+
expect(checkboxByTestId).toBe(checkboxByLabelTest);
29+
});

projects/testing-library/tsconfig.lib.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"emitDecoratorMetadata": true,
1212
"experimentalDecorators": true,
1313
"importHelpers": true,
14-
"types": [],
14+
"types": ["@types/jest", "@types/node"],
1515
"lib": ["dom", "es2015", "es2018.promise"]
1616
},
1717
"angularCompilerOptions": {

0 commit comments

Comments
 (0)