Skip to content

Commit 8138299

Browse files
authored
test: issue with global .asyncapi file getting overwritten while testing (#27)
* fix: issue with global .asyncapi file getting overwritten while testing * feat: changed context_filename check suggested by @magicmatatjahu
1 parent 92401f9 commit 8138299

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/constants.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import * as fs from 'fs';
44
import { SpecificationFile } from './hooks/validation';
55
import { Context } from './hooks/context/models';
66

7-
export const CONTEXTFILE_PATH = path.resolve(os.homedir(), '.asyncapi');
7+
const isTestEnv = (process.env['NODE_ENV'] === 'test') || (process.env['JEST_WORKER_ID'] !== undefined) || typeof jest !== 'undefined';
8+
9+
export const CONTEXT_FILENAME = isTestEnv ? ".test.asyncapi" : ".asyncapi";
10+
11+
export const CONTEXTFILE_PATH = path.resolve(os.homedir(), CONTEXT_FILENAME);
812

913
export class ContextTestingHelper {
1014
private _context: Context;
@@ -25,14 +29,14 @@ export class ContextTestingHelper {
2529
}
2630

2731
createDummyContextFile() {
28-
fs.writeFileSync(CONTEXTFILE_PATH, JSON.stringify(this._context), {encoding: 'utf-8'});
32+
fs.writeFileSync(CONTEXTFILE_PATH, JSON.stringify(this._context), { encoding: 'utf-8' });
2933
}
3034

3135
deleteDummyContextFile() {
32-
if(fs.existsSync(CONTEXTFILE_PATH)) fs.unlinkSync(CONTEXTFILE_PATH);
36+
if (fs.existsSync(CONTEXTFILE_PATH)) fs.unlinkSync(CONTEXTFILE_PATH);
3337
}
3438

35-
getPath(key: string){
39+
getPath(key: string) {
3640
return this._context.store[key];
3741
}
3842
}

0 commit comments

Comments
 (0)