Skip to content

Commit

Permalink
test: Set preload script for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Mar 27, 2024
1 parent c429368 commit 9d8e9b7
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 164 deletions.
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
preload = ["./test/setup.ts"]
2 changes: 1 addition & 1 deletion src/config/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const currentProjectFile = (file = `${rootPath}/current-project`) => {
if (isFile(file)) {
projectFile = file;
} else {
log.error(`Project file ${file} could not be found.\nDoes it exists?`);
log.error(`Project file ${file} could not be found. Does it exists?`);
throw new Error('Could not select or determine the current project file.');

Check failure on line 17 in src/config/cli.ts

View workflow job for this annotation

GitHub Actions / build

error: Could not select or determine the current project file.

at currentProjectFile (/home/runner/work/dems-cli/dems-cli/src/config/cli.ts:17:11) at /home/runner/work/dems-cli/dems-cli/src/config/cli.ts:36:23
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/compose.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs';
import * as path from 'node:path';
import path from 'node:path';
import { projectConfig } from '../config/project';
import { cmd as $ } from './cmd';
import { isFile } from './file-system';
Expand Down
36 changes: 36 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { afterEach, beforeAll, beforeEach, jest, mock, spyOn } from 'bun:test';

beforeAll(() => {
mock.module('node:fs', () => ({
default: {
copyFileSync: mock(),
existsSync: mock(),
lstatSync: mock(),
mkdirSync: mock(),
readdirSync: mock(),
rmdirSync: mock(),
rmSync: mock(),
readFileSync: mock(),
writeFileSync: mock(),
},
}));

mock.module('@inquirer/prompts', () => ({
confirm: mock(),
}));

mock.module('node:child_process', () => ({
execSync: mock(() => {
return;
}),
}));
});

beforeEach(() => {
spyOn(console, 'log').mockImplementation(() => {});
});

afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});
8 changes: 1 addition & 7 deletions test/utils/cmd.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { describe, expect, type jest, mock, test } from 'bun:test';
import { describe, expect, type jest, test } from 'bun:test';
import { execSync } from 'node:child_process';
import cmd from '../../src/utils/cmd';
import { removeExtraSpaces } from '../../src/utils/string';

mock.module('node:child_process', () => ({
execSync: mock(() => {
return;
}),
}));

describe('Utils: cmd', () => {
describe('run', () => {
test('calls execSync with formatted command', () => {
Expand Down
30 changes: 3 additions & 27 deletions test/utils/compose.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
import {
afterEach,
beforeEach,
describe,
expect,
jest,
mock,
spyOn,
test,
} from 'bun:test';
import { describe, expect, type jest, test } from 'bun:test';
import fs from 'node:fs';
import type { DEMSProjectConfig } from '../../src/config/dems';
import { composeExecParams, composeFiles } from '../../src/utils/compose';

mock.module('node:fs', () => ({
default: {
existsSync: mock(),
lstatSync: mock(),
readdirSync: mock(),
},
}));

const testConfigJson: DEMSProjectConfig = {
compose: {
project_name: 'my-project',
Expand All @@ -37,15 +20,6 @@ const testConfigJson: DEMSProjectConfig = {
},
};

beforeEach(() => {
spyOn(console, 'log').mockImplementation(() => {});
});

afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});

describe('Utils: compose', () => {
describe('composeExecParams', () => {
test('should return an array of compose parameters', () => {
Expand All @@ -69,6 +43,8 @@ describe('Utils: compose', () => {
describe('Utils: compose', () => {
describe('composeFiles', () => {
test('should return an array of compose files with --file flag', () => {
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.lstatSync as jest.Mock).mockReturnValue({ isFile: () => true });
(fs.readdirSync as jest.Mock).mockReturnValue([
'compose1.yml',
'compose2.yml',
Expand Down
32 changes: 1 addition & 31 deletions test/utils/file-system.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
afterEach,
beforeEach,
describe,
expect,
jest,
mock,
spyOn,
test,
} from 'bun:test';
import { describe, expect, type jest, mock, test } from 'bun:test';
import fs from 'node:fs';
import { confirm } from '@inquirer/prompts';
import {
Expand All @@ -19,31 +10,10 @@ import {
isFile,
} from '../../src/utils/file-system';

mock.module('node:fs', () => ({
default: {
copyFileSync: mock(),
existsSync: mock(),
lstatSync: mock(),
mkdirSync: mock(),
rmdirSync: mock(),
rmSync: mock(),
writeFileSync: mock(),
},
}));

mock.module('@inquirer/prompts', () => ({
confirm: mock(),
}));

beforeEach(() => {
spyOn(console, 'log').mockImplementation(() => {});
});

afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});

describe('Utils: file-system', () => {
describe('isFile', () => {
test('returns true if file exists', () => {
Expand Down
34 changes: 1 addition & 33 deletions test/utils/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
afterEach,
beforeEach,
describe,
expect,
jest,
mock,
spyOn,
test,
} from 'bun:test';
import { describe, expect, type jest, test } from 'bun:test';
import { execSync } from 'node:child_process';
import fs from 'node:fs';
import git, {
Expand All @@ -18,29 +9,6 @@ import git, {
validateLocalGitRepo,
} from '../../src/utils/git';

mock.module('node:fs', () => ({
default: {
existsSync: mock(),
lstatSync: mock(),
mkdirSync: mock(),
},
}));

mock.module('node:child_process', () => ({
execSync: mock(() => {
return;
}),
}));

beforeEach(() => {
spyOn(console, 'log').mockImplementation(() => {});
});

afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});

describe('Utils: git', () => {
describe('clone', () => {
test('clones the repo if it does not exist locally', () => {
Expand Down
18 changes: 1 addition & 17 deletions test/utils/log.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import {
afterEach,
beforeEach,
describe,
expect,
jest,
spyOn,
test,
} from 'bun:test';
import { describe, expect, test } from 'bun:test';
import chalk from 'chalk';
import log from '../../src/utils/log';

describe('log', () => {
beforeEach(() => {
spyOn(console, 'log').mockImplementation(() => {});
});

afterEach(() => {
jest.restoreAllMocks();
});

test('should log info message in blue color', () => {
log.info('info message');
expect(console.log).toHaveBeenCalledWith(chalk.blue('info message'));
Expand Down
48 changes: 1 addition & 47 deletions test/utils/object.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
import { afterEach, describe, expect, jest, mock, test } from 'bun:test';
import fs from 'node:fs';
import { isFile } from '../../src/utils/file-system';
import log from '../../src/utils/log';
import {
flattenObject,
replaceKeyValue,
replaceKeysInFile,
} from '../../src/utils/object';

mock.module('node:fs', () => ({
default: {
existsSync: mock(),
lstatSync: mock(),
readFileSync: mock(),
writeFileSync: mock(),
},
}));

mock.module('../../src/utils/file-system', () => ({
isFile: mock(),
}));

mock.module('../../src/utils/log', () => ({
default: {
info: mock(),
warning: mock(),
success: mock(),
error: mock(),
},
}));

describe('Utils: object', () => {
afterEach(() => {
jest.clearAllMocks();
});

describe('replaceKeyFile', () => {
test('updates the key-value pair in the file', () => {
const path = 'test-path';
Expand All @@ -46,10 +23,6 @@ describe('Utils: object', () => {

replaceKeyValue(path, key, value);

expect(log.error).not.toHaveBeenCalled();
expect(log.success).toHaveBeenCalledWith(
'File test-path updated successfully with test_key=test value.',
);
expect(fs.writeFileSync).toHaveBeenCalledWith(
'test-path',
'test_key=test value',
Expand All @@ -65,10 +38,6 @@ describe('Utils: object', () => {

replaceKeyValue(path, key, value);

expect(log.error).toHaveBeenCalledWith(
'File test-path is not a valid file.',
);
expect(log.success).not.toHaveBeenCalled();
expect(fs.writeFileSync).not.toHaveBeenCalled();
});

Expand All @@ -83,10 +52,7 @@ describe('Utils: object', () => {
});

expect(() => replaceKeyValue(path, key, value)).toThrow(Error);
expect(log.error).toHaveBeenCalledWith(
'Error updating file test-path. See below for more info:',
);
expect(log.success).not.toHaveBeenCalled();

expect(fs.writeFileSync).not.toHaveBeenCalled();
});
});
Expand All @@ -109,10 +75,6 @@ describe('Utils: object', () => {

replaceKeysInFile(filePath, replaceMap);

expect(log.error).not.toHaveBeenCalled();
expect(log.success).toHaveBeenCalledWith(
'File test-file-path updated successfully with replacements.',
);
expect(fs.writeFileSync).toHaveBeenCalledWith(
'test-file-path',
'key1=value1\nkey2=value2',
Expand All @@ -127,10 +89,6 @@ describe('Utils: object', () => {

replaceKeysInFile(filePath, replaceMap);

expect(log.error).toHaveBeenCalledWith(
'File test-file-path is not a valid file.',
);
expect(log.success).not.toHaveBeenCalled();
expect(fs.writeFileSync).not.toHaveBeenCalled();
});

Expand All @@ -144,10 +102,6 @@ describe('Utils: object', () => {
});

expect(() => replaceKeysInFile(filePath, replaceMap)).toThrow(Error);
expect(log.error).toHaveBeenCalledWith(
'Error updating file test-file-path. See below for more info:',
);
expect(log.success).not.toHaveBeenCalled();
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
});
Expand Down

0 comments on commit 9d8e9b7

Please sign in to comment.