Skip to content

Commit

Permalink
chore: add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Sep 26, 2024
1 parent 4e832c2 commit 24accf0
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { exists } from './utils'

const debug = Debug('dynamodb-local')
const JARNAME = 'DynamoDBLocal.jar'
const runningProcesses: { [port: number]: Subprocess } = {}

export const runningProcesses: { [port: number]: Subprocess } = {}

export const dynamoDb = {
async launch(options?: LaunchOptions): Promise<Subprocess | undefined> {
Expand Down
100 changes: 97 additions & 3 deletions test/toolkit.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,101 @@
import { describe, expect, it } from 'bun:test'
import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test'
import * as fs from 'node:fs'
import * as path from 'node:path'
import { config } from '../src/config'
import { dynamoDb, runningProcesses } from '../src/dynamodb'
import { exists } from '../src/utils'

// Mock external dependencies
mock.module('node:https', () => ({
get: mock(() => ({
on: mock(() => {}),
pipe: mock(() => ({
pipe: mock(() => ({
on: mock(() => {}),
})),
})),
})),
}))

describe('dynamodb-tooling', () => {
it('exported', () => {
expect(1).toEqual(1)
describe('Config', () => {
it('should load default config', () => {
expect(config).toBeDefined()
expect(config.port).toBe(8000)
expect(config.installPath).toBe('dynamodb-local')
})
})

describe('DynamoDB Local', () => {
const testPort = 8001
let process: any
const originalInstall = dynamoDb.install

beforeAll(async () => {
// Ensure the install directory exists for testing
await fs.promises.mkdir(config.installPath, { recursive: true })
// Create a mock JAR file
await fs.promises.writeFile(path.join(config.installPath, 'DynamoDBLocal.jar'), '')

// Mock the install method
dynamoDb.install = mock(async () => {
await fs.promises.writeFile(path.join(config.installPath, 'DynamoDBLocal.jar'), '')
})
})

afterAll(async () => {
// Clean up the test install directory
await fs.promises.rm(config.installPath, { recursive: true, force: true })
// Restore the original install method
dynamoDb.install = originalInstall
})

it('should launch DynamoDB Local', async () => {
process = await dynamoDb.launch({ port: testPort })
expect(process).toBeDefined()
expect(process.pid).toBeDefined()
})

it('should not launch a second instance on the same port', async () => {
const secondProcess = await dynamoDb.launch({ port: testPort })
expect(secondProcess).toBe(process)
})

it('should stop DynamoDB Local', () => {
dynamoDb.stop(testPort)
expect(runningProcesses[testPort]).toBeUndefined()
})

it('should install DynamoDB Local', async () => {
// Remove the mock JAR file to test installation
await fs.promises.unlink(path.join(config.installPath, 'DynamoDBLocal.jar'))

await dynamoDb.install()
const jarExists = await exists(path.join(config.installPath, 'DynamoDBLocal.jar'))
expect(jarExists).toBe(true)
expect(dynamoDb.install).toHaveBeenCalled()
})
})

describe('Utils', () => {
const testDir = 'test-utils-dir'

beforeAll(async () => {
await fs.promises.mkdir(testDir, { recursive: true })
})

afterAll(async () => {
await fs.promises.rm(testDir, { recursive: true, force: true })
})

it('should check if a file exists', async () => {
const existingFile = path.join(testDir, 'test-file.txt')
await fs.promises.writeFile(existingFile, '')

expect(await exists(existingFile)).toBe(true)
expect(await exists(path.join(testDir, 'non-existent-file.txt'))).toBe(false)

await fs.promises.unlink(existingFile)
})
})
})

0 comments on commit 24accf0

Please sign in to comment.