Skip to content

Commit

Permalink
fix: readline and nunjucks now mocked (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonoAugustine committed Jun 3, 2023
1 parent 836f05d commit 527b472
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 22 deletions.
4 changes: 1 addition & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const config: JestConfigWithTsJest = {
// bail: 0,
// Automatically clear mock calls, instances, contexts and results before every test
clearMocks: true,
// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,
// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
// The directory where Jest should output its coverage files
Expand All @@ -24,7 +22,7 @@ const config: JestConfigWithTsJest = {
moduleDirectories: ["node_modules", __dirname],
injectGlobals: true,
testEnvironment: "node",
setupFiles: ["<rootDir>/src/__tests__/setup/module_mocks.ts"],
setupFiles: ["<rootDir>/src/__tests__/module_mocks.ts"],
}

export default config
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"watch/views": "nodemon --watch src -e njk,css --exec yarn build/views",
"watch/ts": "tsc -p tsconfig.build.json --watch",
"watch": "yarn watch/ts & yarn watch/views",
"test": "jest",
"test/coverage": "jest --coverage",
"test": "jest --ci --no-cache",
"prepublishOnly": "yarn build",
"semantic-release": "semantic-release"
},
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/conditionalInterval.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ let watcher: ConditionalInterval

beforeEach(() => {
watcher = new ConditionalInterval(delay, callback)
jest.clearAllTimers()
jest.restoreAllMocks()
})

afterEach(() => {
jest.clearAllTimers()
})

it("should set interval for given delay", async () => {
const timerSpy = jest.spyOn(global, "setTimeout")
await watcher.run()
Expand Down
File renamed without changes.
24 changes: 14 additions & 10 deletions src/__tests__/yuki/yuki.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import Schema$LiveChatMessage = youtube_v3.Schema$LiveChatMessage
jest.useFakeTimers()
jest.mock("google-auth-library")
jest.mock("googleapis")
jest.mock("nunjucks", () => ({
...jest.requireActual("nunjucks"),
render: jest.fn(() => `<html lang="en"></html>`),
}))
jest.mock("../../internal/events/Event", () => {
const actual: typeof import("../../internal/events/Event") =
jest.requireActual("../../internal/events/Event")
Expand Down Expand Up @@ -352,32 +356,32 @@ describe("event announcements", () => {
})

describe("express app", () => {
it("GET root returns html page", () => {
it("GET root returns html page", (done) => {
supertest
.agent(yuki.express)
.get("/")
.expect("Content-Type", /html/)
.expect(200)
.expect(200, done)
})
it("GET /auth redirects to generated url", async () => {
it("GET /auth redirects to generated url", (done) => {
const redirect = "/redirect"
const authUrlSpy = jest
jest
.spyOn(youtubeWrapper, "getAuthUrl")
.mockName("getAuthUrl")
.mockImplementation(() => redirect)
await supertest
supertest
.agent(yuki.express)
.get("/auth")
.expect("Location", new RegExp(redirect))
expect(authUrlSpy).toHaveBeenCalledTimes(1)
.expect("Location", redirect)
.expect(302, done)
})
describe("GET /callback", () => {
it("should redirect home", () => {
it("should redirect home", (done) => {
supertest
.agent(yuki.express)
.get("/callback")
.expect(200)
.expect("Location", new RegExp("/"))
.expect("Location", "/")
.expect(302, done)
})
describe("with code", () => {
const code = "code"
Expand Down
9 changes: 8 additions & 1 deletion src/__tests__/yuki/yukibuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
YukiBuilder,
YukiConfig,
} from "../../logic"
import { Credentials } from "google-auth-library"

jest.useFakeTimers()
jest.mock("google-auth-library")
jest.mock("googleapis")

let googleConfig: GoogleConfig
const yukiConfig: YukiConfig = {
Expand All @@ -27,6 +30,10 @@ beforeEach(() => {
yukiBuilder = new YukiBuilder()
})

afterEach(() => {
jest.clearAllTimers()
})

describe("failure conditions", () => {
describe("google config", () => {
beforeEach(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/logic/yuki/TestYuki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Schema$LiveChatMessage = youtube_v3.Schema$LiveChatMessage
import Schema$Subscription = youtube_v3.Schema$Subscription

export default class TestYuki extends Yuki {
private scanner = readline.createInterface({ input: stdin, output: stdout })
private scanner: readline.Interface

constructor(
yukiConfig: YukiConfig,
Expand Down Expand Up @@ -110,6 +110,7 @@ export default class TestYuki extends Yuki {
override async start(): Promise<boolean> {
const loaders = await this.setup()
if (!loaders) return false
this.scanner = readline.createInterface({ input: stdin, output: stdout })
this.inputWatcher().catch((err) => {
this.logger.error("input watcher failed", { err })
this.restart()
Expand Down
12 changes: 7 additions & 5 deletions src/logic/yuki/Yuki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "../../internal"
import { Credentials } from "google-auth-library"
import express, { Express } from "express"
import nunjucks from "nunjucks"
import nj from "nunjucks"
import { join } from "path"
import BaseYuki from "./BaseYuki"
import { Loader, RouteConfig, YukiConfig } from "./types"
Expand All @@ -33,7 +33,11 @@ export default class Yuki extends BaseYuki {

readonly config: YukiConfig
readonly express: Express = express()
.get("/", (_, res) => res.render("index.njk", this.pageData))
.get("/", (_, res) => {
res
.setHeader("Content-Type", "text/html")
.send(nj.render("index.njk", this.pageData))
})
.get("/auth", (_, res) => res.redirect(this.youtube.getAuthUrl()))
.get("/callback", async (req, res) => {
const code = req.query.code as string
Expand Down Expand Up @@ -65,9 +69,7 @@ export default class Yuki extends BaseYuki {
this.routes = routes
this.tokenLoader = tokenLoader
if (userCacheLoader !== undefined) this.userCacheLoader = userCacheLoader
nunjucks.configure(join(__dirname, "../../views"), {
express: this.express,
})
nj.configure(join(__dirname, "../../views"))

this.chatWatcher = cIntervalOf(
secondsOf(this.config.chatPollRate),
Expand Down

0 comments on commit 527b472

Please sign in to comment.