mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-09 05:20:05 +00:00
fix: jest tests RequestClient.spec.ts
This commit is contained in:
@@ -69,5 +69,5 @@ const setupMocks = () => {
|
||||
.mockImplementation(() => Promise.resolve('Test Log'))
|
||||
jest
|
||||
.spyOn(writeStreamModule, 'writeStream')
|
||||
.mockImplementation(() => Promise.resolve())
|
||||
.mockImplementation(() => Promise.resolve(true))
|
||||
}
|
||||
|
||||
@@ -10,12 +10,22 @@ import {
|
||||
describe('writeStream', () => {
|
||||
const filename = 'test.txt'
|
||||
const content = 'test'
|
||||
|
||||
let stream: WriteStream
|
||||
|
||||
beforeAll(async () => {
|
||||
stream = await createWriteStream(filename)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await deleteFile(filename).catch(() => {}) // Ignore errors if the file doesn't exist
|
||||
stream = await createWriteStream(filename)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await deleteFile(filename).catch(() => {}) // Ensure cleanup after test
|
||||
})
|
||||
|
||||
it('should resolve when the stream is written successfully', async () => {
|
||||
await expect(writeStream(stream, content)).toResolve()
|
||||
await expect(fileExists(filename)).resolves.toEqual(true)
|
||||
@@ -25,11 +35,30 @@ describe('writeStream', () => {
|
||||
})
|
||||
|
||||
it('should reject when the write errors out', async () => {
|
||||
// Mock implementation of the write method
|
||||
jest
|
||||
.spyOn(stream, 'write')
|
||||
.mockImplementation((_, callback) => callback(new Error('Test Error')))
|
||||
.mockImplementation(
|
||||
(
|
||||
chunk: any,
|
||||
encodingOrCb?:
|
||||
| BufferEncoding
|
||||
| ((error: Error | null | undefined) => void),
|
||||
cb?: (error: Error | null | undefined) => void
|
||||
) => {
|
||||
const callback =
|
||||
typeof encodingOrCb === 'function' ? encodingOrCb : cb
|
||||
if (callback) {
|
||||
callback(new Error('Test Error')) // Simulate an error
|
||||
}
|
||||
return true // Simulate that the write operation was called
|
||||
}
|
||||
)
|
||||
|
||||
// Call the writeStream function and catch the error
|
||||
const error = await writeStream(stream, content).catch((e: any) => e)
|
||||
|
||||
// Assert that the error is correctly handled
|
||||
expect(error.message).toEqual('Test Error')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3,9 +3,14 @@ import { WriteStream } from '../../types'
|
||||
export const writeStream = async (
|
||||
stream: WriteStream,
|
||||
content: string
|
||||
): Promise<void> =>
|
||||
stream.write(content + '\n', (e: any) => {
|
||||
if (e) return Promise.reject(e)
|
||||
|
||||
return Promise.resolve()
|
||||
): Promise<boolean> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
stream.write(content + '\n', (err: Error | null | undefined) => {
|
||||
if (err) {
|
||||
reject(err) // Reject on write error
|
||||
} else {
|
||||
resolve(true) // Resolve on successful write
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import {
|
||||
AxiosRequestHeaders,
|
||||
AxiosResponse
|
||||
} from 'axios'
|
||||
import axios from 'axios'
|
||||
import * as https from 'https'
|
||||
import { CsrfToken } from '..'
|
||||
import { isAuthorizeFormRequired, isLogInRequired } from '../auth'
|
||||
|
||||
@@ -24,9 +24,17 @@ const axiosActual = jest.requireActual('axios')
|
||||
jest
|
||||
.spyOn(axiosModules, 'createAxiosInstance')
|
||||
.mockImplementation((baseURL: string, httpsAgent?: https.Agent) =>
|
||||
axiosActual.create({ baseURL, httpsAgent })
|
||||
axiosActual.create({ baseURL, httpsAgent, withCredentials: true })
|
||||
)
|
||||
|
||||
jest.mock('util', () => {
|
||||
const actualUtil = jest.requireActual('util')
|
||||
return {
|
||||
...actualUtil,
|
||||
inspect: jest.fn(actualUtil.inspect)
|
||||
}
|
||||
})
|
||||
|
||||
const PORT = 8000
|
||||
const SERVER_URL = `https://localhost:${PORT}/`
|
||||
|
||||
@@ -432,15 +440,18 @@ ${resHeaders[0]}: ${resHeaders[1]}${
|
||||
})
|
||||
|
||||
describe('prettifyString', () => {
|
||||
const inspectMock = UtilsModule.inspect as unknown as jest.Mock
|
||||
|
||||
beforeEach(() => {
|
||||
// Reset the mock before each test to ensure a clean slate
|
||||
inspectMock.mockClear()
|
||||
})
|
||||
|
||||
it(`should call inspect without colors when verbose mode is set to 'bleached'`, () => {
|
||||
const requestClient = new RequestClient('')
|
||||
let verbose: VerboseMode = 'bleached'
|
||||
requestClient.setVerboseMode(verbose)
|
||||
|
||||
jest.spyOn(UtilsModule, 'inspect')
|
||||
requestClient.setVerboseMode('bleached')
|
||||
|
||||
const testStr = JSON.stringify({ test: 'test' })
|
||||
|
||||
requestClient['prettifyString'](testStr)
|
||||
|
||||
expect(UtilsModule.inspect).toHaveBeenCalledWith(testStr, {
|
||||
@@ -448,15 +459,11 @@ ${resHeaders[0]}: ${resHeaders[1]}${
|
||||
})
|
||||
})
|
||||
|
||||
it(`should call inspect with colors when verbose mode is set to 'true'`, () => {
|
||||
it(`should call inspect with colors when verbose mode is set to true`, () => {
|
||||
const requestClient = new RequestClient('')
|
||||
let verbose: VerboseMode = true
|
||||
requestClient.setVerboseMode(verbose)
|
||||
|
||||
jest.spyOn(UtilsModule, 'inspect')
|
||||
requestClient.setVerboseMode(true)
|
||||
|
||||
const testStr = JSON.stringify({ test: 'test' })
|
||||
|
||||
requestClient['prettifyString'](testStr)
|
||||
|
||||
expect(UtilsModule.inspect).toHaveBeenCalledWith(testStr, {
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
import express = require('express')
|
||||
import cors from 'cors'
|
||||
|
||||
export const app = express()
|
||||
|
||||
app.use(
|
||||
cors({
|
||||
origin: 'http://localhost', // Allow requests only from this origin
|
||||
credentials: true // Allow credentials (cookies, auth headers, etc.)
|
||||
})
|
||||
)
|
||||
|
||||
export const mockedAuthResponse = {
|
||||
access_token: 'access_token',
|
||||
token_type: 'bearer',
|
||||
@@ -12,11 +20,11 @@ export const mockedAuthResponse = {
|
||||
jti: 'jti'
|
||||
}
|
||||
|
||||
app.get('/', function (req: any, res: any) {
|
||||
app.get('/', (req: any, res: any) => {
|
||||
res.send('Hello World')
|
||||
})
|
||||
|
||||
app.post('/SASLogon/oauth/token', function (req: any, res: any) {
|
||||
app.post('/SASLogon/oauth/token', (req: any, res: any) => {
|
||||
let valid = true
|
||||
|
||||
// capture the encoded form data
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
export interface WriteStream {
|
||||
write: (content: string, callback: (err?: Error) => any) => void
|
||||
path: string
|
||||
import { WriteStream as FsWriteStream } from 'fs'
|
||||
|
||||
export interface WriteStream extends FsWriteStream {
|
||||
write(
|
||||
chunk: any,
|
||||
encoding?: BufferEncoding | ((error: Error | null | undefined) => void),
|
||||
cb?: (error: Error | null | undefined) => void
|
||||
): boolean
|
||||
path: string | Buffer
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user