Skip to content

Commit

Permalink
fix: adjusts to tests cleanup (#229)
Browse files Browse the repository at this point in the history
* cleanup adjusts

* add mocha cleanup
  • Loading branch information
mshima authored Aug 26, 2024
1 parent 03588f6 commit 779d203
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .mocharc.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports = {
extension: ['spec.ts'],
parallel: true,
require: ['src/mocha-cleanup.hooks.js'],
};
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
"author": "The Yeoman Team",
"type": "module",
"exports": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
"./mocha-cleanup": {
"import": "./dist/mocha-cleanup.hooks.js"
},
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"types": "./dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -43,7 +48,8 @@
"mem-fs-editor": "^11.1.1",
"sinon": "^18.0.0",
"temp-dir": "^3.0.0",
"type-fest": "^4.3.1"
"type-fest": "^4.3.1",
"when-exit": "^2.1.3"
},
"devDependencies": {
"@types/inquirer": "^9.0.3",
Expand Down
7 changes: 7 additions & 0 deletions src/mocha-cleanup.hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { context } from './index.js';

export const mochaHooks = {
afterAll() {
context.startNewContext();
},
};
30 changes: 22 additions & 8 deletions src/test-context.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
import process from 'node:process';
import onExit from 'when-exit';
import type RunContext from './run-context.js';
import type RunResult from './run-result.js';

class TestContext {
beforeCwd?: string;
autoRestore = true;
autoCleanup?: boolean;
runResult?: RunResult;
private runContext?: RunContext<any>;

startNewContext(runContext?: RunContext<any>) {
this.runContext?.cleanupTemporaryDir();
startNewContext(runContext?, autoCleanup = true) {
if (this.beforeCwd !== process.cwd()) {
if (this.autoCleanup) {
this.runContext?.cleanupTemporaryDir();
} else if (this.autoRestore) {

this.runContext?.restore();
}
}

if (this.beforeCwd && this.beforeCwd !== process.cwd()) {
console.log('Test failed to restore context', this.beforeCwd, process.cwd());
}

this.autoCleanup = autoCleanup;
this.beforeCwd = runContext ? process.cwd() : undefined;
this.runContext = runContext;
this.runResult = undefined;
}
}

const testContext = new TestContext();

const cleanupTemporaryDir = () => {
onExit(() => {
testContext.startNewContext();
};

process.on('exit', cleanupTemporaryDir);
process.on('SIGINT', cleanupTemporaryDir);
process.on('SIGTERM', cleanupTemporaryDir);
});

export default testContext;

Expand Down
2 changes: 1 addition & 1 deletion test/run-result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('run-result', () => {
beforeEach(() => {
cwd = path.join(process.cwd(), 'fixtures', 'tmp');
if (!fs.existsSync(cwd)) {
fs.mkdirSync(cwd);
fs.mkdirSync(cwd, { recursive: true });
}

runResult = new RunResult({
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/* Emit */
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
"declaration": true,
"skipLibCheck": true,

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

0 comments on commit 779d203

Please sign in to comment.