Skip to content

13 add tests #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom: ['src/**/*.ts'],
coverageDirectory: 'coverage',
coverageProvider: 'v8',
transform: {
'^.+\\.ts$': [
'ts-jest',
{
diagnostics: {
warnOnly: true,
},
},
],
},
};
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"build": "tsc",
"watch": "tsc --watch",
"clean": "tsc --build --clean",
"prepare": "npm run build"
"test": "jest",
"prepare": "npm run test && npm run build"
},
"peerDependencies": {
"gatsby": "^5.0.0-next"
Expand All @@ -51,10 +52,14 @@
},
"devDependencies": {
"@biomejs/biome": "1.7.3",
"@jest/globals": "^29.7.0",
"@tsconfig/node20": "^20.1.4",
"@types/express": "^4.17.21",
"gatsby": "^5.0.0-next",
"gatsby-plugin-utils": "^4.0.0",
"jest": "^29.7.0",
"joi": "^17.13.1",
"ts-jest": "^29.1.4",
"typescript": "^5.4.5"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { setDefaultOptions, setGatsbyCreatePageFunction } from './config';
import { generateImages } from './generator';
import { info, setReporter } from './logger';
import type { DefaultOptions } from './types';
import { prettify } from './utilities';
import { prettify } from './utils';
import { getPluginOptionsSchema, setJoi } from './validator';

// Save the reporter and createPage function for later use
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from './config';
import { info } from './logger';
import type { DefaultOptions, JobOptions } from './types';
import { prettify } from './utilities';
import { prettify } from './utils';
import { validateJobOptions } from './validator';

// Public function for setting default options
Expand Down
149 changes: 149 additions & 0 deletions src/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import {
beforeAll,
beforeEach,
describe,
expect,
it,
jest,
} from '@jest/globals';
import type { Reporter } from 'gatsby';
import Joi from 'joi';
import { setDefaultOptions } from './config';
import {
endActivity,
error,
info,
panic,
setReporter,
startActivity,
success,
updateActivity,
warn,
} from './logger';
import { setJoi } from './validator';

describe('info', () => {
let reporter: Reporter;

beforeEach(() => {
// @ts-expect-error: Reporter is intentially not fully mocked
reporter = {
info: jest.fn(),
};
// @ts-expect-error: Gatsby's PluginOptionsSchemaJoi type has an additional `subPlugins` property that we don't need
setJoi(Joi);
setDefaultOptions({ verbose: false });
setReporter(reporter);
});

it('should log an info message when verbose is true', () => {
setDefaultOptions({ verbose: true });
info('This is an info message');
expect(reporter.info).toHaveBeenCalledWith(
'gatsby-plugin-component-to-image: This is an info message',
);
});

it('should not log an info message when verbose is false', () => {
setDefaultOptions({ verbose: false });
info('This is an info message');
expect(reporter.info).not.toHaveBeenCalled();
});
});

describe('success', () => {
it('should log a success message', () => {
const reporter = {
success: jest.fn(),
};

// @ts-expect-error: Reporter is intentially not fully mocked
setReporter(reporter);
success('This is a success message');
expect(reporter.success).toHaveBeenCalledWith(
'gatsby-plugin-component-to-image: This is a success message',
);
});
});

describe('warn', () => {
it('should log a warning message', () => {
const reporter = {
warn: jest.fn(),
};

// @ts-expect-error: Reporter is intentially not fully mocked
setReporter(reporter);
warn('This is a warning message');
expect(reporter.warn).toHaveBeenCalledWith(
'gatsby-plugin-component-to-image: This is a warning message',
);
});
});

describe('error', () => {
it('should log an error message', () => {
const reporter = {
error: jest.fn(),
};

// @ts-expect-error: Reporter is intentially not fully mocked
setReporter(reporter);
error('This is an error message');
expect(reporter.error).toHaveBeenCalledWith(
'gatsby-plugin-component-to-image: This is an error message',
);
});
});

describe('panic', () => {
it('should log a panic message', () => {
const reporter = {
panic: jest.fn(),
};

// @ts-expect-error: Reporter is intentially not fully mocked
setReporter(reporter);
panic('This is a panic message');
expect(reporter.panic).toHaveBeenCalledWith(
'gatsby-plugin-component-to-image: This is a panic message',
);
});
});

describe('startActivity, updateActivity, and endActivity', () => {
const reporter = {
activityTimer: jest.fn(),
};
const activity = {
start: jest.fn(),
setStatus: jest.fn(),
end: jest.fn(),
};

beforeAll(() => {
reporter.activityTimer.mockReturnValue(activity);

// @ts-expect-error: Reporter is intentially not fully mocked
setReporter(reporter);
});

it('startActivity should start an activity', () => {
startActivity('Timer');
expect(reporter.activityTimer).toHaveBeenCalledWith(
'gatsby-plugin-component-to-image: Timer',
);
expect(activity.start).toHaveBeenCalled();
});

it('updateActivity should update the status of an activity', () => {
updateActivity('Status');
expect(activity.setStatus).toHaveBeenCalledWith('Status');
});

it('endActivity should end an activity', () => {
endActivity();
expect(activity.setStatus).toHaveBeenCalledWith('');
expect(activity.end).toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Reporter } from 'gatsby';
import { getDefaultOptions } from './config';
import { getPackageName } from './utilities';
import { getPackageName } from './utils';

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

Expand Down
36 changes: 36 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it } from '@jest/globals';
import { assertIsDefined, getPackageName, prettify } from './utils';

describe('assertIsDefined', () => {
it('should throw an error if value is undefined', () => {
expect(() => assertIsDefined(undefined)).toThrow(
'Expected value to be defined, but it was not',
);
});

it('should not throw an error if value is defined', () => {
expect(() => assertIsDefined('defined value')).not.toThrow();
});
});

describe('getPackageName', () => {
it('should return the package name from package.json', () => {
expect(getPackageName()).toBe('gatsby-plugin-component-to-image');
});
});

describe('prettify', () => {
it('should format an object as a human-readable string', () => {
const obj = {
name: 'Timmy',
size: 'chonky',
color: 'orange',
};
const expectedOutput = `{
"name": "Timmy",
"size": "chonky",
"color": "orange"
}`;
expect(prettify(obj)).toBe(expectedOutput);
});
});
File renamed without changes.
Loading