Skip to content

Commit 779d203

Browse files
authoredAug 26, 2024··
fix: adjusts to tests cleanup (#229)
* cleanup adjusts * add mocha cleanup
1 parent 03588f6 commit 779d203

7 files changed

+49
-13
lines changed
 

Diff for: ‎.mocharc.cjs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module.exports = {
22
extension: ['spec.ts'],
3+
parallel: true,
4+
require: ['src/mocha-cleanup.hooks.js'],
35
};

Diff for: ‎package-lock.json

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
"author": "The Yeoman Team",
1313
"type": "module",
1414
"exports": {
15-
"types": "./dist/index.d.ts",
16-
"import": "./dist/index.js"
15+
"./mocha-cleanup": {
16+
"import": "./dist/mocha-cleanup.hooks.js"
17+
},
18+
".": {
19+
"types": "./dist/index.d.ts",
20+
"import": "./dist/index.js"
21+
}
1722
},
1823
"types": "./dist/index.d.ts",
1924
"files": [
@@ -43,7 +48,8 @@
4348
"mem-fs-editor": "^11.1.1",
4449
"sinon": "^18.0.0",
4550
"temp-dir": "^3.0.0",
46-
"type-fest": "^4.3.1"
51+
"type-fest": "^4.3.1",
52+
"when-exit": "^2.1.3"
4753
},
4854
"devDependencies": {
4955
"@types/inquirer": "^9.0.3",

Diff for: ‎src/mocha-cleanup.hooks.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { context } from './index.js';
2+
3+
export const mochaHooks = {
4+
afterAll() {
5+
context.startNewContext();
6+
},
7+
};

Diff for: ‎src/test-context.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
import process from 'node:process';
2+
import onExit from 'when-exit';
23
import type RunContext from './run-context.js';
34
import type RunResult from './run-result.js';
45

56
class TestContext {
7+
beforeCwd?: string;
8+
autoRestore = true;
9+
autoCleanup?: boolean;
610
runResult?: RunResult;
711
private runContext?: RunContext<any>;
812

9-
startNewContext(runContext?: RunContext<any>) {
10-
this.runContext?.cleanupTemporaryDir();
13+
startNewContext(runContext?, autoCleanup = true) {
14+
if (this.beforeCwd !== process.cwd()) {
15+
if (this.autoCleanup) {
16+
this.runContext?.cleanupTemporaryDir();
17+
} else if (this.autoRestore) {
18+
19+
this.runContext?.restore();
20+
}
21+
}
22+
23+
if (this.beforeCwd && this.beforeCwd !== process.cwd()) {
24+
console.log('Test failed to restore context', this.beforeCwd, process.cwd());
25+
}
26+
27+
this.autoCleanup = autoCleanup;
28+
this.beforeCwd = runContext ? process.cwd() : undefined;
1129
this.runContext = runContext;
1230
this.runResult = undefined;
1331
}
1432
}
1533

1634
const testContext = new TestContext();
1735

18-
const cleanupTemporaryDir = () => {
36+
onExit(() => {
1937
testContext.startNewContext();
20-
};
21-
22-
process.on('exit', cleanupTemporaryDir);
23-
process.on('SIGINT', cleanupTemporaryDir);
24-
process.on('SIGTERM', cleanupTemporaryDir);
38+
});
2539

2640
export default testContext;
2741

Diff for: ‎test/run-result.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe('run-result', () => {
171171
beforeEach(() => {
172172
cwd = path.join(process.cwd(), 'fixtures', 'tmp');
173173
if (!fs.existsSync(cwd)) {
174-
fs.mkdirSync(cwd);
174+
fs.mkdirSync(cwd, { recursive: true });
175175
}
176176

177177
runResult = new RunResult({

Diff for: ‎tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
/* Emit */
1515
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
1616
"declaration": true,
17+
"skipLibCheck": true,
1718

1819
/* Interop Constraints */
1920
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,

0 commit comments

Comments
 (0)
Please sign in to comment.