Skip to content

Commit 458df12

Browse files
authored
Merge pull request #16 from jerboa88/13-add-tests
13 add tests
2 parents 4059566 + 7e1a30f commit 458df12

File tree

11 files changed

+2718
-53
lines changed

11 files changed

+2718
-53
lines changed

jest.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
collectCoverage: true,
6+
collectCoverageFrom: ['src/**/*.ts'],
7+
coverageDirectory: 'coverage',
8+
coverageProvider: 'v8',
9+
transform: {
10+
'^.+\\.ts$': [
11+
'ts-jest',
12+
{
13+
diagnostics: {
14+
warnOnly: true,
15+
},
16+
},
17+
],
18+
},
19+
};

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"build": "tsc",
4141
"watch": "tsc --watch",
4242
"clean": "tsc --build --clean",
43-
"prepare": "npm run build"
43+
"test": "jest",
44+
"prepare": "npm run test && npm run build"
4445
},
4546
"peerDependencies": {
4647
"gatsby": "^5.0.0-next"
@@ -51,10 +52,14 @@
5152
},
5253
"devDependencies": {
5354
"@biomejs/biome": "1.7.3",
55+
"@jest/globals": "^29.7.0",
5456
"@tsconfig/node20": "^20.1.4",
5557
"@types/express": "^4.17.21",
5658
"gatsby": "^5.0.0-next",
5759
"gatsby-plugin-utils": "^4.0.0",
60+
"jest": "^29.7.0",
61+
"joi": "^17.13.1",
62+
"ts-jest": "^29.1.4",
5863
"typescript": "^5.4.5"
5964
},
6065
"engines": {

src/gatsby-node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { setDefaultOptions, setGatsbyCreatePageFunction } from './config';
33
import { generateImages } from './generator';
44
import { info, setReporter } from './logger';
55
import type { DefaultOptions } from './types';
6-
import { prettify } from './utilities';
6+
import { prettify } from './utils';
77
import { getPluginOptionsSchema, setJoi } from './validator';
88

99
// Save the reporter and createPage function for later use

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from './config';
77
import { info } from './logger';
88
import type { DefaultOptions, JobOptions } from './types';
9-
import { prettify } from './utilities';
9+
import { prettify } from './utils';
1010
import { validateJobOptions } from './validator';
1111

1212
// Public function for setting default options

src/logger.test.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import {
2+
beforeAll,
3+
beforeEach,
4+
describe,
5+
expect,
6+
it,
7+
jest,
8+
} from '@jest/globals';
9+
import type { Reporter } from 'gatsby';
10+
import Joi from 'joi';
11+
import { setDefaultOptions } from './config';
12+
import {
13+
endActivity,
14+
error,
15+
info,
16+
panic,
17+
setReporter,
18+
startActivity,
19+
success,
20+
updateActivity,
21+
warn,
22+
} from './logger';
23+
import { setJoi } from './validator';
24+
25+
describe('info', () => {
26+
let reporter: Reporter;
27+
28+
beforeEach(() => {
29+
// @ts-expect-error: Reporter is intentially not fully mocked
30+
reporter = {
31+
info: jest.fn(),
32+
};
33+
// @ts-expect-error: Gatsby's PluginOptionsSchemaJoi type has an additional `subPlugins` property that we don't need
34+
setJoi(Joi);
35+
setDefaultOptions({ verbose: false });
36+
setReporter(reporter);
37+
});
38+
39+
it('should log an info message when verbose is true', () => {
40+
setDefaultOptions({ verbose: true });
41+
info('This is an info message');
42+
expect(reporter.info).toHaveBeenCalledWith(
43+
'gatsby-plugin-component-to-image: This is an info message',
44+
);
45+
});
46+
47+
it('should not log an info message when verbose is false', () => {
48+
setDefaultOptions({ verbose: false });
49+
info('This is an info message');
50+
expect(reporter.info).not.toHaveBeenCalled();
51+
});
52+
});
53+
54+
describe('success', () => {
55+
it('should log a success message', () => {
56+
const reporter = {
57+
success: jest.fn(),
58+
};
59+
60+
// @ts-expect-error: Reporter is intentially not fully mocked
61+
setReporter(reporter);
62+
success('This is a success message');
63+
expect(reporter.success).toHaveBeenCalledWith(
64+
'gatsby-plugin-component-to-image: This is a success message',
65+
);
66+
});
67+
});
68+
69+
describe('warn', () => {
70+
it('should log a warning message', () => {
71+
const reporter = {
72+
warn: jest.fn(),
73+
};
74+
75+
// @ts-expect-error: Reporter is intentially not fully mocked
76+
setReporter(reporter);
77+
warn('This is a warning message');
78+
expect(reporter.warn).toHaveBeenCalledWith(
79+
'gatsby-plugin-component-to-image: This is a warning message',
80+
);
81+
});
82+
});
83+
84+
describe('error', () => {
85+
it('should log an error message', () => {
86+
const reporter = {
87+
error: jest.fn(),
88+
};
89+
90+
// @ts-expect-error: Reporter is intentially not fully mocked
91+
setReporter(reporter);
92+
error('This is an error message');
93+
expect(reporter.error).toHaveBeenCalledWith(
94+
'gatsby-plugin-component-to-image: This is an error message',
95+
);
96+
});
97+
});
98+
99+
describe('panic', () => {
100+
it('should log a panic message', () => {
101+
const reporter = {
102+
panic: jest.fn(),
103+
};
104+
105+
// @ts-expect-error: Reporter is intentially not fully mocked
106+
setReporter(reporter);
107+
panic('This is a panic message');
108+
expect(reporter.panic).toHaveBeenCalledWith(
109+
'gatsby-plugin-component-to-image: This is a panic message',
110+
);
111+
});
112+
});
113+
114+
describe('startActivity, updateActivity, and endActivity', () => {
115+
const reporter = {
116+
activityTimer: jest.fn(),
117+
};
118+
const activity = {
119+
start: jest.fn(),
120+
setStatus: jest.fn(),
121+
end: jest.fn(),
122+
};
123+
124+
beforeAll(() => {
125+
reporter.activityTimer.mockReturnValue(activity);
126+
127+
// @ts-expect-error: Reporter is intentially not fully mocked
128+
setReporter(reporter);
129+
});
130+
131+
it('startActivity should start an activity', () => {
132+
startActivity('Timer');
133+
expect(reporter.activityTimer).toHaveBeenCalledWith(
134+
'gatsby-plugin-component-to-image: Timer',
135+
);
136+
expect(activity.start).toHaveBeenCalled();
137+
});
138+
139+
it('updateActivity should update the status of an activity', () => {
140+
updateActivity('Status');
141+
expect(activity.setStatus).toHaveBeenCalledWith('Status');
142+
});
143+
144+
it('endActivity should end an activity', () => {
145+
endActivity();
146+
expect(activity.setStatus).toHaveBeenCalledWith('');
147+
expect(activity.end).toHaveBeenCalled();
148+
});
149+
});

src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Reporter } from 'gatsby';
22
import { getDefaultOptions } from './config';
3-
import { getPackageName } from './utilities';
3+
import { getPackageName } from './utils';
44

55
const LOG_PREFIX = `${getPackageName()}: ` as const;
66

src/utils.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { assertIsDefined, getPackageName, prettify } from './utils';
3+
4+
describe('assertIsDefined', () => {
5+
it('should throw an error if value is undefined', () => {
6+
expect(() => assertIsDefined(undefined)).toThrow(
7+
'Expected value to be defined, but it was not',
8+
);
9+
});
10+
11+
it('should not throw an error if value is defined', () => {
12+
expect(() => assertIsDefined('defined value')).not.toThrow();
13+
});
14+
});
15+
16+
describe('getPackageName', () => {
17+
it('should return the package name from package.json', () => {
18+
expect(getPackageName()).toBe('gatsby-plugin-component-to-image');
19+
});
20+
});
21+
22+
describe('prettify', () => {
23+
it('should format an object as a human-readable string', () => {
24+
const obj = {
25+
name: 'Timmy',
26+
size: 'chonky',
27+
color: 'orange',
28+
};
29+
const expectedOutput = `{
30+
"name": "Timmy",
31+
"size": "chonky",
32+
"color": "orange"
33+
}`;
34+
expect(prettify(obj)).toBe(expectedOutput);
35+
});
36+
});
File renamed without changes.

0 commit comments

Comments
 (0)