Skip to content

Commit

Permalink
replace Mocha with Vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordfirespeed committed Jul 16, 2024
1 parent 481eb21 commit 4eac287
Show file tree
Hide file tree
Showing 8 changed files with 820 additions and 240 deletions.
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"types": "./dist/index.d.ts",
"files": ["dist/**"],
"scripts": {
"test": "mocha --recursive ./src/tests/*.test.ts",
"test": "vitest run --silent",
"test:coverage": "vitest run --silent --coverage --coverage.reporter=lcov",
"clean": "rm -rf ./dist",
"check": "biome check .",
"check:fix": "biome check --write .",
Expand All @@ -40,20 +41,24 @@
"@types/chai": "^4.3.11",
"@types/http-errors": "^2.0.4",
"@types/mocha": "^10.0.6",
"@types/node": "^18.15.8",
"@types/node": "^20.14.10",
"@vitest/coverage-istanbul": "^2.0.3",
"chai": "^4.3.10",
"cookie": "^0.6.0",
"cookie-parser": "^1.4.6",
"cookie-signature": "^1.2.1",
"mocha": "^10.2.0",
"standard-version": "^9.5.0",
"ts-node": "^10.9.2",
"tsup": "^8.1.0",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"vitest": "^2.0.3"
},
"dependencies": {
"@tinyhttp/cookie": "^2.1.1",
"@tinyhttp/cookie-signature": "^2.1.1",
"http-errors": "^2.0.0"
},
"engines": {
"node": ">= 20"
}
}
1,002 changes: 780 additions & 222 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/tests/doublecsrf.test.ts → tests/doublecsrf.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { assert } from "chai"
import { doubleCsrf } from "../index.js"
import type { DoubleCsrfConfig } from "../types"
import { createTestSuite } from "./testsuite.js"
import { HEADER_KEY } from "./utils/constants.js"
import { it, describe, assert } from "vitest"
import { doubleCsrf } from "@/index"
import type { DoubleCsrfConfig } from "@/types"
import { createTestSuite } from "./testsuite"
import { HEADER_KEY } from "./utils/constants"
import {
attachResponseValuesToRequest,
getMultipleSecrets,
Expand Down Expand Up @@ -88,7 +88,7 @@ describe("csrf-csrf token-rotation", () => {
}
}

context("validating requests with combination of different secret/s", () => {
describe("validating requests with combination of different secret/s", () => {
// Generate request --> CSRF token with secret1
// We will then match a request with token and secret1 with other combinations of secrets
const { mockRequest, validateRequest } = generateMocksWithMultipleSecrets(SECRET1)
Expand Down Expand Up @@ -125,7 +125,7 @@ describe("csrf-csrf token-rotation", () => {
})
})

context("should generate tokens correctly, simulating token rotations", () => {
describe("should generate tokens correctly, simulating token rotations", () => {
const getEmptyResponse = () => {
const { mockResponse } = generateMocks()
return mockResponse
Expand Down
13 changes: 7 additions & 6 deletions src/tests/testsuite.ts → tests/testsuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import type { Request, Response } from "@tinyhttp/app"
import { serialize as serializeCookie } from "@tinyhttp/cookie"
import { sign } from "@tinyhttp/cookie-signature"
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { assert, expect } from "chai"
import { doubleCsrf } from "../index.js"
import type { DoubleCsrfConfig } from "../types"
import { HEADER_KEY, TEST_TOKEN } from "./utils/constants.js"
import { getCookieFromRequest, getCookieFromResponse, switchSecret } from "./utils/helpers.js"
import { generateMocks, generateMocksWithToken, next } from "./utils/mock.js"
import { it, describe, assert, expect } from "vitest"
import { doubleCsrf } from "@/index"
import type { DoubleCsrfConfig } from "@/types"

import { HEADER_KEY, TEST_TOKEN } from "./utils/constants"
import { getCookieFromRequest, getCookieFromResponse, switchSecret } from "./utils/helpers"
import { generateMocks, generateMocksWithToken, next } from "./utils/mock"

type CreateTestSuite = (
name: string,
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/tests/utils/mock.ts → tests/utils/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parse } from "@tinyhttp/cookie"
import { cookieParser, signedCookie } from "@tinyhttp/cookie-parser"
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { assert } from "chai"
import type { CsrfRequestValidator, CsrfTokenCreator } from "../../types.js"
import type { CsrfRequestValidator, CsrfTokenCreator } from "@/types.js"
import { COOKIE_SECRET, HEADER_KEY } from "./constants.js"
import { getCookieFromRequest, getCookieValueFromResponse } from "./helpers.js"

Expand Down
16 changes: 16 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from "node:path"
import { defineConfig } from "vitest/config"

export default defineConfig({
resolve: {
alias: {
"@": path.resolve(import.meta.dirname, "./src"),
},
},
test: {
coverage: {
provider: "istanbul",
include: ["src/**"],
},
},
})

0 comments on commit 4eac287

Please sign in to comment.