Setup Jest #57
11 changed files with 2510 additions and 115 deletions
|
@ -6,12 +6,19 @@ steps:
|
||||||
image: node
|
image: node
|
||||||
commands:
|
commands:
|
||||||
- yarn install --frozen-lockfile
|
- yarn install --frozen-lockfile
|
||||||
|
- yarn build
|
||||||
- name: test
|
- name: test
|
||||||
image: node
|
image: node
|
||||||
commands:
|
commands:
|
||||||
- yarn test
|
- yarn test
|
||||||
depends_on:
|
depends_on:
|
||||||
- build
|
- build
|
||||||
|
- name: lint
|
||||||
|
image: node
|
||||||
|
commands:
|
||||||
|
- yarn lint
|
||||||
|
depends_on:
|
||||||
|
- build
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
branch:
|
branch:
|
||||||
|
|
1
.eslintignore
Normal file
1
.eslintignore
Normal file
|
@ -0,0 +1 @@
|
||||||
|
dist/
|
|
@ -44,6 +44,7 @@
|
||||||
"module": "writable",
|
"module": "writable",
|
||||||
"require": "writable",
|
"require": "writable",
|
||||||
"process": "writable",
|
"process": "writable",
|
||||||
"console": "writable"
|
"console": "writable",
|
||||||
|
"jest": "writable"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,5 @@
|
||||||
app.ts
|
app.ts
|
||||||
|
|
||||||
|
tests/
|
||||||
|
jest.config.cjs
|
||||||
|
jest.setup.js
|
4
jest.config.cjs
Normal file
4
jest.config.cjs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
module.exports = {
|
||||||
|
preset: "ts-jest",
|
||||||
|
setupFiles: [ "./jest.setup.js" ]
|
||||||
|
}
|
3
jest.setup.js
Normal file
3
jest.setup.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
jest.setTimeout(1 * 1000); // 1 second
|
||||||
|
jest.resetModules();
|
||||||
|
jest.resetAllMocks();
|
20
package.json
20
package.json
|
@ -17,13 +17,13 @@
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"glob-parent": "^6.0.0",
|
"glob-parent": "^6.0.0",
|
||||||
"got": "^12.0.0",
|
"got-cjs": "^12.5.4",
|
||||||
"linqts": "^1.14.4"
|
"linqts": "^1.14.4"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"start": "ts-node app.ts",
|
"start": "ts-node app.ts",
|
||||||
"test": "echo none",
|
"test": "jest",
|
||||||
"lint": "eslint ."
|
"lint": "eslint ."
|
||||||
},
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
|
@ -33,16 +33,20 @@
|
||||||
"homepage": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny",
|
"homepage": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny",
|
||||||
"funding": "https://ko-fi.com/vylpes",
|
"funding": "https://ko-fi.com/vylpes",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/eslint": "^8.21.1",
|
||||||
|
"@types/jest": "^29.4.0",
|
||||||
"@types/node": "^18.0.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",
|
"ts-node": "^10.9.1",
|
||||||
"typescript": "^5.0.0"
|
"typescript": "^5.0.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist"
|
||||||
],
|
]
|
||||||
"type": "module",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=14.16"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
14
src/index.ts
14
src/index.ts
|
@ -1,6 +1,6 @@
|
||||||
import IReturnResult from "./contracts/IReturnResult.js";
|
import IReturnResult from "./contracts/IReturnResult.js";
|
||||||
import IRedditResult from "./contracts/IRedditResult.js";
|
import IRedditResult from "./contracts/IRedditResult.js";
|
||||||
import fetch from "got";
|
import fetch from "got-cjs";
|
||||||
import { List } from 'linqts';
|
import { List } from 'linqts';
|
||||||
import IFetchResult from "./contracts/IFetchResult.js";
|
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'))
|
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png'))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
|
let random = 0;
|
||||||
|
|
||||||
const randomSelect = dataWithImages[random];
|
if (dataWithImages.length == 0) {
|
||||||
|
|
||||||
if (!randomSelect) {
|
|
||||||
return {
|
return {
|
||||||
IsSuccess: false,
|
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;
|
const randomData = randomSelect.data;
|
||||||
|
|
||||||
|
|
172
tests/index.test.ts
Normal file
172
tests/index.test.ts
Normal 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 it’s 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 it’s 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 it’s 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 it’s 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');
|
||||||
|
});
|
||||||
|
});
|
|
@ -73,7 +73,9 @@
|
||||||
"./src",
|
"./src",
|
||||||
],
|
],
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"./tests"
|
"./tests",
|
||||||
|
"jest.config.cjs",
|
||||||
|
"jest.setup.js"
|
||||||
],
|
],
|
||||||
"ts-node": {
|
"ts-node": {
|
||||||
"esm": true
|
"esm": true
|
||||||
|
|
Loading…
Reference in a new issue