Skip to content

Commit 24accf0

Browse files
committed
chore: add test cases
1 parent 4e832c2 commit 24accf0

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

src/dynamodb.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { exists } from './utils'
1313

1414
const debug = Debug('dynamodb-local')
1515
const JARNAME = 'DynamoDBLocal.jar'
16-
const runningProcesses: { [port: number]: Subprocess } = {}
16+
17+
export const runningProcesses: { [port: number]: Subprocess } = {}
1718

1819
export const dynamoDb = {
1920
async launch(options?: LaunchOptions): Promise<Subprocess | undefined> {

test/toolkit.test.ts

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,101 @@
1-
import { describe, expect, it } from 'bun:test'
1+
import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test'
2+
import * as fs from 'node:fs'
3+
import * as path from 'node:path'
4+
import { config } from '../src/config'
5+
import { dynamoDb, runningProcesses } from '../src/dynamodb'
6+
import { exists } from '../src/utils'
7+
8+
// Mock external dependencies
9+
mock.module('node:https', () => ({
10+
get: mock(() => ({
11+
on: mock(() => {}),
12+
pipe: mock(() => ({
13+
pipe: mock(() => ({
14+
on: mock(() => {}),
15+
})),
16+
})),
17+
})),
18+
}))
219

320
describe('dynamodb-tooling', () => {
4-
it('exported', () => {
5-
expect(1).toEqual(1)
21+
describe('Config', () => {
22+
it('should load default config', () => {
23+
expect(config).toBeDefined()
24+
expect(config.port).toBe(8000)
25+
expect(config.installPath).toBe('dynamodb-local')
26+
})
27+
})
28+
29+
describe('DynamoDB Local', () => {
30+
const testPort = 8001
31+
let process: any
32+
const originalInstall = dynamoDb.install
33+
34+
beforeAll(async () => {
35+
// Ensure the install directory exists for testing
36+
await fs.promises.mkdir(config.installPath, { recursive: true })
37+
// Create a mock JAR file
38+
await fs.promises.writeFile(path.join(config.installPath, 'DynamoDBLocal.jar'), '')
39+
40+
// Mock the install method
41+
dynamoDb.install = mock(async () => {
42+
await fs.promises.writeFile(path.join(config.installPath, 'DynamoDBLocal.jar'), '')
43+
})
44+
})
45+
46+
afterAll(async () => {
47+
// Clean up the test install directory
48+
await fs.promises.rm(config.installPath, { recursive: true, force: true })
49+
// Restore the original install method
50+
dynamoDb.install = originalInstall
51+
})
52+
53+
it('should launch DynamoDB Local', async () => {
54+
process = await dynamoDb.launch({ port: testPort })
55+
expect(process).toBeDefined()
56+
expect(process.pid).toBeDefined()
57+
})
58+
59+
it('should not launch a second instance on the same port', async () => {
60+
const secondProcess = await dynamoDb.launch({ port: testPort })
61+
expect(secondProcess).toBe(process)
62+
})
63+
64+
it('should stop DynamoDB Local', () => {
65+
dynamoDb.stop(testPort)
66+
expect(runningProcesses[testPort]).toBeUndefined()
67+
})
68+
69+
it('should install DynamoDB Local', async () => {
70+
// Remove the mock JAR file to test installation
71+
await fs.promises.unlink(path.join(config.installPath, 'DynamoDBLocal.jar'))
72+
73+
await dynamoDb.install()
74+
const jarExists = await exists(path.join(config.installPath, 'DynamoDBLocal.jar'))
75+
expect(jarExists).toBe(true)
76+
expect(dynamoDb.install).toHaveBeenCalled()
77+
})
78+
})
79+
80+
describe('Utils', () => {
81+
const testDir = 'test-utils-dir'
82+
83+
beforeAll(async () => {
84+
await fs.promises.mkdir(testDir, { recursive: true })
85+
})
86+
87+
afterAll(async () => {
88+
await fs.promises.rm(testDir, { recursive: true, force: true })
89+
})
90+
91+
it('should check if a file exists', async () => {
92+
const existingFile = path.join(testDir, 'test-file.txt')
93+
await fs.promises.writeFile(existingFile, '')
94+
95+
expect(await exists(existingFile)).toBe(true)
96+
expect(await exists(path.join(testDir, 'non-existent-file.txt'))).toBe(false)
97+
98+
await fs.promises.unlink(existingFile)
99+
})
6100
})
7101
})

0 commit comments

Comments
 (0)