Setup Jest (#57)
All checks were successful
continuous-integration/drone/push Build is passing

- Setup Jest
- Add test scripts
- Configure Drone

Fixes #1

Co-authored-by: Ethan Lane <ethan@vylpes.com>
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/57
This commit is contained in:
Vylpes 2023-04-22 13:20:30 +01:00
parent 2ab2fe04fd
commit 06485e0a88
11 changed files with 2510 additions and 115 deletions

View file

@ -6,12 +6,19 @@ steps:
image: node
commands:
- yarn install --frozen-lockfile
- yarn build
- name: test
image: node
commands:
- yarn test
depends_on:
- build
- name: lint
image: node
commands:
- yarn lint
depends_on:
- build
trigger:
branch:

1
.eslintignore Normal file
View file

@ -0,0 +1 @@
dist/

View file

@ -44,6 +44,7 @@
"module": "writable",
"require": "writable",
"process": "writable",
"console": "writable"
"console": "writable",
"jest": "writable"
}
}

View file

@ -1 +1,5 @@
app.ts
app.ts
tests/
jest.config.cjs
jest.setup.js

4
jest.config.cjs Normal file
View file

@ -0,0 +1,4 @@
module.exports = {
preset: "ts-jest",
setupFiles: [ "./jest.setup.js" ]
}

3
jest.setup.js Normal file
View file

@ -0,0 +1,3 @@
jest.setTimeout(1 * 1000); // 1 second
jest.resetModules();
jest.resetAllMocks();

View file

@ -17,13 +17,13 @@
],
"dependencies": {
"glob-parent": "^6.0.0",
"got": "^12.0.0",
"got-cjs": "^12.5.4",
"linqts": "^1.14.4"
},
"scripts": {
"build": "tsc",
"start": "ts-node app.ts",
"test": "echo none",
"test": "jest",
"lint": "eslint ."
},
"bugs": {
@ -33,16 +33,20 @@
"homepage": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny",
"funding": "https://ko-fi.com/vylpes",
"devDependencies": {
"@types/eslint": "^8.21.1",
"@types/jest": "^29.4.0",
"@types/node": "^18.0.0",
"eslint": "^8.0.0",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"eslint": "^8.35.0",
"jest": "^29.4.3",
"jest-mock-extended": "^3.0.3",
"ts-jest": "^29.0.5",
"ts-mockito": "^2.6.1",
"ts-node": "^10.9.1",
"typescript": "^5.0.0"
},
"files": [
"dist"
],
"type": "module",
"engines": {
"node": ">=14.16"
}
]
}

View file

@ -1,6 +1,6 @@
import IReturnResult from "./contracts/IReturnResult.js";
import IRedditResult from "./contracts/IRedditResult.js";
import fetch from "got";
import fetch from "got-cjs";
import { List } from 'linqts';
import IFetchResult from "./contracts/IFetchResult.js";
@ -35,15 +35,17 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png'))
.ToArray();
const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
let random = 0;
const randomSelect = dataWithImages[random];
if (!randomSelect) {
if (dataWithImages.length == 0) {
return {
IsSuccess: false,
};
};
} else if (dataWithImages.length > 1) {
random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
}
const randomSelect = dataWithImages[random];
const randomData = randomSelect.data;

172
tests/index.test.ts Normal file
View file

@ -0,0 +1,172 @@
import randomBunny from "../src/index";
import fetch from "got-cjs";
jest.mock('got-cjs');
const fetchMock = jest.mocked(fetch);
describe('randomBunny', () => {
test('GIVEN subreddit AND sortBy is supplied, EXPECT successful result', async() => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [
{
data: {
archived: false,
downs: 0,
hidden: false,
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
subreddit: 'Rabbits',
subreddit_subscribers: 298713,
title: 'Someone told pickles its Monday… *internal fury*',
ups: 1208,
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
},
},
],
}
}),
});
const result = await randomBunny('rabbits', 'new');
expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
});
test('GIVEN sortBy is NOT supplied, expect it to default to hot', async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [
{
data: {
archived: false,
downs: 0,
hidden: false,
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
subreddit: 'Rabbits',
subreddit_subscribers: 298713,
title: 'Someone told pickles its Monday… *internal fury*',
ups: 1208,
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
},
},
],
}
}),
});
const result = await randomBunny('rabbits');
expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
});
test('GIVEN sortBy is NOT valid, expect it to default to hot', async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [
{
data: {
archived: false,
downs: 0,
hidden: false,
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
subreddit: 'Rabbits',
subreddit_subscribers: 298713,
title: 'Someone told pickles its Monday… *internal fury*',
ups: 1208,
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
},
},
],
}
}),
});
const result = await randomBunny('rabbits', 'invalid');
expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
});
test('GIVEN the fetch fails, EXPECT failure result', async () => {
fetchMock.mockResolvedValue(null);
const result = await randomBunny('rabbits', 'new');
expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
});
test('GIVEN the result is NOT valid JSON, EXPECT failure result', async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify(null),
});
const result = await randomBunny('rabbits', 'new');
expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
});
test('GIVEN randomSelect does NOT find a response, EXPECT failure result', async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [],
}
}),
});
const result = await randomBunny('rabbits', 'new');
expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
});
test('GIVEN randomSelect does NOT find a valid response, EXPECT failure result', async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [
{
data: {
archived: false,
downs: 0,
hidden: false,
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
subreddit: 'Rabbits',
subreddit_subscribers: 298713,
title: 'Someone told pickles its Monday… *internal fury*',
ups: 1208,
url: 'https://i.redd.it/cr8xudsnkgua1.webp',
},
},
],
}
}),
});
const result = await randomBunny('rabbits', 'new');
expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
});
});

View file

@ -73,7 +73,9 @@
"./src",
],
"exclude": [
"./tests"
"./tests",
"jest.config.cjs",
"jest.setup.js"
],
"ts-node": {
"esm": true

2391
yarn.lock

File diff suppressed because it is too large Load diff