Skip to content

Commit c335226

Browse files
committed
test: add more test coverage
1 parent 08b119f commit c335226

5 files changed

Lines changed: 122 additions & 21 deletions

File tree

src/files.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ export const writeFile = (file: string, contents: string): void => {
4040
/**
4141
* Return an array of file paths that matches the given files string.
4242
*/
43-
export const getAllFilesToTransform = (files: string) => glob.sync(files);
43+
export const getAllFilesToTransform = (files: string) =>
44+
glob.sync(files, { nodir: true });

test/cli.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('cli', () => {
2+
it('should perform file transformations', () => {
3+
// cli();
4+
});
5+
});

test/files.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { getAllFilesToTransform, getFileInfo, writeFile } from '../src/files';
4+
5+
describe('files', () => {
6+
let fsWriteFileSync: jest.SpyInstance;
7+
let consoleError: jest.SpyInstance;
8+
let processExit: jest.SpyInstance;
9+
10+
beforeEach(() => {
11+
fsWriteFileSync = jest.spyOn(fs, 'writeFileSync').mockImplementation();
12+
consoleError = jest.spyOn(console, 'error').mockImplementation();
13+
processExit = jest.spyOn(process, 'exit').mockImplementation();
14+
});
15+
16+
afterEach(() => {
17+
jest.restoreAllMocks();
18+
});
19+
20+
describe('#getFileInfo', () => {
21+
it('should return file info for the file', () => {
22+
const testFile = path.join(__dirname, 'css', 'a.css');
23+
expect(getFileInfo(testFile)).toEqual({
24+
path: testFile,
25+
source: `.class {
26+
margin: 42px;
27+
color: blue;
28+
padding-right: 10em;
29+
background-color: rgba(0, 0, 0, 0.2);
30+
}
31+
`,
32+
});
33+
expect(consoleError).not.toHaveBeenCalled();
34+
expect(processExit).not.toHaveBeenCalled();
35+
});
36+
37+
it('should exit if an error occurs reading a file', () => {
38+
const testFile = path.join(__dirname, 'css', 'totally-bogus.css');
39+
getFileInfo(testFile);
40+
expect(consoleError).toHaveBeenCalledTimes(1);
41+
expect(processExit).toHaveBeenCalledTimes(1);
42+
});
43+
});
44+
45+
describe('#writeFile', () => {
46+
it('should write the files contents', () => {
47+
writeFile('./testing.css', '.class {}');
48+
expect(fsWriteFileSync).toHaveBeenCalledTimes(1);
49+
expect(fsWriteFileSync).toHaveBeenCalledWith(
50+
'./testing.css',
51+
'.class {}'
52+
);
53+
expect(consoleError).not.toHaveBeenCalled();
54+
expect(processExit).not.toHaveBeenCalled();
55+
});
56+
57+
it('should print the error but not exit if an error occurs writing a file', () => {
58+
fsWriteFileSync.mockImplementation(() => {
59+
throw new Error('Cannot write to file.');
60+
});
61+
62+
writeFile('./testing.css', '.class {}');
63+
expect(consoleError).toHaveBeenCalledTimes(1);
64+
expect(processExit).toHaveBeenCalledTimes(0);
65+
});
66+
});
67+
68+
describe('#getAllFilesToTransform', () => {
69+
it('should expand the file path and return and array of resolved file paths', () => {
70+
expect(getAllFilesToTransform('./test/**/*.css')).toEqual([
71+
'./test/css/a.css',
72+
'./test/css/b.css',
73+
]);
74+
expect(getAllFilesToTransform('./test/css/*.css')).toEqual([
75+
'./test/css/a.css',
76+
'./test/css/b.css',
77+
]);
78+
});
79+
80+
it('should ignore directories', () => {
81+
expect(getAllFilesToTransform('./test')).toEqual([]);
82+
expect(getAllFilesToTransform('./test/css')).toEqual([]);
83+
});
84+
});
85+
});

test/perform.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// import { perform } from '../src/perform';
2+
3+
describe('perform', () => {
4+
describe('#perform', () => {
5+
it('should process the files and perform transformations', () => {
6+
// perform();
7+
});
8+
});
9+
});

test/transform.test.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { validateTransform, loadTransform, Transform } from '../src/transform';
44
jest.mock('bundle-require', () => ({ bundleRequire: jest.fn() }));
55

66
describe('transform', () => {
7-
let exit: jest.SpyInstance;
7+
let consoleError: jest.SpyInstance;
8+
let processExit: jest.SpyInstance;
89

910
beforeEach(() => {
10-
console.error = jest.fn();
11-
exit = jest.spyOn(process, 'exit').mockImplementation();
11+
consoleError = jest.spyOn(console, 'error').mockImplementation();
12+
processExit = jest.spyOn(process, 'exit').mockImplementation();
1213
});
1314

1415
afterEach(() => {
15-
exit.mockRestore();
16+
jest.restoreAllMocks();
1617
});
1718

1819
describe('#validateTransform', () => {
@@ -26,25 +27,25 @@ describe('transform', () => {
2627
const twoArg: Transform = (_fileInfo, _api) => null;
2728
expect(validateTransform(twoArg)).toBe(twoArg);
2829

29-
expect(console.error).not.toHaveBeenCalled();
30-
expect(exit).not.toHaveBeenCalled();
30+
expect(consoleError).not.toHaveBeenCalled();
31+
expect(processExit).not.toHaveBeenCalled();
3132
});
3233

3334
it('should exit if an invalid transform is provided since there is no way to recover', () => {
34-
expect(console.error).toHaveBeenCalledTimes(0);
35-
expect(exit).toHaveBeenCalledTimes(0);
35+
expect(consoleError).toHaveBeenCalledTimes(0);
36+
expect(processExit).toHaveBeenCalledTimes(0);
3637

3738
validateTransform(null);
38-
expect(console.error).toHaveBeenCalledTimes(1);
39-
expect(exit).toHaveBeenCalledTimes(1);
39+
expect(consoleError).toHaveBeenCalledTimes(1);
40+
expect(processExit).toHaveBeenCalledTimes(1);
4041

4142
validateTransform(undefined);
42-
expect(console.error).toHaveBeenCalledTimes(2);
43-
expect(exit).toHaveBeenCalledTimes(2);
43+
expect(consoleError).toHaveBeenCalledTimes(2);
44+
expect(processExit).toHaveBeenCalledTimes(2);
4445

4546
validateTransform((_fileInfo: any, _api: any, _invalid: any) => null);
46-
expect(console.error).toHaveBeenCalledTimes(3);
47-
expect(exit).toHaveBeenCalledTimes(3);
47+
expect(consoleError).toHaveBeenCalledTimes(3);
48+
expect(processExit).toHaveBeenCalledTimes(3);
4849
});
4950
});
5051

@@ -58,8 +59,8 @@ describe('transform', () => {
5859
});
5960

6061
expect(await loadTransform('./transform.ts')).toBe(transform);
61-
expect(console.error).not.toHaveBeenCalled();
62-
expect(exit).not.toHaveBeenCalled();
62+
expect(consoleError).not.toHaveBeenCalled();
63+
expect(processExit).not.toHaveBeenCalled();
6364
});
6465

6566
it('should load the transform file and fallback to the default export', async () => {
@@ -70,8 +71,8 @@ describe('transform', () => {
7071
});
7172

7273
expect(await loadTransform('./transform.ts')).toBe(transform);
73-
expect(console.error).not.toHaveBeenCalled();
74-
expect(exit).not.toHaveBeenCalled();
74+
expect(consoleError).not.toHaveBeenCalled();
75+
expect(processExit).not.toHaveBeenCalled();
7576
});
7677

7778
it('should exit if an error occurs loading the transform file', async () => {
@@ -80,8 +81,8 @@ describe('transform', () => {
8081
);
8182

8283
await loadTransform('./transform.ts');
83-
expect(console.error).toHaveBeenCalledTimes(1);
84-
expect(exit).toHaveBeenCalledTimes(1);
84+
expect(consoleError).toHaveBeenCalledTimes(1);
85+
expect(processExit).toHaveBeenCalledTimes(1);
8586
});
8687
});
8788
});

0 commit comments

Comments
 (0)